Question

I want to import Excel data to Microsoft Dynamics Ax. I've used this code from this site:

http://axpedia.blogspot.com.tr/2013/02/import-from-excel-file-using-x-in-ax.html

but when i try to run i get this error:

SysExcellworksheet could not start (C)\Classes\SysExcellWorksheets\cells(C)\Jobs\ProductType  (line 35)

I am newbie Can you help me about this?

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); //Here 3 is the worksheet Number
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+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}
Was it helpful?

Solution

You probably translated the error, the exact error when I test this is:

SysExcelWorksheet object not initialized.

This is probably because on the following line you are trying to retrieve the 4th worksheet of the excel, but your excel file has less:

worksheet = worksheets.itemFromNum(4);

If your data is on the first worksheet, use '1' instead of '4':

worksheet = worksheets.itemFromNum(1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top