Tuesday, 20 June 2017

Import the data from Excel to D365/Ax7

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;

class RunnableClass1
{      
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {  
        System.IO.Stream            stream;
        ExcelSpreadsheetName        sheeet;
        FileUploadBuild             fileUpload;
        DialogGroup                 dlgUploadGroup;
        FileUploadBuild             fileUploadBuild;
        FormBuildControl            formBuildControl;
        TableTest                          test;
        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;
                    //rowCount = 1;
                    //i=Range.Rows;

                    for (i = 2; i<= rowCount; i++)

                    {

                        test.AccountNum = range.get_Item(i, 1).value;

                        test.AccountName = range.get_Item(i, 2).value;
                        test.insert();

                    }

                }

            }

            else

            {

                error("Error ");

            }
            info("Done");

        }

       

    }

}

4 comments:

  1. Very useful information regarding xapta . Thanks to the author.. Keep it up your work this may help for dynamics xapta learners

    ReplyDelete
  2. Great Information about Excel. Very Nice Article. Not only Excel we Can Perform this type of Operation at any Microsoft Dynamics Tools.

    ReplyDelete
  3. Improving the Performance of tools ... Increasing Robotic Technology in software as well as reducing man power has became common now a days... Among it Microsoft is playing a major role which Provides Microsoft Dynamics CRM tools.

    ReplyDelete