NPOI不评估3d公式

时间:2012-11-24 18:24:52

标签: npoi

我正在使用NPOI从Excel 2003文件中读取数据。这些文件包含这样的公式 SUM('1:2'!$ C $ 17)。 NPOI认可了这样的公式,如 SUM('1'!$ C $ 17)(没有表2)并评估无效结果。 我正在使用NPOI示例中的常规代码,例如

    foreach (var row in docRows)
            {
                sb.AppendFormat("{0}\t", SomeCode);
                rowCounter += 1;
                sb.AppendFormat("{0}\t", rowCounter);
                foreach (var col in docColumns)
                {
                    ICell cell = sheet.GetRow(row).GetCell(col);

                    sb.AppendFormat("{0}\t", GetExcelCellValue(cell));
                }
                sb.AppendLine();
            }
private string GetExcelCellValue(ICell cell)
        {
            string cellValue = string.Empty;
            IFormulaEvaluator evaluator = _hssfworkbook.GetCreationHelper().CreateFormulaEvaluator();
            evaluator.Evaluate(cell);

            switch (evaluator.EvaluateInCell(cell).CellType)
            {
                case CellType.BLANK:
                    cellValue = string.Empty;
                    break;
                case CellType.BOOLEAN:
                    cellValue = string.Empty;
                    break;
                case CellType.NUMERIC:
                    cellValue = Convert.ToString(cell.NumericCellValue);    //This is a trick to get the correct value of the cell. NumericCellValue will return a numeric value no matter the cell value is a date or a number.
                    break;
                case CellType.STRING:
                    throw new ArgumentNullException();
                    cellValue = cell.StringCellValue;
                    break;
                case CellType.ERROR:
                    cellValue = string.Empty;
                    break;
                case CellType.FORMULA:

                    break;

            }
            return cellValue;
        }

1 个答案:

答案 0 :(得分:0)

我刚遇到这个问题,我通过

解决了这个问题
switch (cell.CellType)
                {
                    case CellType.Blank:
                        cellValue = "";
                        break;
                    case CellType.Boolean:
                        cellValue = cell.BooleanCellValue.ToString();
                        break;
                    case CellType.Formula:

                        cellValue = cell.NumericCellValue.ToString();
                        break;
                    case CellType.Numeric:
                        cellValue = cell.NumericCellValue.ToString();
                        break;
                    case CellType.String:
                        cellValue = cell.StringCellValue;
                        break;
                }