How to read Excelfile using x++ code in ax 2012
Hi Guys,
The following code will helps you to read Excel file using x++ code.
static void howToReadExcelSheet(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
FileName filename;
date expiryDate,warrentyDate;
AccountNum accountNum;
row = 1;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
filename = @'C:\TestFolder\test.xlsx';
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;
accountNum = cells.item(row,1).value().bStr();
expiryDate = cells.item(row,2).value().date();
warrentyDate = cells.item(row,3).value().date();
type = cells.item(row+1, 1).value().variantType();
}
while(type != COMVariantType::VT_EMPTY);
application.quit();
info(strFmt(" No.of rows: %1",row));
}
Thanks.
The following code will helps you to read Excel file using x++ code.
static void howToReadExcelSheet(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
FileName filename;
date expiryDate,warrentyDate;
AccountNum accountNum;
row = 1;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
filename = @'C:\TestFolder\test.xlsx';
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;
accountNum = cells.item(row,1).value().bStr();
expiryDate = cells.item(row,2).value().date();
warrentyDate = cells.item(row,3).value().date();
type = cells.item(row+1, 1).value().variantType();
}
while(type != COMVariantType::VT_EMPTY);
application.quit();
info(strFmt(" No.of rows: %1",row));
}
Thanks.
Comments
Post a Comment