Example
The following example lets the user choose to import or export an Excel sheet. Message boxes relay the status and result of the action.
To access the file used in this sample, change the path in the code to the path for your product installation's \SAMPLES\FILES directory.
C++
void CmyWnd::OnCommand1()
{
// Declare variables
VARIANT varr,varg;
SAFEARRAYBOUND rgsabound[1];
bool y,z;
short x, listcount,handle;
long lg=0;
VariantClear(&varr);
VariantInit(&varr);
VariantClear(&varg);
VariantInit(&varg);
varg.vt = VT_BSTR;
varr.vt = VT_ARRAY | VT_BSTR;
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 4;
varr.parray = SafeArrayCreate(VT_BSTR,1,rgsabound);
// Check if file is an Excel file and set result to x
x = m_Spread1.IsExcelFile("C:\\Samples\\Files\\quotes.XLS ");
// If file is Excel file, tell user, import sheet
// list, and set result to y
if (x=1)
{
MessageBox("File is an Excel file.");
y = m_Spread1.GetExcelSheetList("C:\\Samples\\Files\\quotes.XLS ", varr, &listcount,"C:\\Samples\\Files\\log.txt", &handle, TRUE);
// If received sheet list, tell user, import file,
// and set result to z
if (y)
{
MessageBox("Got sheet list.");
SafeArrayGetElement(varr.parray, &lg, &varg.bstrVal);
z = m_Spread1.ImportExcelSheet(handle, varg);
// Tell user result based on T/F value of z
if (z)
MessageBox("Import complete.");
else
MessageBox("Import did not succeed.");
}
else
// Tell user cannot obtain sheet list
MessageBox("Cannot return information for Excel file.");
}
else
// Tell user file is not Excel file or is locked
MessageBox("File is not an Excel file or is locked and cannot be imported.");
}
void CmyWnd::OnCommand2()
{
bool x;
// Export Excel file and set result to x
x = m_Spread1.ExportToExcel("C:\\Samples\\FILE.XLS", "Test Sheet 1", "C:\\Samples\\LOGFILE.TXT");
// Display result to user based on T/F value of x
If (x)
MessageBox("Export complete.");
else
MessageBox("Export did not succeed.");
}
Visual Basic
Sub Form_Load()
' Set up buttons
Command1.Caption = "Import Excel Sheet"
Command2.Caption = "Export Excel Sheet"
End Sub
Sub Command1_Click()
' Declare variables
Dim y As Boolean, z As Boolean
Dim Var As Variant
Dim x As Integer, listcount As Integer, handle As Integer
Dim List(10) As String
' Check if file is an Excel file and set result to x
x = fpSpread1.IsExcelFile("C:\Samples\Files\quotes.XLS")
' If file is Excel file, tell user, import sheet
' list, and set result to y
If x = 1 Then
MsgBox "File is an Excel file.", , "File Type"
y = fpSpread1.GetExcelSheetList("C:\Samples\Files\quotes.XLS", List, listcount, "C:\Samples\Files\ILOGFILE.TXT", handle, True)
' If received sheet list, tell user, import file,
' and set result to z
If y = True Then
MsgBox "Got sheet list.", , "Status"
z = fpSpread1.ImportExcelSheet(handle, 0)
' Tell user result based on T/F value of z
If z = True Then
MsgBox "Import complete.", , "Result"
Else
MsgBox "Import did not succeed.", , "Result"
End If
Else
' Tell user cannot obtain sheet list
MsgBox "Cannot return information for Excel file.", , "Result"
End If
Else
' Tell user file is not Excel file or is locked
MsgBox "File is not an Excel file or is locked and cannot be imported.", ,"Invalid File Type or Locked"
End If
End Sub
Sub Command2_Click()
Dim x As Boolean
' Export Excel file and set result to x
x = fpSpread1.ExportToExcel("C:\Samples\FILE.XLS", "Test Sheet 1", "C:\Samples\LOGFILE.TXT")
' Display result to user based on T/F value of x
If x = True Then
MsgBox "Export complete.", , "Result"
Else
MsgBox "Export did not succeed.", , "Result"
End If
End Sub