Cell index must be >= 0
bkarakaya01 opened this issue · comments
var excelData = mapper.Take<CustomNpoiModel>(mapper.Workbook.GetSheetAt(0).SheetName).Select(x => x.Value).ToList();
I have used this line in 10 different "Actions" and every single approach this mapping works great, but in my last action I got this strange exception which is "Cell index must be >= 0" and the error does not provide any additional information!
Strangely, I have tried to use an Excel File which I've created last week, and when I use that File, I didn't get any errors,
then I simply copy that Excel file and create a new one, and copy those rows into the new Excel File, and yet I got the same error.
Additional Information :
public class CustomNpoiModel
{
[Column("No")]
public int No { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Path")]
public string Path { get; set; }
}
At least please send the InnerException.
Still need information on this!
Hi, if you can upload the file (with dummy values) and paste your code snippet, that will help me a lot, thanks!
Hello Mr.Donny,
The problem is that this issue is not reproducible.
You can download the simple excel from here basic-excel
Basically, as I mentioned above,
I have a model like below
public class ExcelService
{
public void ImportFromExcel(IFormFile excelFile)
{
var excelData = mapper.Take<CustomNpoiModel>(mapper.Workbook.GetSheetAt(0).SheetName)
.Select(x => x.Value).ToList();
excelData.ForEach(data => {
var product = new Product
{
No = data.No,
Name = data.Name,
Path = data.Path
};
_context.Products.Add(product);
_context.SaveChanges();
});
}
}
public class CustomNpoiModel
{
[Column("No")]
public int No { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Path")]
public string Path { get; set; }
}
var excelData = mapper.Take<CustomNpoiModel>(mapper.Workbook.GetSheetAt(0).SheetName).Select(x => x.Value).ToList();
I am simply importing the data from excel file, then I am updating the rows of the excel file everytime when I have to import new data, but sometimes I am getting this exception from this line.
And also the exception has no "InnerException"
hi, seems the file format is invalid, neither NPOI nor my local Office365 can open it.
But I will include more error details for any exception during import.
Thank you!
Hello Donny,
It seems that when you construct a mapper as Mapper mapper = new Mapper(stream)
, sometimes it creates mapper.FirstRowIndex
value is set to -1. So, when you explicitly set it to 0 as mapper.FirstRowIndex = 0
, the problem is solving.
I have created a new & small helper library for empty cells detection of a sheet and sets the headers & _firstRowIndex, I can share it with you if you want to.
ok, this property may mislead users to get the value after constructing a mapper. Actually it is initialized as -1 by design. The purpose of this property is to let user specify for the first row so the mapper can start to work from that row, even if there are non-blank rows above that index. for example you may have description rows at the beginning rows, and data rows just start after that.
public int FirstRowIndex { get; set; } = -1;
If you set it as zero explicitly, means there is no header row, the first row is data row already. if mapper see it's -1 then mapper will use default value sheet.FirstRowNum
.
private int GetFirstRowIndex(ISheet sheet) =>
FirstRowIndex >= 0 ? FirstRowIndex : sheet.FirstRowNum;
I don't see any issue with -1 as the default value, your exception may be related to the column index rather than the row index.
Or may already been fixed :)