Importing data from Excel through X++ code

  Sometimes we need to import data from Excel to Microsoft Dynamics 365 form  using x++ code.
Through this post can see how to import a data from excel to form


Public static class ImportLeaveRequestLine
{
    public static void main(Args _args)
    {
        System.IO.Stream stream;
        LeaveRequestHeader LeaveRequestHeader = _args.record();
        LeaveRequestLine   LeaveRequestLine;
        ExcelSpreadsheetName sheeet;
        FileUploadBuild fileUpload;
        DialogGroup dlgUploadGroup;
        FileUploadBuild fileUploadBuild;
        FormBuildControl formBuildControl;
        Dialog dialog = new Dialog("Import the leave details");
        ;
        dlgUploadGroup = dialog.addGroup("Select excel file");
        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 (OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(stream))
                {
                    int rowCount, i;
                    Package.Load(stream);
                    OfficeOpenXml.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;
                    try
                    {
                        for (i = 2; i<= rowCount; i++)
                        {
                            str startdatestr = range.get_Item(i, 1).value;
                            str enddatestr   = range.get_Item(i, 2).value;
                            str ReturnDatestr   = range.get_Item(i, 3).value;
                            LeaveRequestLine.clear();
                            LeaveRequestLine.initValue();
                            LeaveRequestLine.LeaveRequestID = LeaveRequestHeader.LeaveRequestID;
                            LeaveRequestLine.StartDate = str2Date(startdatestr,123);
                            LeaveRequestLine.modifiedField(fieldNum(LeaveRequestLine, StartDate));
                            LeaveRequestLine.EndDate = str2Date(enddatestr,123);
                            LeaveRequestLine.modifiedField(fieldNum(LeaveRequestLine, EndDate));
                            LeaveRequestLine.ReturnDate = str2Date(ReturnDatestr,123);
                            LeaveRequestLine.modifiedField(fieldNum(LeaveRequestLine, ReturnDate));
                            if (LeaveRequestLine.validateWrite() == true
                                &&  LeaveRequestHeader.LeaveType() == range.get_Item(i, 4).value
                                &&  LeaveRequestLine.validateField(fieldNum(LeaveRequestLine, StartDate)) == true
                                &&  LeaveRequestLine.validateField(fieldNum(LeaveRequestLine, EndDate))== true
                                &&  LeaveRequestLine.validateField(fieldNum(LeaveRequestLine, ReturnDate)) == true)
                            {
                                LeaveRequestLine.insert();
                            }
                        }
                        info("Data inserted succesfully");
                    }
                    catch (Exception::Error)
                    {
                        ttsabort;
                        throw error("error here");
                    }
                }
            }
        }
    }

}

Comments

Popular posts from this blog

SSRS Report using Controller , Contract and RDP classes in D365

Exporting data to Excel through X++ code

COC for Form level method

How to pass the parameter from one form to another in Dynamic365

Multi Select Lookup in SSRS Report in D365