Friday, May 26, 2017

Import data from Excel to AX through X++

static void ImportExcelData(Args _args)
{
    SysExcelApplication             application;
    SysExcelWorkbooks               workbooks;
    SysExcelWorkbook                workbook;
    SysExcelWorksheets              worksheets;
    SysExcelWorksheet               worksheet;
    SysExcelCells                   cells;
    COMVariantType                  type;
    System.DateTime                 ShlefDate;
    FilenameOpen                    filename;

    dialogField                     dialogFilename;
    Dialog                          dialog;
    #AviFiles
    // Progress Bar
    SysOperationProgress progress = new SysOperationProgress(1, NoYes::Yes); 

    StyleTable                      styleTable;  // Table name
    str                             style;
    int                             row = 0;
    #Excel

    str COMVariant2Str(COMVariant _cv,
                       int _decimals = 0,
                       int _characters = 0,
                       int _separator1 = 0,
                       int _separator2 = 0)
    {
        switch(_cv.variantType())
        {
            case (COMVariantType::VT_BSTR):
                return _cv.bStr();

            case (COMVariantType::VT_R4):
                return num2str(_cv.float(),
                                _characters,
                                _decimals,
                                _separator1,
                                _separator2);

            case (COMVariantType::VT_R8):
                return num2str(_cv.double(),
                                _characters,
                                _decimals,
                                _separator1,
                                _separator2);

            case (COMVariantType::VT_DECIMAL):
                return num2str(_cv.decimal(),
                                _characters,
                                _decimals,
                                _separator1,
                                _separator2);

            case (COMVariantType::VT_DATE):
                return date2str(_cv.date(),
                                123,
                                2,
                                1,
                                2,
                                1,
                                4);

            case (COMVariantType::VT_EMPTY):
                return "";

            default:
                throw error(strfmt("@SYS26908",_cv.variantType()));
        }
        return "";
    }
    ;

    dialog = new Dialog("ExcelUpload");
    dialogFilename      =   dialog.addFieldValue(extendedTypeStr(FilenameOpen),filename);
    dialog.filenameLookupFilter(["@SYS28576",#XLS,"@SYS28576",#XLSX]);
    dialog.filenameLookupTitle("Upload from Excel");
    dialog.caption("Excel Upload");
    dialogFilename.value(filename);

    if(!dialog.run())
        return;

    filename            =   dialogFilename.value();
    application         =   SysExcelApplication::construct();
    workbooks           =   application.workbooks();

    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();

    // Progress Caption & Animation
    progress.setCaption("Copying..");
    progress.setAnimation(#AviUpdate);

    do
    {
        try
        {
            ttsbegin;
            row++;
            // Getting EquipmentId value form Excel Row wise and assigning as str
            style = COMVariant2Str(cells.item(row,1).value());         

        // While Revecing datas from Excel Sheet If invalid value presents it wil not allowe to insert and the
            //   last  record also deleted
            if (!equipId)
            {
                ttsbegin;

                _TestXls.delete();

                ttscommit;

                box::warning(strfmt("Check the value in Excel Sheet row %1", row));

                return;

            }

            else
            {
                if (row > 1)
                {
                    // Progress bar Text and Total
                    progress.setText(strfmt("Importing to Ax Table : %1", row));
                    progress.setTotal(row, 1);

                    _TestXls.clear();
                    _TestXls.Style    =  equipId;
                    _TestXls.insert();

                }
            }

            ttscommit;
        }

        catch
        {
            Error(strfmt("Upload Failed in row %1", row));
        }

        type = cells.item(row + 1, 1).value().variantType();

    } while (type!= COMVariantType::VT_EMPTY);

    info(strfmt(" Details Uploaded Successfully"));

    application.quit(); 

}

No comments:

Post a Comment

update_recordset with joins

 update_recordset with joins update_recordSet storeTransfer         setting      TransactionId = transfertable.TransferId     join transfert...