We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ImportDataTable lost sorting


I have a function that exports a DataTable (that powers a GridView on a .Net page) to Excel using Aspose.Cells. However, if you sort the GridView (and therefore the DataTable as well), then export to Excel using the ImportDataTable method, the sorting is lost.

The code sample is below (irrelevant portions of the function are omitted - this is the part that deals with the data table). We use a lot of private methods here to encapsulate our work, but I can assure you that using breakpoints, I have verified that when the DataTable dt hits ImportDataTable, the sorting from the webpage is intact (in DefaultView.Sort). I hope this will be enough, but if you need any other portions of the method, please let me know. Thanks in advance!

dt = GetGridData(tableName:=PerformanceCalculatorProvider.DefaultTables.PerformanceTable, forExport:=True)

If dt.Rows.Count > 0 Then

exportSheet.Cells.ImportDataTable(dt, _

isFieldNameShown:=False, _

firstRow:=startRow, _

firstColumn:=0, _

insertRows:=False, _


exportSheet.AutoFitRow(startRow - 1, 0, iColCount)

End If


Could you create a sample application, zip it and post it here to show the issue, we will check it soon.

And, also tell us which version you are using of Aspose.Cells.

Thank you.

I assume we are on the latest version (still pretty new to my company, so I haven't gotten all the way into the code yet).

It's not really feasible to make a brand new project to demonstrate my problem. I guess I'm mostly trying to verify the functionality of the ImportDataTable function in the .NET version of Aspose.Cells.

Boiled down, what I'm getting at is, if I ImportDataTable, and the DataTable I'm importing has a sorting order, will that sort be maintained? I am not seeing that as being the case. If not, is there a quick way to perform the sort after it's imported? Speed is definitely a factor here.

Thanks again.


Well, I am afraid, if you sort a datatable before importing it to Excel worksheet using Cells.ImportDataTable() API, the method would import the original data and not the sorted data to the sheet. I think you may sort the data after you import it to the worksheet using Excel’s sorting feature. Please see the document on how you can sort data using Aspose.Cells API:

And, I have also added your feature request for the enhancement of ImportDataTable() method to log it into our issue tracking system with an id: CELLSNET-18517. We will support it in future versions, this will not be supported in near future though.

Thank you.

Hi Ando,

As only DataView support sorting not DataTable support sorting,
please use Cells.ImportDataView() method.

Please check the following codes:

Workbook workbook = new Workbook();

DataTable dt = new DataTable();


dt.Rows.Add(new object[] { 2 });

dt.Rows.Add(new object[] { 1 });

DataView view = dt.DefaultView;//.Sort();

view.Sort = "Test";



workbook.Worksheets[0].Cells.ImportDataView(view, 0, 0);


If you
still want to use Cells.ImportDataView, you have to convert the original
data table
to a sorted datatable programmatically.

That did the trick. The export now displays the data sorted correctly. Thanks, Salman!