Read from Excel using X++ Code (AX7/ D365 for Operations)

Hi Friends,

Been long. Hope all is well. Work kept me busy.

Below is the code snippet which will be handy to read the data from excel using code in D365/ax 7

Please note that, in D365 SysExcel* classes have been deprecated.

Use OfficeOpenXML namespace to achieve this. Improvise the below snippet based on your need.

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.ExcelPackage;

using OfficeOpenXml.ExcelRange;

 

class SRReadFromExcel_D365

{       

    public static void main(Args _args)

    { 

        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(‘.xlsx’);

 

        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");

            }

 

        }

    }

 

}

Happy Dax6ng

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: