Importing a DataTable that has zero rows will produce an Index Error on workbook.Save(). We’re working around by checking and avoiding the ImportData call if the DataTable is empty. Thought you should know.
Which version of the APIs you are using? I did test your scenario/case using our latest version/fix: Aspose.Cells for .NET v22.7 (Download | NuGet) and it works fine. I am using the following sample code. Also, please find attached the output file for your reference:
e.g. Sample code:
Sorry I was wrong on what was causing the error. We have code that imports a datatable into a list object and lines up matching columns between the datatable and the list object. If the datatable was empty then our code would attempt to insert -1 rows using the InsertRows method below. After inserting the rows into the listobject we have to re-apply listObject column formulas because this doesn’t happen automatically using InsertRows. I’ve found that when applying both of these steps will result in the Index error on save. If I skip either the insert rows or re-applying the column formulas then there is no problem saving.
The only trouble is I can’t reproduce this with just the file and a test case. It only happens when we run our entire process and I haven’t been able to figure out the missing piece. I would say let it be for now since it was an edge condition to begin with and we’ll write back if we hone in on it further.
As always thank you for your support.
listObject.DataRange.Worksheet.Cells.InsertRows(listObject.StartRow + 1, numRows);
foreach (var column in listObject.ListColumns)
{
if (!string.IsNullOrEmpty(column.Formula))
{
column.Formula = column.Formula;
}
}
To avoid the exception, I guess you may simply evaluate if the underlying datatable is empty or not then decide which step to do and which to skip and write code accordingly. For example, see the line of code to evaluate if the datatable is empty or not:
e.g. Sample code:
......
int recordsCount = dt.Rows.Count;
//check if datatable is empty or not.
if(recordsCount>0)
{
//........
//your code goes here
//....
}
......
Sure, please take your time to evaluate it and share the sample to reproduce the issue. And, in the mean time you may workaround the issue accordingly.