Wednesday, April 13, 2016

Import From Excel File Using X++ in AX 2012

Import From Excel File Using X++ in AX 2012


static void ProductType(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
Name name;
FileName filename;
ProductType productType;
int row;
int _productTypeId;
str _productType;
str _description;
;

application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "Path\\filename.xlsx";
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}

workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(4); 
cells = worksheet.cells();
do
{
row++;
_productTypeId = any2int(cells.item(row, 1).value().toString());
_productType = cells.item(row, 2).value().bStr();
_description = cells.item(row, 3).value().bStr();

productType.ID = _productTypeId;
productType.ProductType = _productType;
productType.Description = _description;
productType.insert();

type = cells.item(row+11).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...