This post is machine-translated. The original post in german language can be found here.

Dynamics AX: Import Excel file

Recently I was forced to deal with how to read data from an Excel file into AX. Therefore in the following a job with a kind of basic structure, how to solve something like this in X++.

static void importFromExcel(Args _args) 
{
    Filename                fileNameExcel = "C:\temp\file.xls";

    SysExcelApplication     sysExcelApplication;
    SysExcelWorkbooks       sysExcelWorkbooks;
    SysExcelWorksheets      sysExcelWorksheets;
    SysExcelWorksheet       sysExcelWorksheet;
    SysExcelRange           sysExcelRange;
    SysExcelCells           sysExcelCells;
    SysExcelWorkbooks       sysExcelWorkBooksCollection;
    str                     column_a;
    str                     column_b;
    str                     column_c;
    str                     column_d;
    int                     i = 0;
    #Excel
    ;

    sysExcelApplication = SysExcelApplication::construct();
    sysExcelWorkbooks   = sysExcelApplication.workbooks();

    sysExcelWorkbooks.open(fileNameExcel);

    sysExcelWorksheets  = sysExcelApplication.worksheets();
    sysExcelWorksheet   = sysExcelWorksheets.itemFromNum(1);
    sysExcelRange       = sysExcelWorksheet.rows();
    sysExcelCells       = sysExcelWorksheet.cells();

    try
    {
        ttsbegin;

        while (sysExcelCells.item(i+1, 1).value().variantType() != ComVariantType::VT_EMPTY)  // In der Annahme, dass die erste Spalte nicht leer ist         
        {
            i++;

            column_a = sysExcelCells.item(i, 1).value().bStr();
            column_b = sysExcelCells.item(i, 2).value().bStr();
            column_c = sysExcelCells.item(i, 3).value().bStr();
            column_d = sysExcelCells.item(i, 4).value().bStr();

            // ... do something ...
        }

        ttscommit;

        info("Finished");

        sysExcelApplication.quit();
        sysExcelApplication = null;
    }
    catch (Exception::Error)
    {
        sysExcelApplication.quit();
        sysExcelApplication = null;
    }
}
These post applies to following version:
Dynamics AX 2009

 
 

 

 
 
 
Posts of the actual month
November 2024
MoTuWeThFrSaSu
 123
45678910
11121314151617
18192021222324
252627282930 
 
© 2006-2024 Heinz Schweda | Imprint | Contact | German version | Mobile version
In order to provide you with better service, this site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.