Write to Excel (Create an Excel) using X++ Code (AX 7/D365 for Operations)

Hi all,

Below is the quick code snippet to export the data to excel/create an excel in AX 7/D365 for operations

Please note, in D365 we can achieve this through OfficeOpenXml namespace

Improvise it based on your requirement

using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.Style;

using OfficeOpenXml.Table;

class SRWriteToExcel

{

    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’);

           

        }

       

    }

 

}

Happy Dax6ng,

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: