使用 NPOI,如何 return 单元格值,因为它已被 Excel 格式化?

Using NPOI, how do I return a cell value as it has been formatted by Excel?

使用NPOI, is there any buildin possibility to format a cell value (especially numeric and date values) as it has been formatted by Excel?

如果不是,最好的实施方式是什么?我想到了从 Excel-formatstrings 到 C#-formatstrings 的格式字符串转换器?

以下示例假定 Excel-formatstring 和 C#-formatstring 相同。所以它适用于一些基本的格式字符串,例如:“#,##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 的可能性。然而,某些格式如 "Sunday, September 18, 1983" 被评估为 "EEEE, September 18, 1983".

using NPOI.SS.UserModel;

DataFormatter dataFormatter = new DataFormatter(CultureInfo.CurrentCulture);
ICell cell = workbook.GetSheet("table1").GetRow(0).GetCell(0);

string value = dataFormatter.FormatCellValue(cell);
private static CellValue EvaluateFormulaCellValue(XSSFWorkbook wb, ICell cell)
        {
             WorkbookEvaluator _bookEvaluator = null;
            _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.Create(wb), null, null);
            // XSSFFormulaEvaluator.EvaluateAllFormulaCells(wb);
            ValueEval eval = _bookEvaluator.Evaluate(new XSSFEvaluationCell((XSSFCell)cell));
            if (eval is NumberEval)
            {
                NumberEval ne = (NumberEval)eval;
                return new NPOI.SS.UserModel.CellValue(ne.NumberValue);
            }
            if (eval is BoolEval)
            {
                BoolEval be = (BoolEval)eval;
                return NPOI.SS.UserModel.CellValue.ValueOf(be.BooleanValue);
            }
            if (eval is StringEval)
            {
                StringEval ne = (StringEval)eval;
                return new NPOI.SS.UserModel.CellValue(ne.StringValue);
            }
            if (eval is ErrorEval)
            {
                return NPOI.SS.UserModel.CellValue.GetError(((ErrorEval)eval).ErrorCode);
            }
            throw new InvalidOperationException("Unexpected eval class (" + eval.GetType().Name + ")");
        }