web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Export to Excel Range

(0) ShareShare
ReportReport
Posted on by

I have been successfully exporting data to Excel using X++, but I am concerned about the performance. 

The data is being written to each cell one at a time, but I would like to switch to setting a range of values at once.

Does anyone know the proper syntax for this? I have tried a variety of possibilities, but I think that what I am missing is the ability to pass a 2D array to the proper Excel function. 

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;

class ExcelTest
{
public static void main(Args args)
{
System.IO.MemoryStream memoryStream = new MemoryStream();
var package = new ExcelPackage(memoryStream);
var worksheets = package.get_Workbook().get_Worksheets();
OfficeOpenXml.ExcelWorksheet Worksheet = worksheets.Add("Export");
OfficeOpenXml.ExcelRange cells = Worksheet.get_Cells();
OfficeOpenXml.ExcelRange cell;
OfficeOpenXml.ExcelRange range;
var currentRow = 1;
;

cell = cells.get_Item(currentRow, 1);
System.String value = "Title";
cell.set_Value(value); // This works
currentRow++;

// next part tries to send an array, but it doesn't work. Instead the cells are all filled with "System.String[]"

System.String[] strArray = new System.String[9]();
strArray.SetValue("Col1",0);
strArray.SetValue("Col2",1);
strArray.SetValue("Col3",2);
strArray.SetValue("Col4",3);
range = Cells.get_Item(strfmt("A%1:D%1",currentRow));
range.set_Value(strArray);
currentRow++;

package.Save();
file::SendFileToUser(memoryStream, 'Test.xlsx');
}

}

I believe the problem is that the range.setValue() function requires a 2D array (Excel RangeValueDataType), and I cannot figure out the correct syntax to pass. Alternatively if anyone can point to an example of Microsoft exporting in bulk in the standard code, that would be great.

*This post is locked for comments

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,053 Super User 2025 Season 2 on at

    Hi Tom,

    Microsoft is using SSIS packages for reading from and writing data to Excel in combination with Data import export framework. This is really fast.

  • Suggested answer
    Mea_ Profile Picture
    60,284 on at

    Hi Tom Zak2,

    Try LoadFromCollection() method on the range as proposed here stackoverflow.com/.../epplus-add-values-to-a-range-of-cells

  • Rustem Galiamov Profile Picture
    8,072 on at

    Hi Tom Zak2!

    Did you solve the issue?

  • Tom Zak2 Profile Picture
    on at

    So far, I have been unable to solve the issue with the proposed solution of using range.loadfromCollection() and code like:

    var vals = new string[] { "value1", "value2", "value3" };
    var rng = sheet.Cells["A1:A3"];
    
    rng.LoadFromCollection(vals);

    In X++ the LoadFromCollection() requires me to pass in a parameter of type "System.Collections.Generic.IEnumerable`1[T]Collection" and I have so far been unable to format that data to match that data type.

  • Mea_ Profile Picture
    60,284 on at

    Just declare any .Net collection, like array and use it. You can mix c sharp code with x++.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans