Search your query

Thursday, February 9, 2017

X++ Code to create and write the Excel File in Dynamics 365 (Excel Export)


/// <summary>
/// Create and Download the Excel file.
/// </summary>

        public void createAndDownloadExcelFile()
        {
            DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName", "xlsx", null, "Testing excel export");
         
            if (saveResult && saveResult.parmAction() != DocuFileSaveAction::Cancel)
            {
                saveResult.parmOpenParameters('web=1');
                saveResult.parmOpenInNewWindow(false);

                System.IO.Stream workbookStream = new System.IO.MemoryStream();
             
             
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

                using (var package = new OfficeOpenXml.ExcelPackage(memoryStream))
                {
                    var worksheets = package.get_Workbook().get_Worksheets();
                    var worksheet = worksheets.Add("First sheet");
                    var cells = worksheet.get_Cells();
                    var cell = cells.get_Item(1,1);
                    cell.set_Value("MyColumnTitle");

                    package.Save();
                }

                memoryStream.Seek(0, System.IO.SeekOrigin::Begin);

                DocuFileSave::processSaveResult(memoryStream, saveResult); // Download the file.
            }
        }

5 comments:

  1. How to read the existing file from local and load in to D365 in excel

    ReplyDelete
  2. when writing the data in file we have required to merge the cells could you please help us with it

    ReplyDelete
  3. Is there any way to save this file into download folder without any other action from user such as select save/save and open?

    ReplyDelete