我已经使用 NPOI库从数据表中创建了excel文件。 Excel文件已创建,但未评估公式。这是我第一次使用NPOI lib。
经过大量的Google搜索,我发现我在代码中用来评估公式的代码片段很少,但还是没有运气。
此例程使用随机数值和动态组成的公式填充数据表。我的数据表屏幕快照位于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文件时显示正确的值。谢谢
答案 0 :(得分: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);
}
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);