This post is machine-translated. The original post in german language can be found here.
These post applies to following version:
Dynamics AX 2009
Dynamics AX 2009
|
|
|
|
|
|
|
This post is machine-translated. The original post in german language can be found here.
These post applies to following version:
Dynamics AX 2009
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Recently I was forced to deal with how to read data from an Excel file into AX. Therefore in the following a job with a kind of basic structure, how to solve something like this in X++.
static void importFromExcel(Args _args) { Filename fileNameExcel = "C:\temp\file.xls"; SysExcelApplication sysExcelApplication; SysExcelWorkbooks sysExcelWorkbooks; SysExcelWorksheets sysExcelWorksheets; SysExcelWorksheet sysExcelWorksheet; SysExcelRange sysExcelRange; SysExcelCells sysExcelCells; SysExcelWorkbooks sysExcelWorkBooksCollection; str column_a; str column_b; str column_c; str column_d; int i = 0; #Excel ; sysExcelApplication = SysExcelApplication::construct(); sysExcelWorkbooks = sysExcelApplication.workbooks(); sysExcelWorkbooks.open(fileNameExcel); sysExcelWorksheets = sysExcelApplication.worksheets(); sysExcelWorksheet = sysExcelWorksheets.itemFromNum(1); sysExcelRange = sysExcelWorksheet.rows(); sysExcelCells = sysExcelWorksheet.cells(); try { ttsbegin; while (sysExcelCells.item(i+1, 1).value().variantType() != ComVariantType::VT_EMPTY) // In der Annahme, dass die erste Spalte nicht leer ist { i++; column_a = sysExcelCells.item(i, 1).value().bStr(); column_b = sysExcelCells.item(i, 2).value().bStr(); column_c = sysExcelCells.item(i, 3).value().bStr(); column_d = sysExcelCells.item(i, 4).value().bStr(); // ... do something ... } ttscommit; info("Finished"); sysExcelApplication.quit(); sysExcelApplication = null; } catch (Exception::Error) { sysExcelApplication.quit(); sysExcelApplication = null; } }