【问题标题】:NPOI not being able to evaluate formula when excel file is generated生成 excel 文件时 NPOI 无法评估公式
【发布时间】:2019-05-27 03:18:05
【问题描述】:

我使用 NPOI 库从数据表创建了 excel 文件。已创建 Excel 文件,但未计算公式。这是我第一次使用 NPOI lib。

经过大量谷歌搜索后,我发现了一些我在代码中用于评估公式的代码 sn-p,但仍然没有运气。

此例程使用随机数值和动态组合的公式填充数据表。我的数据表屏幕截图附在这里data table screen shot

public DataTable GetDataTable()
{
    string strSum = "", strColName, strImmediateOneUp = "", strImmediateTwoUp = "";

    int startsum = 0;
    int currow = 0;
    bool firstTimeSum = true;

    int NumRows = 6;
    int NumColumns = 5;

    DataTable dt = new DataTable();

    for (int col = 0; col < NumColumns; col++)
    {
        strColName = GenerateColumnText(col);
        DataColumn datacol = new DataColumn(strColName, typeof(object));
        dt.Columns.Add(datacol);
    }


    for (int row = 0; row < NumRows; row++)
    {
        dt.Rows.Add();

        for (int col = 0; col < NumColumns; col++)
        {
            if (row < 2)
            {
                dt.Rows[row][col] = Convert.ToInt32(new Random().Next(1, NumRows));
            }
            else
            {
                if (firstTimeSum)
                {
                    if (row - currow == 2)
                    {
                        currow = row;
                        startsum = 0;
                        firstTimeSum = false;
                    }
                    else
                    {
                        startsum = 1;
                    }
                }
                else
                {
                    if (row - currow == 3)
                    {
                        currow = row;
                        startsum = 0;
                    }
                }


                if (startsum == 0)
                {
                    strColName = GenerateColumnText(col);
                    strImmediateOneUp = strColName + ((row + 1) - 1).ToString();
                    strImmediateTwoUp = strColName + ((row + 1) - 2).ToString();
                    strSum = string.Format("=SUM({0}:{1})", strImmediateTwoUp, strImmediateOneUp);
                    dt.Rows[row][col] = strSum;
                }
                else
                {
                    dt.Rows[row][col] = Convert.ToInt32(new Random().Next(1, NumRows));
                }
            }

        }

        startsum = 1;
    }
    return dt;
}

此例程从我的数据表中生成 excel 文件,问题在于公式未计算。

public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
{
    int i = 0;
    int j = 0;
    int count = 0;
    ISheet sheet = null;
    IWorkbook workbook=null;
    double d;

    string fileName = @"d:\SpreadsheetLight_npoi.xlsx";

    FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
    if (fileName.IndexOf(".xlsx") > 0) // 2007
        workbook = new XSSFWorkbook();
    else if (fileName.IndexOf(".xls") > 0) // 2003
        workbook = new HSSFWorkbook();

    try
    {
        if (workbook != null)
        {
            sheet = workbook.CreateSheet(sheetName);
        }
        else
        {
            return -1;
        }

        if (isColumnWritten == true)
        {
            IRow row = sheet.CreateRow(0);
            for (j = 0; j < data.Columns.Count; ++j)
            {
                row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
            }
            count = 1;
        }
        else
        {
            count = 0;
        }

        for (i = 0; i < data.Rows.Count; ++i)
        {
            IRow row = sheet.CreateRow(count);
            for (j = 0; j < data.Columns.Count; ++j)
            {
                if (Double.TryParse(data.Rows[i][j].ToString(), out d))
                {
                    row.CreateCell(j).SetCellValue(d);
                    //row.CreateCell(j).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                }
                else
                {
                    row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString().Replace("=",string.Empty));
                    //row.CreateCell(j).SetCellFormula(data.Rows[i][j].ToString().Replace("=",string.Empty));
                }

            }
            ++count;
        }

        if (workbook is XSSFWorkbook)
        {
            XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
        }
        else
        {
            HSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
        }
        workbook.Write(fs);
        return count;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception: " + ex.Message);
        return -1;
    }
}

我以这种方式评估我的公式,但仍然不起作用。

if (workbook is XSSFWorkbook)
{
    XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
}
else
{
    HSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
}

请告诉我在我的代码中添加或更改什么,因为结果公式应该被评估,并且当我打开 excel 文件时会显示正确的值。谢谢

【问题讨论】:

    标签: c# excel npoi spreadsheetlight


    【解决方案1】:

    我已经解决了我的问题。这是代码更正的代码。

    for (i = 0; i < data.Rows.Count; ++i)
    {
        IRow row = sheet.CreateRow(count);
        for (j = 0; j < data.Columns.Count; ++j)
        {
        if (Double.TryParse(data.Rows[i][j].ToString(), out d))
        {
            row.CreateCell(j).SetCellValue(d);
        }
        else
        {
           ICell cell = row.CreateCell(j);
            cell.SetCellType(CellType.Formula);
            cell.SetCellFormula(data.Rows[i][j].ToString().Replace("=", string.Empty));
        }
    
        }
        ++count;
    }
    
    if (workbook is XSSFWorkbook)
    {
        XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
    }
    else
    {
        HSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
    }
    workbook.Write(fs);
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      相关资源
      最近更新 更多