Imports data from Excel in AX2012 using X++ programming

Hi All,

Pls tell how to read excel file which is having multiple sheets…??

Thanks in Advance…

I referred this link…
community.dynamics.com/…/how-to-read-write-an-excel-file-through-x-code

Have a look at \Classes\COSExcelImport

BTW, you should be looking at using the available options like DIXF, Excel add-in for importing data from Excel.

Hi kranthi,

static void SK_ReadExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
int itemid;
Name name;
FileName filename;
    real salary;
    int i,counter;
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = "E:\\Sarath\\SarathCsv.xls";
try
{
    workbooks.open(filename);
}
catch (Exception::Error)
{
    throw error("File cannot be opened.");
}
   
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
  counter = worksheets.count();
    for(i=1;i<=counter;i++)
    {
worksheet = worksheets.itemFromNum(i);
cells = worksheet.cells();
do
{
        row++;
    if(i==1)
    {

    itemId = any2int(cells.item(row, 1).value().toString());
    name = cells.item(row, 2).value().bStr();
    info(strfmt('%1 - %2', itemId, name));
        // type = cells.item(row+1, 1).value().variantType();
   // break;
       // continue;
    }
    
    if(i==2)
    {
        //  row++;
        salary=any2real(cells.item(row,1).value().toString());
         info(strfmt('%1',salary));
       
       
    
    }
      type = cells.item(row+1,1).value().variantType();
    }
while (type != COMVariantType::VT_EMPTY);
    }
    
application.quit();

}

It works for first sheet only…Can u tell me what is the issue with my code…

Thanks in Advance…

Hi,

SarathCsv.xls (7 KB)

This is my xls document

Hi,
Any changes…???

pastedimage1493119464517v2.png

Hi Kranthi,
It doesn’t reads the second sheet.It reads the first sheet twice…

Hi,
Sorry mistake was mine…I have used double() instead of tostring()…Its working…