Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.
Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

How to Create and Export Excel Files using X++ in D365 FO

0

In this article, I am showing how to create and export Microsoft Excel files using X++ programming in D365 F&O. The management and integration of Microsoft Dynamics 365 with Microsoft 365 products or Office 365 products are very good, especially with Microsoft Excel. 

There are many situations in our development, where we must write the x++ codes to generate excel files in a specific format. These requirements may be special and do not available with the standard integration.

For this reason, I have written this article to get the basic coding concept to create an excel file and export the excel file using X++ coding.

If you are a beginner or new to dynamics 365 Finance & Operations, you can use Microsoft's Free Virtual Machine. Learn How to Set Up a Free Virtual Machine for Dynamics 365 Development

For this, I have created a Runnable Class (Jobs) with the name ExcelCreateAndExport,

Create and Export excel files in d365 using xpp d365snippets

After creating the runnable class(Job) copy and paste the below code, here I have written static and dynamic codes for adding records to the excel file.

class ExcelCreateAndExport

{       

public static void main(Args _args)

{

CustTable custTable;

DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName","xlsx", null, "excel create and export");

if (saveResult&& saveResult.parmAction() != DocuFileSaveAction::Cancel)

{

saveResult.parmOpenParameters('web=1');

saveResult.parmOpenInNewWindow(false);

System.IO.Stream workbookStream = new System.IO.MemoryStream();

System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

using(var package = new OfficeOpenXml.ExcelPackage(memoryStream))

{

var worksheets = package.get_Workbook().get_Worksheets();

var worksheet = worksheets.Add("Sheet1");

var cells = worksheet.get_Cells();

var currentRow=1 ;

 /*-------HEADER PART -------*/

var cell = cells.get_Item(currentRow,1);

cell.set_Value("Customer Name");

cell=null;

cell = cells.get_Item(currentRow,2);

cell.set_Value("Customer Address");

/*-------HEADER PART END-------*/

/*-------RECORD 1-------*/

currentRow=2;

cell= cells.get_Item(currentRow, 1);

cell.set_Value("ABCD Trading");

cell= null;

cell= cells.get_Item(currentRow, 2);

cell.set_Value("ABCD Complex, P.O Box :xxxxxx, XYZ Street");

/*-------RECORD 1 END-------*/

/*-------RECORD 2-------*/

currentRow=3;

cell= cells.get_Item(currentRow, 1);

cell.set_Value("XYZ Trading");

cell = null;

cell = cells.get_Item(currentRow, 2);

cell.set_Value("XYZ Complex, P.O Box :xxxxxx, ABC Street");

/*-------RECORD 2 END-------*/

package.Save();

 }

memoryStream.Seek(0,System.IO.SeekOrigin::Begin);

//Download the file.

DocuFileSave::processSaveResult(memoryStream,saveResult);

}

}

}


After writing these codes, build the project, and set this Runnable Class as starting Object by right-clicking on the class name in Solution Explorer. Then run the project, you can see the output,

Create and Export excel files in d365 using xpp d365snippets2

Click on the Download button, generated excel file will be downloaded to the local download folder.

Create and Export excel files in d365 using xpp d365snippets2

Writing x++ code for generating and exporting excel files in d365 fo is not a hard task, but the complexity of coding will increase if we require complex formats and adding more functionalities using x++ code. I have written this article for d365 for beginners and learners. Keep coding and Happy coding with D365 Snippets

Some Useful Links

Post a Comment

0Comments
Post a Comment (0)