Wednesday, 2 January 2019

Import DATA from excel in D365FO

HI Guys,

Here is the sample code to import excel sheet in D365FO 

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

class DMSImportFromExcel
{        

    /// <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;
        Dialog                      dialog = new Dialog("Import the data from Excel");

        dlgUploadGroup          = dialog.addGroup("Group");
        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 DAXing :-)

Import General journal from excel in D365 F&O

 Hi Guys, Import General journal from excel in D365 F&O Code:  using System.IO; using OfficeOpenXml; using OfficeOpenXml.ExcelPackage; u...