Friday, June 11, 2021

Create and export xlsx in D365 FO X++

 using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.Style;

using OfficeOpenXml.Table;

class ExportToExcel

{        

    public static void main(Args _args)

    {

        HcmWotker hcmWorker;

        MemoryStream memoryStream = new MemoryStream();

        using (var package = new ExcelPackage(memoryStream))

        {

            var currentRow = 1;

            Filename fileName = “Test.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 = “Personnel Number”;

            cell.set_Value(value);

            cell = null;

            value = “Name”;

            cell = cells.get_Item(currentRow, 2);

            cell.set_Value(value);

            while select hcmWorker

            {

                currentRow ++;

                cell = null;

                cell = cells.get_Item(currentRow, 1);

                cell.set_Value(hcmworker.PersonnelNumber);

                cell = null;

                cell = cells.get_Item(currentRow, 2);

                cell.set_Value(hcmworker.Name());

            }

            package.Save();

            file::SendFileToUser(memoryStream, fileName);           

        }

    }

}