Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

How can I import data from Excel?

Posted on by Microsoft Employee

Hi guys

I would like ask you that how i can import data from excel to dynamics ax r3

*This post is locked for comments

  • Suggested answer
    Hossein.K Profile Picture
    Hossein.K 6,642 on at
    RE: How can I import data from Excel?

    Hi,

    If you use AX2012, you can use below code:

     SalesLine           _salesLine;
        InventDim           _inventDim;
        SalesTable          _salesTable;
        InventTable         inventTable;
        SalesId             salesId = salesTable.SalesId;
        ItemId              itemId;
        EcoResItemSizeName  inventSize;
        EcoResItemColorName inventColor;
        EcoResItemStyleName inventStyle;
        InventSiteId        inventSiteId;
        InventLocationId    inventLocationId;
        EcoResItemConfigurationName configId;
        SalesPrice          salesPrice;
        Qty             qty;
        InventDimId     inventDimIdTmp;
    
        SysExcelApplication             application;
        SysExcelWorkbooks               workbooks;
        SysExcelWorkbook                workbook;
        SysExcelWorksheets              worksheets;
        SysExcelWorksheet               worksheet;
        SysExcelCells                   cells;
        COMVariantType                  type;
        FilenameOpen                    filename;
        dialogField                     dialogFilename;
        Dialog                          dialog;
        Integer                         row = 1;
    
    
        str COMVariant2Str(COMVariant       _cv)
        {
            switch (_cv.variantType())
            {
                case (COMVariantType::VT_BSTR):
                    return _cv.bStr();
                case (COMVariantType::VT_R8):
                  //  return _cv.toString() ;
                    return _cv.bStr() ;
                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()));
            }
        }
         ;
         dialog              =   new Dialog('Import Vendor Details');
         dialogFilename      =   dialog.addField(ExtendedTypeStr("FilenameOpen"));
         dialog.filenameLookupTitle('Import from excel.');
         dialog.caption('Import From Excel');
         dialogFilename.value(filename);
         if(dialog.run())
         {
             filename            =   dialogFilename.value();
             application         =   SysExcelApplication::construct();
             //application.displayAlerts(false);
             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();
             try
             {
                // ttsbegin;
    
                 do
                 {
                     inventTable.clear();
                     _inventDim.clear();
                     _salesLine.clear();
    
    
                     row  ;
                     itemId           = cells.item(row, 1).value().bStr();
                     inventSize       = strreplace(cells.item(row, 2).value().bStr(),'\'','');
                     inventColor      = strreplace(cells.item(row, 3).value().bStr(),'\'','');
                     inventStyle      = strreplace(cells.item(row, 4).value().bStr(),'\'','');
                     configId         = cells.item(row, 5).value().bStr();
                     qty              = cells.item(row, 6).value().double();
                     inventSiteId     = cells.item(row, 7).value().bStr();
                     inventLocationId = cells.item(row, 8).value().bStr();
                     salesPrice       = cells.item(row, 9).value().double();
    
                     type = cells.item(row 1, 1).value().variantType();
    
                     _salesLine.initValue();
    
                     /* Init SalesLine from SalesTable*/
                     _salesLine.SalesId = salesId;
                     _salesLine.initFromSalesTable(salesTable);
    
                     /* Set SalesLine Item*/
                     _salesLine.ItemId = itemId;
    
    
                     //Initializing the sales line from inventory
                    _salesLine.initFromInventTable(InventTable::find(_salesLine.ItemId));
    
                    //Setting and creating inventory dimensions
                    //I have given the warehouse in
                    if(Inventlocation::find(inventLocationId).InventLocationId != "")
                    {
                        _inventDim.inventBatchId = "General";
                        _inventDim.InventSiteId = InventLocation::find(inventLocationId).InventSiteId;
                        _inventDim.InventLocationId = Inventlocation::find(inventLocationId).InventLocationId;
                        _inventDim.wMSLocationId = "In_01";
                        _inventDim.InventSizeId = inventSize;
                        _inventDim.InventStyleId = inventStyle;
                        _inventDim.InventColorId = inventColor;
                        _inventDim.InventStatusId = "normal";
                        _inventDim.ConfigId = configId;
                    }
                    _salesLine.InventDimId = InventdIm::findOrCreate(_inventDim).inventDimId;
                   // _salesLine.initFromProductDimensions(inventDim);
                    inventDimIdTmp = _salesLine.InventDimId;
                     /* Set Sales line value*/
                     _salesLine.SalesQty   = qty;
                    // _salesLine.SalesPrice = salesPrice;
                     /* End*/
    
                     /* Look for trade agreement if not found base selling price will be use*/
                     //_salesLine.setPriceDisc(inventDim);
                     /* End*/
    
                     /* Create SalesLine*/
                     //_salesLine.CreateLine(NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes );
                     _salesLine.createLine(NoYes::Yes, // Validate
                                          NoYes::Yes, // initFromSalesTable
                                          NoYes::Yes, // initFromInventTable
                                          NoYes::Yes, // calcInventQty
                                          NoYes::Yes, // searchMarkup
                                          NoYes::Yes ); //
    
                    _salesLine.SalesPrice = salesPrice;
                    _salesLine.LineAmount = _salesLine.calcLineAmount();
                    _salesLine.InventDimId = inventDimIdTmp;
                    _salesLine.update();
                 }
                while (type != COMVariantType::VT_EMPTY);
                workbooks.close();
                application.quit();
               // ttscommit;
    
                 SalesLine_ds.research();
                 InventDim_ds.research();
                 InventDim_ds.refresh();
    
             }
             catch
             {
                 try{
    
                    workbooks.close();
                    application.quit();
                     }
                 catch{
                     info('error in importing...');
                 }
             }
        }

  • ergun sahin Profile Picture
    ergun sahin 8,812 Super User 2024 Season 1 on at
    RE: How can I import data from Excel?

    Can you confirm you are using ax 2012? Sounds like 365fo error to me.

  • Dishanth Profile Picture
    Dishanth 90 on at
    RE: How can I import data from Excel?

    I tried with this code but there is error saying SysExcelWorkbook does not denote any class table.....

    how can i clear this

  • Okay Akdeniz Profile Picture
    Okay Akdeniz 110 on at
    RE: How can I import data from Excel?

    Hi Musaad,

    There are many methods to import data from excel into AX. 

    • DIXF
      • Create or use existing entity
      • Create an data source type. (Excel)
      • Create a mapping source and target
      • Process 
    • AIF Document Services. 
      • You can create an query.
      • Execute Tools\AIF Document Services Wizard
      • Create an Inbound Port in System Management module
      • Create an DocumentSource in Organization Management module
      • Full CIL
      • Open an Excel with Dynamics AX Excell add-in
      • Select Add Data and use
    • XPP Custom Code
      • There are many code samples on the internet.
      • But standard samples just shows import method.
      • You will be faced many problems when the excel crashes or another excel application run.
      • You may prefer import csv file method. Because TXT file import faster than excel.
    • Office Add-in VSTO
      • You can create an VSTO projects
      • You can add a ribbon bar on excel like Dynamics AX Excel add-in
      • Business Connecter or AIF Document Services can help to import.

    DIXF the best way to import Excel.

  • Ruben Heinrich Profile Picture
    Ruben Heinrich 631 on at
    RE: Import

    In general you would have to install the Micsosoft Office Plug In on the Client. Once installed you can then open your Excel and use the Dynamics AX Plug In which enables you to exchange all table data as well as some data sets. However you must be Aware of the interdependencies and Relations of the tables.

    Another way is, working with the Dynamics Import Export Framework, which has some preconfigured entitites. It also supports Format Excel.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Import

    Hi Musaad,

       You can import data by using Job and by using form buttons and so..

    You can you this code to import data form excel to ax. Try this code by using Job or Button(Clicked Method).

    Job:

    static void InsertRecords(Args _args)
    {
         SysExcelApplication application;
         SysExcelWorkbooks workbooks;
         SysExcelWorkbook workbook;
         SysExcelWorksheets worksheets;
         SysExcelWorksheet worksheet;
         SysExcelCells cells;
         COMVariantType type;
         Name name;
         FileName filename;
         int row =1;
         int i=0;
         InventTable         inventTableLoc;
         ItemId              itemid;
         TransDate           transdate;
         str                 NewField;
         Dialog dialog;
         DialogField dialogField;
         ;
         application = SysExcelApplication::construct();
         workbooks = application.workbooks();
         dialog = new Dialog("FileOpen");
         dialogfield = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
         dialog.run();

         if (dialog.run())
         {
            filename = (dialogfield.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
         {
             ttsBegin;
         row++;
         itemid    = cells.item(row, 1).value().bStr();
         NewField  = cells.item(row, 2).value().bStr();
         inventTableLoc = InventTable::find(itemid,true);
         inventTableLoc.NameAlias = NewField;
         inventTableLoc.insert();
             ttsCommit;
         i++;
         type = cells.item(row+1, 1).value().variantType();
         }
         while (type != COMVariantType::VT_EMPTY);
         application.quit();
         info("Done");
         info(strFmt("%1", i));
    }

    Thanks

    Suresh

  • Ludwig Reinhard Profile Picture
    Ludwig Reinhard Microsoft Employee on at
    RE: Import

    Hi Musaad,

    What data do you want to import from Excel to AX?

    In AX2012 you have the AIF and the DIXF available that can help you importing the data.

    What might fit best depends on what you try to import and do.

    Best regards,

    Ludwig

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans