WorksheetPage::OpenExcel
OpenExcel
Description
Open or import an Excel file. The int return version was added for Origin 8 and provides more useful error returns, while the BOOL return version was kept for backward compatible reason.
Syntax
int OpenExcel( int nMode, LPCSTR lpcszxlsFile, LPCSTR lpcszSheet = NULL, DWORD dwCntrl = 0, int nMaxNumEmptyColumnsBeyondLastData = 0 )
int OpenExcel( int nMode, LPCSTR lpcszxlsFile, int nSheetIndex, DWORD dwCntrl = 0, int nMaxNumEmptyColumnsBeyondLastData = 0 )
BOOL OpenExcel( LPCSTR lpcszxlsFile, LPCSTR lpcszSheetName = NULL )
BOOL OpenExcel( LPCSTR lpcszxlsFile, int nSheetIndex = -1 )
Parameters
- nMode
- [input] can be one of XL_OPEN, or XL_IMPORT. XL_OPEN will open the excel book as Excel. XL_IMPORT will import the Excel book as Origin Workbook.
- lpcszxlsFile
- [input]Path and name of the *.XLS file to be opened or imported.
- lpcszSheet
- [input]The name of the Excel sheet inside the workbook to open. If NULL, open/import all sheets in the Excel file, otherwise open/import the named sheet.
- nSheetIndex
- [input]The index(0,1,2) of the Excel sheet inside the workbook to open. An index of -1 will open the active (top) sheet in the book.
- dwCntrl
- [input] 0 will import both values and styles, but XLIMP_SKIP_STYLES will import values only and thus can be faster
- nMaxNumEmptyColumnsBeyondLastData
- [input] due to the presence of styles that may extend beyond the reaches of nonempty cells, additional columns mayy have to be added during import. nMaxNumEmptyColumnsBeyondLastData determines how many additonal columns at most may get added.
Return
int OpenExcel( int nMode, LPCSTR lpcszxlsFile, ...)
Returns 0 on successful exit and
-1 : if lpcszxlsFile is not found
-2 : if lpcszxlsFile lack the XLS extension
-3 : if lpcszxlsFile cannot be open/imported
BOOL OpenExcel( LPCSTR lpcszWorkbookFile, ...)
Returns TRUE on successful exit and FALSE on failure.
Examples
EX1
//Assume there is already an excel file in C:\
int WorksheetPage_OpenExcel_ex1(string strFile = "C:\\Book1.xls")
{
WorksheetPage wp;
int nErr = wp.OpenExcel(XL_IMPORT, strFile);
if( wp )
{
Worksheet wks = wp.Layers();
printf("Opened [%s] as an Origin workbook (%s) with Sheet '%s' active", strFile, wp.GetName(), wks.GetName());
}
else
printf("Import Excel failed, error code = %d\n", nErr);
return nErr;
}
EX2
int WorksheetPage_OpenExcel_ex2(string strWorkbookPathname = "C:\\Book1.xls")
{
WorksheetPage wp;
BOOL bOK = wp.OpenExcel(strWorkbookPathname, "Sheet2");
if( wp )
{
Worksheet wks = wp.Layers();
if( wks )
{
printf("Opened '%s' as an Origin workbook with '%s' active", wp.GetName(), wks.GetName());
return 0;
}
}
printf("Invalid page or sheet!\n");
return 0;
}
EX3
int WorksheetPage_OpenExcel_ex3(string strWorkbookPathname,string strSheetName)
{
WorksheetPage wp;
BOOL bOK = wp.OpenExcel(strWorkbookPathname, strSheetName);
if( wp )
{
Worksheet wks = wp.Layers();
if( wks )
{
printf("Opened '%s' as an Origin workbook with '%s' active", wp.GetName(), wks.GetName());
return 0;
}
}
printf("Invalid page or sheet!\n");
return 0;
}
EX4
int WorksheetPage_OpenExcel_ex4(string strWorkbookPathname)
{
WorksheetPage wp;
BOOL bOK = wp.OpenExcel(strWorkbookPathname, 2);
if( wp )
{
Worksheet wks = wp.Layers();
if( wks )
{
printf("Opened '%s' as an Origin workbook with '%s' active", wp.GetName(), wks.GetName());
return 0;
}
}
printf("Invalid page or sheet!\n");
return 0;
}
EX5
int WorksheetPage_OpenExcel_ex5(string strWorkbookPathname)
{
WorksheetPage wp;
BOOL bOK = wp.OpenExcel(strWorkbookPathname, -1);
if( wp )
{
Worksheet wks = wp.Layers();
if( wks )
{
printf("Opened '%s' as an Origin workbook with '%s' active", wp.GetName(), wks.GetName());
return 0;
}
}
printf("Invalid page or sheet!\n");
return 0;
}
Remark
See Also
WorksheetPage::ExcelSheet
header to Include
origin.h
|