khaled alomar
khaled alomar

Reputation: 683

Dynamics AX : error while importing data from excel on client

I did a custom X++ code to import data from Excel to General Ledger, however the import works very well on the server directly but while running it from the end user(client) it imports several records (like 24 records) then throw an error

the number of argument provided is different from the number of argument provided to the method

it is obvious that the error is related to connectivity issue since I have tried the same Excel file on the on the server and successfully imported.

in order to prevent this issue I was thinking to alternative solution rather than looping the through the excel file and do the business and insert the records, instead I thought it might be useful if I save the file directly/ bulk save in a table or something else then try to loop through the table to prevent the connectivity issue.

note: several solution are available on google such as windows ghosting but none works for me

could anyone please advise about that or suggest the suitable solution

Upvotes: 0

Views: 1797

Answers (1)

Jan B. Kjeldsen
Jan B. Kjeldsen

Reputation: 18051

I would recommend you save the Excel file as tab-separated text, then do the import using the TextIO class.

You will also benefit from a +10 times increase in performance!

static void ExcelTest(Args _args)
{
    #Excel
    FilePath excelFile = @'C:\Users\user\Documents\MyExcelFile.xlsx';
    FilePath textFile  = @'C:\Users\user\Documents\MyTextFile.txt';
    Microsoft.Office.Interop.Excel.Application  application = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbooks    workBooks   = application.get_Workbooks();
    Microsoft.Office.Interop.Excel.Workbook     workBook; 

    // Save the excel file as tab-separated text
    application.set_DisplayAlerts(false);
    application.set_Visible(false);
    new FileIOPermission(excelFile, 'r').assert();
    workBooks.Open(excelFile, 0, true, 5, '', '', true, #xlWindows, '', false, false, 1, false, false, 1);
    CodeAccessPermission::revertAssert();
    workBook  = workBooks.get_Item(1);
    new FileIOPermission(textFile, 'w').assert();
    CodeAccessPermission::revertAssert();
    workBook.SaveAs(textFile, #xlTextWindows, '', '', false, false, null, #xlLocalSessionChanges, false, null, null, false);
    workBooks.Close();
    application.Quit();

    // Now read the text file
    new FileIOPermission(textFile, 'r').assert();
    io = new TextIo(textFile, 'r');
    if (!io)
        throw error("@SYS18447");
    io.inFieldDelimiter('\t');
    for (con = io.read(); io.status() == IO_Status::Ok; con = io.read())
    {
        info(con2str(con));
    }
}

Upvotes: 2

Related Questions