EXCELL IMPORT IN AX 2012




static void LTWeightimport(Args _args)
{

    Dialog                      dialog;
    Filename                    filename;
    DialogField                 dialogFilename;
    //To filter the files while selecting
    container                   conFilter = ["Microsoft Excel 97-2003 Worksheet (.xls)" ,"*.xlsx"];


    SysExcelApplication                 application;
    SysExcelWorkbooks                   workbooks;
    SysExcelWorkbook                    workbook;
    SysExcelWorksheets                  worksheets;
    SysExcelWorksheet                   worksheet;
    SysExcelCells                       cells;
    COMVariantType                      type;
    int                                 row=1;

    ItemId                           itemId;
    InventTable                      inventtable;
    ItemNetWeight                    ItemNetWeight;
    ItemTaraWeight                   ItemTaraWeight;


    ;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    dialog = new dialog();
    dialog.caption("select a file");
    dialogFilename      =   dialog.addField(typeId(FilenameOpen));
    dialog.filenameLookupFilter(conFilter);
    dialog.run();

    if(dialog.closedOk())
    {
        filename = dialogFileName.value();
    }

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();

    do
{


        row++;
        itemId                             = cells.item(row,1).value().bStr();
        ItemNetWeight                      = cells.item(row,2).value().double();
        ItemTaraWeight                     = cells.item(row,3).value().double();
        inventtable.reread();
            ttsbegin;
               select forupdate inventtable
                    where inventtable.ItemId   == itemId;
                    if(inventtable)
                    {

                        inventtable.NetWeight = ItemNetWeight;
                        inventtable.TaraWeight = ItemTaraWeight;
                        inventtable.doUpdate();
                    }


              ttscommit;
              info(strfmt("%1",inventtable.ItemId));
        type = cells.item(row+1, 1).value().variantType();
        } while(type != COMVariantType::VT_EMPTY);


    workbooks.close();
    application.quit();
    info("Operation/Processing Completed");
}

Comments

Popular Posts