Tuesday, August 2, 2016

X++ code for Import data from excel using all datatypes in ax 2012

Hi Reader's

Today i come up with an important concept i.e how to import data from excel using all datatypes in ax 2012,
here is the code .
Keep Importing !!! :)

static void ExcelImportwithAllDataTypes(Args _args)
{
        SysExcelApplication application;
        SysExcelWorkbooks workbooks;
        SysExcelWorkbook workbook;
        SysExcelWorksheets worksheets;
        SysExcelWorksheet worksheet;
        SysExcelCells cells;
        SK_Excel sk_excel;
        COMVariantType type;
        Name name;
        FileName filename;
        int row;
        str path;
        str filetype;
        int i;
        NoYes   NoYes;
        FileIoPermission perm;
        DocuValue docuvalue;
        #define.FileMode('W')
        ;

    //specify the file path that you want to read
    filename = "D:\\ReadExcel"; //path of excel


        perm = new FileIOPermission(filename, #FileMode);
        perm.assert();
        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.setAnimation(#AviTransfer);
        try
        {
          
            do
            {
                row++;
                if (row >= 1)
                {
                    sk_excel.clear();
                    sk_excel.String             =       cells.item(row,1).value().bstr(); // string
                    sk_excel.Int64              =       any2int64(cells.item(row,2).value().double()); // int64
                    sk_excel.Integer            =       any2int(cells.item(row,3).value().double()); //int
                    sk_excel.Real               =       cells.item(row,4).value().double(); //real
                    sk_excel.Container          =       str2con( cells.item(row,5).value().bstr()); //container
                    sk_excel.Date               =       cells.item(row,6).value().date(); //date
                    sk_excel.DateTime                      =     DateTimeUtil::newDateTime(cells.item(row,7).value().date(),timeNow());// datetime
                    sk_excel.Guid               =       str2guid(cells.item(row,8).value().bstr()); //guid
                    sk_excel.Enum               =       str2enum(NoYes,cells.item(row,9).value().bstr());//enum
                    sk_excel.time               =       cells.item(row,10).value().time(); // time
                    sk_excel.insert();
                }
                type = cells.item(row+1, 1).value().variantType();
        }

        while (type != COMVariantType::VT_EMPTY);

        info('done');

    }
  catch(Exception::Error)

    {

        workbooks.close();

        CodeAccessPermission::revertAssert();
        application.quit();

        ttsabort;

    }

    workbooks.close();

    CodeAccessPermission::revertAssert();

    application.quit();

}


1 comment:

Export a copy of the standard user acceptance testing (UAT) database

 Reference link: Export a copy of the standard user acceptance testing (UAT) database - Finance & Operations | Dynamics 365 | Microsoft ...