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!
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:
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:
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.
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.
Turns out the DateUtil.IsCellDateFormatted() can be used as such:
case CellType.Numeric:
{
return DateUtil.IsCellDateFormatted(cell)
? cell.DateCellValue.ToString()
: cell.NumericCellValue.ToString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With