Blank Cell with Celltype: String is throwing FormatException trying to parse as an int with NPOI

39 views Asked by At

I have been getting such a bizarre bug in my C# application. I am going through an xlsx file, and saving it to a List<List>, each string representing a cell. Inexplicably, some but not most blank cells throw an exception:

System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at NPOI.XSSF.UserModel.XSSFCell.get_RichStringCellValue() in D:\github\npoi\ooxml\XSSF\UserModel\XSSFCell.cs:line 427
   at NPOI.XSSF.UserModel.XSSFCell.get_StringCellValue() in D:\github\npoi\ooxml\XSSF\UserModel\XSSFCell.cs:line 379
   at PEDYN_Replacement_1.LxlsxFull.GetSheet(String sheetString) in C:\Users\levi.applebaum\source\repos\PEDYN_Replacement_1\LxlsxFull.cs:line 85

On these problem cells, they still are of CellType String, but the exception seems to be about parsing an int. I have no idea what's going on, but any help you could provide would be greatly appreciated!

The code block I am using for each cell is:

try {
    switch (ic.CellType) {
        case NPOI.SS.UserModel.CellType.String:
            numStrings++;
            if (!string.IsNullOrEmpty(ic.StringCellValue.ToString())) tempList.Add(ic.StringCellValue.ToString());
            else tempList.Add("");
            break;
        case NPOI.SS.UserModel.CellType.Boolean:
            numBools++;
            tempList.Add(ic.BooleanCellValue.ToString().ToUpper());
            break;
        case NPOI.SS.UserModel.CellType.Numeric:
            numDoubles++;
            if (DateUtil.IsCellDateFormatted(ic)) {
                tempList.Add(
                    ic.DateCellValue.ToString(
                        ic.CellStyle.GetDataFormatString().Replace('m', 'M')));
            }
            else {
                tempList.Add(ic.NumericCellValue.ToString());
            }
            break;
        case NPOI.SS.UserModel.CellType.Error:
            numErrors++;
            tempList.Add("");
            break;
        case NPOI.SS.UserModel.CellType.Blank:
            numBlanks++;
            tempList.Add("");
            break;
        case NPOI.SS.UserModel.CellType.Unknown:
            numUnknown++;
            tempList.Add("");
            break;
        default:
            tempList.Add("");
            break;
    }
} catch(Exception exc) {
    Logn(EMP("Error in XLSX. Cell: " + B(ic.RowIndex +
        "," + ic.ColumnIndex) +
        " Type: " + ic.CellType));
    Logn(exc.ToString());
    numExceptions++;
}
0

There are 0 answers