Excel read through x++ in ax2012:
static void ReadExcelFile(Args _args)
{
SysExcelApplication excel;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells,Rowcell;
COMVariantType type;
int row;
CustAccount account;
CustName name;
#define.filename(@"D:\ReadExcel.xlsx")
// convert into str from excel cell value
//str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
//{
//switch (_cv.variantType())
//{
//case (COMVariantType::VT_BSTR):
//return _cv.bStr();
//case (COMVariantType::VT_R4):
//return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_R8):
//return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_DECIMAL):
//return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_DATE):
//return date2str(_cv.date(),123,2,1,2,1,4);
//case (COMVariantType::VT_EMPTY):
//return "";
//default:
//throw error(strfmt("@SYS26908", _cv.variantType()));
//}
//return "";
//}
//;
//
try
{
excel = SysExcelApplication::construct();
workbooks = excel.workbooks();
workbooks.open(#filename);
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
//row = 1;
type = cells.item(row+1, 1).value().variantType();
while (type != COMVariantType::VT_EMPTY)
{
row++;
if(type == COMVariantType::VT_BSTR )
{
account = strFmt("%1", cells.item(row, 1).value().bStr());
}
else
{
account =num2str((cells.item(row, 1).value().double()),0,0,0,0);
}
name = cells.item(row, 2).value().bStr();
info(strFmt('%1 - %2', account, name));
type = cells.item(row+1, 1).value().variantType();
}
excel.quit();
}
catch
{
if (workbook)
{
workbook.close();
}
if (excel)
{
excel.quit();
}
}
}
static void ReadExcelFile(Args _args)
{
SysExcelApplication excel;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells,Rowcell;
COMVariantType type;
int row;
CustAccount account;
CustName name;
#define.filename(@"D:\ReadExcel.xlsx")
// convert into str from excel cell value
//str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
//{
//switch (_cv.variantType())
//{
//case (COMVariantType::VT_BSTR):
//return _cv.bStr();
//case (COMVariantType::VT_R4):
//return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_R8):
//return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_DECIMAL):
//return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
//case (COMVariantType::VT_DATE):
//return date2str(_cv.date(),123,2,1,2,1,4);
//case (COMVariantType::VT_EMPTY):
//return "";
//default:
//throw error(strfmt("@SYS26908", _cv.variantType()));
//}
//return "";
//}
//;
//
try
{
excel = SysExcelApplication::construct();
workbooks = excel.workbooks();
workbooks.open(#filename);
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
//row = 1;
type = cells.item(row+1, 1).value().variantType();
while (type != COMVariantType::VT_EMPTY)
{
row++;
if(type == COMVariantType::VT_BSTR )
{
account = strFmt("%1", cells.item(row, 1).value().bStr());
}
else
{
account =num2str((cells.item(row, 1).value().double()),0,0,0,0);
}
name = cells.item(row, 2).value().bStr();
info(strFmt('%1 - %2', account, name));
type = cells.item(row+1, 1).value().variantType();
}
excel.quit();
}
catch
{
if (workbook)
{
workbook.close();
}
if (excel)
{
excel.quit();
}
}
}
No comments:
Post a Comment