Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / What is the best way t...
Finance forum
Suggested answer

What is the best way to work with xls files (x++)

Posted on by 45

I want to work with ".xls" files, however EPPlus produces an error when I try to read the ".xls" file.

this my code:

        System.IO.Stream stream;
        ExcelSpreadsheetName sheeet;
        FileUploadBuild fileUpload;
        DialogGroup dlgUploadGroup;
        FileUploadBuild fileUploadBuild;
        FormBuildControl formBuildControl;
        Dialog dialog = new Dialog("Import the data from Excel");
        dlgUploadGroup          = dialog.addGroup("@SYS54759");
        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');
        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        fileUploadBuild.fileTypesAccepted('.xls');
 
        if (dialog.run() && dialog.closedOk())
        {
            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId('Upload'));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
 
            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();
                using (ExcelPackage Package = new ExcelPackage(stream))
                {
                    int rowCount, i;
                    Package.Load(stream);
                    ExcelWorksheet  worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);
                    OfficeOpenXml.ExcelRange    range       = worksheet.Cells;
                    rowCount                  = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row   1;
 
                    for (i = 2; i<= rowCount; i  )
                    {
                        info(range.get_Item(i, 1).value);
                        info(range.get_Item(i, 2).value);
                    }
                }
            }
            else
            {
                error("Error here");
            }

I'm wondering whether there is another approach to work with "xls" files.

I saw several code snippets that use SysExcelWorkbook, but they fail with the error "The name 'SysExcelApplication' does not denote a class, a table, or an extended data type."

        SysExcelApplication application;
        SysExcelWorkbooks workbooks;
        SysExcelWorkbook workbook;
        SysExcelWorksheets worksheets;
        SysExcelWorksheet worksheet;
        SysExcelCells cells;
        COMVariantType type;
        int row;
        ItemId itemid;
        Name name;
        FileName filename;
        ;
        application = SysExcelApplication::construct();
        workbooks = application.workbooks();
        //specify the file path that you want to read
        filename = @"C:\Users\hsaoudi\Downloads\crypresenceetcoutparemploye.xls";
        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
        {
            row  ;
            itemId = cells.item(row, 1).value().bStr();
            name = cells.item(row, 2).value().bStr();
            info(strfmt('%1 - %2', itemId, name));
            type = cells.item(row 1, 1).value().variantType();
        }
        while (type != COMVariantType::VT_EMPTY);
        application.quit();

  • Martin Dráb Profile Picture
    Martin Dráb 228,126 Most Valuable Professional on at
    RE: What is the best way to work with xls files (x++)

    There are some .NET APIs you could use. Check out How to convert xls file to xlsx file using C#?, for example.

  • Saoudi Hatem Profile Picture
    Saoudi Hatem 45 on at
    RE: What is the best way to work with xls files (x++)

    Yes that is what I want

  • Martin Dráb Profile Picture
    Martin Dráb 228,126 Most Valuable Professional on at
    RE: What is the best way to work with xls files (x++)

    Unfortunately I'm not sure what you need from me.

    My guess it that you:

    1) Insist on using EPPlus, despite the fact that it can't work with your files.

    2) You can't get files in the newer format.

    3) You want to do the conversion in code.

    Is that correct?

  • Saoudi Hatem Profile Picture
    Saoudi Hatem 45 on at
    RE: What is the best way to work with xls files (x++)

    goshoom no I don't find a way to.

    I would be eternally grateful if you could help me.

  • Martin Dráb Profile Picture
    Martin Dráb 228,126 Most Valuable Professional on at
    RE: What is the best way to work with xls files (x++)

    EPPLus is a .NET library for working with .xlsx files. It doesn't support the old format (.xls).

    Can't you convert your files to .xlsx?

  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,277 Super User 2024 Season 2 on at
    RE: What is the best way to work with xls files (x++)

    Hi Saoudi,

    Can you tell what EPPlus is and what error you get?

    The SysExcelApplication was an implementation used for AX2012. That was using the client application API which is not possible with a cloud solution. In Dynamics 365 F&O you can work with OpenOfficeXml to read Excel files.

    There are examples on the internet how to use it in Dynamics 365 like this post: Import records from Excel using X++ code in D365FO - Dynamics 365 Finance Community

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,277 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,126 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans