openxml sdk excel总和

时间:2018-02-07 16:13:16

标签: excel openxml openxml-sdk

我在打开xml sdk时遇到困难: 我正在尝试使用包含数字的多个列生成excel文件,并且我希望在结尾处有总和

我试图生成表定义部分内容,并在内部定义每个列(id,name etC)。如果TotalColumn的列为true,则会添加代码(粗略示例)

var column = new TableColumn{
id = 1, 
name = "example", 
TotalsRowFunction = TotalsRowFunctionValues.Sum,
TotalsRowFormula = new TotalsRowFormula("=SUBTOTAL(109;[" + rowName + "])")
};

我无法让它工作,当我打开excel时报告错误,但它没有明确说明问题是什么...我尝试使用微软验证器但是无法计算任何事情......

我感谢任何帮助/示例代码,因为我无法解决任何问题

编辑: 我最后用这个:

workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

2 个答案:

答案 0 :(得分:0)

为什么不使用细胞配方?

E.g。

cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.CellFormula = new CellFormula(string.Format("=SUBTOTAL({0};[{1}])", "109", rowName));

//This will force a full recalculation of all the cells
workbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
workbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

答案 1 :(得分:0)

我最终使用EPPlus,因为它似乎工作简单而有效