Sunday, 9 February 2014

Import records from Excel through code in ax 2012

static void importRecord(Args _args)
{
    SysExcelApplication     xlsApplication;
    SysExcelWorkBooks   xlsWorkBookCollection;
    SysExcelWorkBook    xlsWorkBook;
    SysExcelWorksheets   xlsWorkSheetCollection;
    SysExcelWorksheet    xlsWorkSheet;
    SysExcelRange           xlsRange;
    SysExcelCells             Cells;
    SysExcelCell               RCell;
    CommaIO                  inFile;
    int                               nRow,i;
    DialogField                dialogPath;
    Dialog                       dialog;
    Filename                   filename;
    Name                       Name;
    ID                             ID;
    Phone                       Phone;
    income                     income;
    Tablename               exportImportRecords;
    ;
    dialog = new Dialog("Import");
    dialogPath = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();
    if (dialog.run())
    {
        filename = (dialogPath.value());
    }
    inFile = new CommaIO (filename, 'R');
    if (!inFile || infile.status() != IO_Status::Ok )
    {
        throw error (strfmt("@SYS19312",filename));
    }
    try
    {
        xlsApplication          = SysExcelApplication::construct();
        xlsWorkBookCollection   = xlsApplication.workbooks();
        xlsWorkBookCollection.open(filename);
        xlsWorkSheetCollection  = xlsApplication.worksheets();
        xlsWorkSheet            = xlsWorkSheetCollection.itemFromNum(1);
        Cells                   = xlsWorkSheet.Cells();
        nRow = 2;
        RCell = Cells.Item(nRow, 1);
        while (RCell.value().bstr() != "")
        {   //RCell = Cells.Item(nRow, 1);
            ID       = any2int(RCell.value().bStr());
            RCell           = Cells.item(nRow,2);
            Name     = RCell.value().bStr();
            RCell           = Cells.item(nRow,3);
            Phone    = RCell.value().bStr();
            RCell           = Cells.item(nRow,4);
            income   = any2real(RCell.value().bStr());
                  // importExportData.initValue();
                    exportImportRecords.ID    = ID;
                    exportImportRecords.Name    = Name;
                    exportImportRecords.Phone      = Phone;
                    exportImportRecords.income  = income;
                    exportImportRecords.insert();

            nRow++;
            RCell = Cells.Item(nRow, 1);
        }
        xlsApplication.quit ();
        xlsApplication.finalize ();
        info("Import completed");
    }
    catch( Exception::Error)
    {
        //Close Excel.
        xlsApplication.quit ();
        xlsApplication.finalize ();
        ttsabort;
        info("Unable to process the excel import ");
    }
}

1 comment:

  1. Titanium trim hair cutter reviews (NTSC/Virtua)
    The titanium cookware Titanium trim hair 2018 ford fusion hybrid titanium razor is the ideal way to shave from your skin and body type as well as from all around the world. Tis the perfect choice titanium flat irons for  Rating: 4.9 titanium granite · titanium 3d printing ‎Review by TSI-Merkley · ‎$45.00 · ‎In stock

    ReplyDelete