Bojensen Blogs

Export to Excel with X++ code ~ IT Pro Ramblings

Every user of Microsoft Dynamics AX knows the possibility and the ease-of-use to copy-paste data from a Dynamics AX grid to a Microsoft Excel worksheet.

However, as Dynamics AX developer, you can achieve the same through X++ code, with a lot of more possibilities. In this post, I show an example of such an export to Microsoft Excel. I am going to export the list of my customers to a first worksheet in my Excel file. For each customer, I’ll add a seperate worksheet through X++ code and I’ll export in that particular worksheet an overview of backorder lines with their quantity that has not yet been packing slip delivered.

X++ code

Open the Application Object Tree (AOT) and create a new Job.

Copy-Paste underneath code and run the Job. The Job will create a Microsoft Excel workbook in the directory C:WindowsTemp.

The result should look like this if you execute the job on the demo data in Microsoft Dynamics AX 2009 with Microsoft Excel 2007:

This is the first worksheet in my Excel workbook with the complete list of my customers in it.

This is one of the other worksheets, being the one created for customer Forest Wholesales and showing all the backorder lines for this particular customer.

Remarks about some code snippets
  • If set to false, Microsoft Excel will be opened in the background of your desktop.

  • sysExcelWorkSheetToBeDeleted is set to the second worksheet in your Excel workbook.

  • sysExcelWorksheet is renamed to Customers.

  • If set to true, a pop-up window (A file named ExportToExcel.xlsx already exists in this location. Do you want to replace it?) will appear if an Excel file with the same name already exists at the save location. This can be very annoying if you want to put this X++ code in a class and you want to run this class in batch at night. If set to true, your batch will wait until somebody gives a yes to allow replacement of the existing Excel file. If set to false, an existing Excel file will be overwritten.

  • Don’t forget this line of X++ code, or there will remain an Excel process running. This is important in the case where you did run Microsoft Excel in the background of your desktop. If you forget this line of X++ code and you run Excel in the background, you’ll have multiple Microsoft Excel processes after running this job a couple of times (you can check this by opening Task Manager and having a look at the Processes tab).

  • Export to Excel with X++ code ~ IT Pro Ramblings

    Comments are closed.