简体   繁体   English

Apache POI Excel表-总计行

[英]Apache POI Excel Table-TotalsRow

While writing a tool to export result sets to Excel I've come across one problem. 在编写将结果集导出到Excel的工具时,我遇到了一个问题。 I am successfully creating tables with formatting and filters on the column headers, no problem there. 我成功创建了带有格式和列标题上的过滤器的表,那里没有问题。 The problem is I can't figure out how to make the total rows "work." 问题是我不知道如何使总行“有效”。 I want to use true total rows so they respond to the filters applied, but so far I can either get a row with subtotals that function but aren't a part of the table, or I can get a blank subtotal row. 我想使用真实的总行,以便它们对应用的过滤器做出响应,但是到目前为止,我可以得到具有功能但不属于表的小计的行,或者可以得到空白的小计行。

I believe there must be some magic, like a formula evaluator or something similar, but I have yet to stumble on it in the javadocs or example code. 我相信必须有一些魔术,例如公式求值器或类似的东西,但是我还没有在javadocs或示例代码中发现它。 I am using the code at this location with the following modifications. 我在此位置使用代码进行了以下修改。 Inside the loop that sets the column headers: 在设置列标题的循环内:

     if(i == 0)
         column.setTotalsRowLabel("Totals:");
     else
         column.setTotalsRowFunction(STTotalsRowFunctionImpl.COUNT);          

Then outside the loop: 然后在循环之外:

cttable.setTotalsRowShown(true);
cttable.setTotalsRowCount(1);

No luck, if I add a blank row for the totals, it is formatted as a part of the table, but no values show. 运气不好,如果我为总计添加空白行,则其格式将作为表的一部分,但不会显示任何值。 If I set a formula for any of the total cells the formula works, but Excel doesn't like the table and removes the total row, although the formula is there and works, just not as a total row. 如果我为所有总单元格设置一个公式,则该公式可以工作,但是Excel不喜欢该表并删除总行,尽管该公式存在并且可以工作,但不能作为总行。

When I look at the raw XML underneath, it is virtually indistinguishable from an Excel sheet save for the table, while the work sheet is considerably different. 当我看下面的原始XML时,它与表格的Excel工作表几乎没有区别,而工作表却大不相同。

UPDATE: I have been away from this project for quite a while, recently turned back to it. 更新:我离开这个项目已经有一段时间了,最​​近又回到了这个项目。 I've given up on POI doing this automatically and instead have turned to trying to backdoor it through DOM manipulation. 我放弃了自动执行此操作的POI,而是转向尝试通过DOM操作对其进行后门操作。

I am so close I can't give up. 我是如此亲密,我不能放弃。 This is all coming down to a namespace issue in the final worksheet. 所有这些都归结为最终工作表中的名称空间问题。 This code: 这段代码:

Element b = (Element) wb.getSheetAt(0).getCTWorksheet().getSheetData().getRowList().get(4).getCArray()[3].getDomNode();
Element f = b.getOwnerDocument().createElementNS("main", "f");
b.removeAttribute("t");
b.removeChild(b.getElementsByTagName("v").item(0));
f.appendChild(b.getOwnerDocument().createTextNode("SUBTOTAL(103,MYTABLE[Human])"));
b.appendChild(f);

produces the following in the sheet1.xml file: 在sheet1.xml文件中生成以下内容:

<c r="D5">
    <main:f>SUBTOTAL(103,MYTABLE[Human])</f>
</c>

If I use createElement("f"), I get: 如果使用createElement(“ f”),则会得到:

<c r="D5">
    <f xmlns="">SUBTOTAL(103,MYTABLE[Human])</f>
</c>

If I manually edit the sheet inside the archive and remove the namespace tag or qualifier, it works! 如果我手动编辑档案中的工作表并删除名称空间标签或限定符,那么它将起作用! I can't see how to solve the NS issue without saving the work book and then proceeding to open it up and fix the problems with file IO. 如果不保存工作簿,然后继续打开它并解决文件IO问题,我将看不到如何解决NS问题。 Does anyone have any hints on this at all? 有人对此有任何暗示吗?

If I use the correct full URI for the namespace, the workbook saves and all is well as long as I don't try to evaluate the formula with POI. 如果我对名称空间使用正确的完整URI,则只要不尝试使用POI评估公式,工作簿就会保存并且一切都很好。 I use the workbook setForceFormulaRecalculation on and it works when Excel is opened. 我使用工作簿setForceFormulaRecalculation并在打开Excel时可以使用。 It's still a bug in POI I believe, but I needed a quick fix and this does it for me. 我相信这仍然是POI中的错误,但是我需要快速修复,这对我来说确实有用。

Note: I realize this is a very old post, but just in case anyone in the future may be looking for a solution to this as I just encountered the same issue... 注意:我意识到这是一篇非常古老的文章,但以防万一将来有人遇到同样的问题时正在寻找解决方案...

You are on the right track, but I don't see where you are applying a formula to the cell in the actual total row. 您的路线正确,但我看不到要在实际总行中的单元格上应用公式的位置。 If you don't set this, the total row will be present but empty upon opening the file. 如果未设置,则总行将显示,但在打开文件时为空。

When creating the table 创建表格时

cttable.setTotalsRowShown(true);
cttable.setTotalsRowCount(1);
cttable.addNewAutoFilter();

When creating the columns 创建列时

column.setTotalsRowFunction(STTotalsRowFunctionImpl.COUNT); 

After populating all of the table data 填充所有表数据后

Row totalsRow = sheet.createRow(tableLastRow);
Cell totalsCell = totalsRow.createCell(lastColumnPosition);
totalsCell.setCellFormula("SUBTOTAL(103,MYTABLE[Human])");

You will have to populate the tableLastRow and lastColumnPosition for the values specific to your code. 您将必须填充tableLastRow和lastColumnPosition以获取特定于代码的值。 This works for me with Apache POI 3.17 and Excel 2016. 这对我适用于Apache POI 3.17和Excel 2016。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM