X++ code to export the records to Excel Sheet from Table dynamically in D365 F&O
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
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;
MemoryStream memoryStream = new MemoryStream();
using (var package = new ExcelPackage(memoryStream))
{
var currentRow = 1;
Filename fileName = 'Book1.Xlsx';
var worksheets = package.get_Workbook().get_Worksheets();
var CustTableWorksheet = worksheets.Add('Export');
var cells = CustTableWorksheet.get_Cells();
OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
System.String value = 'Employee ID';
cell.set_Value(value);
cell = null;
value = 'Employee Name';
cell = cells.get_Item(currentRow, 2);
cell.set_Value(value);
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();
file::SendFileToUser(memoryStream, fileName);
}
}
}
Comments
Post a Comment