使用NPOI,是否可以通过Excel格式化单元格值(尤其是数字和日期值)的内置方式?
如果不是,最好的实施方法是什么?我想到了从Excel格式字符串到C#格式字符串的格式字符串转换器吗?
下面的示例假定Excel格式字符串和C#格式字符串相同。因此,它适用于一些基本格式字符串,例如:“#,## 0.00”
using NPOI.SS.UserModel;
ICell cell = workbook.GetSheet("table1").GetRow(0).GetCell(0);
string value = null;
if(cell.CellType == CellType.String) {
value = cell.StringCellValue;
} else if(cell.CellType == CellType.Numeric) {
string formatString = cell.CellStyle.GetDataFormatString();
if(DateUtil.IsCellDateFormatted(cell)) {
value = cell.DateCellValue.ToString(formatString);
} else {
value = cell.NumericCellValue.ToString(formatString);
}
} else [...]
发现可能建立的NPOI。但是,某些格式如“ 1983年9月18日,星期日”的求值方法类似于“ EEEE,1983年9月18日”。
using NPOI.SS.UserModel;
DataFormatter dataFormatter = new DataFormatter(CultureInfo.CurrentCulture);
ICell cell = workbook.GetSheet("table1").GetRow(0).GetCell(0);
string value = dataFormatter.FormatCellValue(cell);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句