Using Aspose.Cells 8.3.2.1, I am seeing an index out of range exception when attempting to read data from a CSV file that has an empty column if I use the GetCellByIndex method. If I use the bracket operator it works, and curiously if I use the bracket operator first and then attempt to use GetCellByIndex the call works.
Hi Barry,
Thanks for your posting and using Aspose.Cells.
We were able to observe this issue after testing this issue with the following sample code using the latest version: Aspose.Cells
for .NET v8.3.2.5. GetCellByIndex throws exception on 6th iteration.
We have logged this issue in our database for investigation. We will look into it and fix this issue. Once, the issue is resolved or we have some other update for you, we will let you know asap.
This issue has been logged as
- CELLSNET-43433 - Reading CSV data with an empty column fails when using GetCellByIndex
C#
var fileName = @“empty_column.csv”;
var workbook = new Workbook(fileName, new LoadOptions(LoadFormat.CSV));
var sheet = workbook.Worksheets[0];
var headers = new List<string>();
for (int i = 0; i <= sheet.Cells.Rows[0].LastDataCell.Column; i++)
{
//This line throws exception on i=5
var header = sheet.Cells.Rows[0].GetCellByIndex(i).StringValue;
headers.Add(header);
}
Hi.
Please use one of the following solutions to iterate the cells in the row:
a) Row.GetEnumerator() method.
for (IEnumerator ie = workbook.Worksheets[0].Cells.Rows[0].GetEnumerator(); ie.MoveNext(); )
{
Console.WriteLine(((Cell)ie.Current).StringValue);
}
b) Row.GetCellOrNull() method.
for (int i = 0; i <= workbook.Worksheets[0].Cells.Rows[0].LastDataCell.Column; i++)
{
//This line throws exception on i=5
Cell cell = workbook.Worksheets[0].Cells.Rows[0].GetCellOrNull(i);
if (cell != null)
{
Console.WriteLine(cell.StringValue);
}
}
For the Row.GetCellByIndex method, we are
considering to remove it.
Thank you.