Exporting records from master table(D365FO) to excel using X++ code
we are trying to export data from master table to Excel through the following job.
Create a job :-
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class BASCustInfoExportJob
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = “_args”>The specified arguments.</param>
public static void main(Args _args)
{
BAS_Employees employees;
MemoryStream memoryStream = new MemoryStream();
using (var package = new ExcelPackage(memoryStream))
{
var currentRow = 1;
Filename fileName = "Emp.xlsx";
var worksheets = package.get_Workbook().get_Worksheets();
var EmployeeTableWorksheet = worksheets.Add("Export");
var cells = EmployeeTableWorksheet.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 employees
{
currentRow ++;
cell = null;
cell = cells.get_Item(currentRow, 1);
cell.set_Value(employees.BAS_EmployeID);
cell = null;
cell = cells.get_Item(currentRow, 2);
cell.set_Value(employees.BAS_FirstName);
}
package.Save();
file::SendFileToUser(memoryStream, fileName);
}
}
}