Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPOI - How to distinguish datetime-formatted numeric Excel-cells (c#)

Background

I have an Excel-file (xlxs) with a number of datetime and decimals that I want to convert into a two-dimensional string-array. The strings in the array should look exactly as the user has entered them into the excel. To do this I'm using NPOI version 2.2.1 with C#.

Note that column A in the example is formatted as a date in Excel!

COLUMN A     COLUMN B
2016-07-20   -46,95
2016-07-20   283,59
2016-07-20   -46,95
2016-07-20   52194,64

I have a generic method ConvertCellToString() that converts a cell into it's proper string representation:

    private string GetCellAsString(ICell cell)
    {
        switch (cell.CellType)
        {
            case CellType.Boolean:
                return cell.BooleanCellValue.ToString();
            case CellType.Error:
                return cell.ErrorCellValue.ToString();
            case CellType.Formula:
                return cell.CellFormula.ToString();
            //ALL CELLS IN THE EXEMPEL ARE NUMERIC AND GOES HERE
            case CellType.Numeric:
                return cell.NumericCellValue.ToString();
            case CellType.String:
                return cell.StringCellValue.ToString();
            case CellType.Blank:
            case CellType.Unknown:
            default:
                return "";
        }
    }

However, since even the datetime cells are numeric cells, this solution results in all the dates below being incorrectly represented as numbers rather than dates ("21672" instead of "2016-07-20" etc)

The method below needs to distinguish numeric cells with datetimes vs those with numbers. Is there any way of doing this with NPOI? I'd rather not resort to parsing the string, especially since NPIOs cell.ToString() returns the horrible "2016-jan-20" format if it's a date.

I'm pretty much stuck here so help would be greatly appreciated! There must be some obvious best practice for this that I just can't find!

like image 434
user1531921 Avatar asked Aug 10 '16 11:08

user1531921


People also ask

What is an NPOI Excel worksheet?

An NPOI Excel worksheet consists of rows, and rows contain cells. In other words, you need a row to work with its cells. The concept of column isn’t as important. In order to create rows and cells, you’ll need to create a worksheet first if you haven’t already:

How do you bold a cell in an NPOI workbook?

Using styles (bold, italic, etc.) in an NPOI workbook If you’d like make a cell bold, it isn’t as simple as cell.Bold = true. Instead, you must create a style, and then apply that style to a cell. In order to make cells bold, we can define the following style:

How to find the date/time value of a cell type?

The CellType enum does not have any item like DATE for date/time value, which are stored as number in Excel. So, there is no direct way to identity the cell is storing a date/time value. I think one way is to see its format by refering to ICell.CellStyle.DataFormat property or ICell.CellStyle.GetDataFormatString () function.

How to display numeric values in cells in Excel?

You can specify how to display numeric values in cells by applying number formats. For example, a number can appear in a cell as a percentage, decimal, currency, accounting, date or time value.


1 Answers

Turns out the DateUtil.IsCellDateFormatted() can be used as such:

            case CellType.Numeric:
                {
                    return DateUtil.IsCellDateFormatted(cell) 
                        ? cell.DateCellValue.ToString() 
                        : cell.NumericCellValue.ToString();
                }
like image 195
user1531921 Avatar answered Sep 18 '22 14:09

user1531921