X++ code to export the records to Excel Sheet from Table dynamically in D365 F&O
class BASExcelExport { /// <summary> /// Runs the class with the specified arguments. /// </summary> /// <param name = "_args">The specified arguments.</param> public static void main(Args _args) { BASEmployeeDetails employeeDetails; DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName","xlsx", null, "excel create and 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("Sheet1"); var cells = worksheet.get_Cells(); var currentRow=1 ; /*-------HEADER PART -------*/ var cell = cells.get_Item(currentRow,1); cell.set_Value("Employee ID"); cell=null; cell = cells.get_Item(currentRow,2); cell.set_Value("Employee Name"); /*-------HEADER PART END-------*/ /*-------Insert all the records-------*/ while select employeeDetails { currentRow ++; cell = null; cell = cells.get_Item(currentRow, 1); cell.set_Value(employeeDetails.EmpId); cell = null; cell = cells.get_Item(currentRow, 2); cell.set_Value(employeeDetails.EmpName); } package.Save(); } memoryStream.Seek(0,System.IO.SeekOrigin::Begin); //Download the file. DocuFileSave::processSaveResult(memoryStream,saveResult); } } }
Comments
Post a Comment