Tuesday 8 August 2017

Exporting Data to an Excel Using X++ in Dynamics 365 for Operations

 Here In D365 we don't have SysExcelApplication, we can use OfficeOpenXml Namespace to do this.  

    using System.IO;
    using OfficeOpenXml;
    using OfficeOpenXml.Style;
    using OfficeOpenXml.Table;
    class CustExport
    {
             public static void main(Args   _args)
        {
       
            CustTable custTable;
            MemoryStream memoryStream = new MemoryStream();

            using (var package = new ExcelPackage(memoryStream))
            {
                var currentRow = 1;

                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 = "Account Number";
                cell.set_Value(value);
                cell = null;
                value = "Currency";
                cell = cells.get_Item(currentRow, 2);
                cell.set_Value(value);

                while select CustTable
                {
                    currentRow ++;
                    cell = null;

                    cell = cells.get_Item(currentRow, 1);
                    cell.set_Value(CustTable.AccountNum);
                    cell = null;

                    cell = cells.get_Item(currentRow, 2);
                    cell.set_Value(CustTable.Currency);
                }
                package.Save();
                file::SendFileToUser(memoryStream, "Test");
          
            }
      
        }

}

1 comment:

  1. Hi Mahesh;
    When I try using OfficeOpenXml, I am getting this error message "The type or namespace name 'OfficeOpenXml' could not be found (are you missing a using directive or an assembly reference?)"

    While writing the code, I am able to use and reference OfficeOpenXml - but the build gives this message. Any ideas?

    I am on the latest VM package - Nov 2017 release - that has Visual Studio 2015 in it.

    ReplyDelete