Search code examples
c#excel-2007npoi

How to read unformatted contents of the numeric cells using NPOI?


I want to read an unformatted contents of the numeric cells (e.g. 0.05 instead of 5% and 123456 instead of 123,456.000).

I thought the easiest way to do it would be to change the format of the cell:

ICell cell = ...;
string s = cell.SetCellType(<ICell.CELL_TYPE_STRING-doesn't compile>).ToString();

but I do not know how to set string/numeric format.

All examples I have googled are either from POI or HSSF universes, they won't do for me (I am reading Excel 2007 spreadsheet using NPOI)


Solution

  • This worked for me:

    string formatProofCellReading(ICell cell)
    {
        if (cell == null)
        {
            return "";
        }
        if (cell.CellType == CellType.NUMERIC)
        {
            double d = cell.NumericCellValue;
            return (d.ToString());
        }
        return cell.ToString();
    }