2.2.4.12.15 Datasheet::ImportExcel

Description

Import an Excel sheet (97-2003 *.xls format) by replacing existing data

Syntax

int ImportExcel( LPCSTR lpcszFile, int nSheet, XLSReadCntrl * pst )

Parameters

lpcszFile
[input] fullpath filename to an existing Excel file
nSheet
[input] 0-offset index of the Excel sheet to import
pst
[input] option on import

Return

0 if no error, otherwise <0 error codes, can one of EXCEL_ERR_* enumeration;

Examples

EX1

#include <oExtFile.h>
void impxls(string strFile, bool bHeading = true, int nSheet = 0, bool bCellStyles = false)
{
	vector<string> vsSheets;
	int nn = get_excel_sheet_names(strFile, &vsSheets);
	if(nn <= 0) {
		out_str("not a valid Excel file");
		return;
	}
	Worksheet wks = Project.ActiveLayer();

	ExcelImportOptions stR;
	stR.xlsctrl.m_dwRXC = RXC_RESET_FORMAT | RXC_VALUE | RXC_VALUE_FORMAT | RXC_AUTO_RENAME_SHEET;
	if(bCellStyles)
		stR.xlsctrl.m_dwRXC |= RXC_STYLE;

	if(bHeading) {
		stR.header.SubHeaderLines = -1;
		stR.header.LongName = 1;
	}
	int nC2=-1;//to receive output actual last column
	
	//destination columns
	stR.xlsctrl.m_nStartColIndex = 0;
	stR.xlsctrl.m_pnLastColIndex = &nC2;

	int nErr = wks.ImportExcel(strFile, nSheet, &stR, true);
	if(nErr != 0)
		out_int("failed: ", nErr);
}

Remark

To import Excel sheet with *.xlsx format, use Excel COM.

See Also

Header to Include

origin.h