POI Excel合并导致“修复的记录:来自/xl/styles.xml部分(样式)”

6
我使用此处指定的代码合并了两个Excel文件。

http://www.coderanch.com/t/614715/Web-Services/java/merge-excel-files

这是应用于合并单元格的样式块

 if (styleMap != null)
{
  if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook())
  {
    newCell.setCellStyle(oldCell.getCellStyle());
  }
  else
  {
    int stHashCode = oldCell.getCellStyle().hashCode();
    XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
    if (newCellStyle == null)
    {
      newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
      newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
      styleMap.put(stHashCode, newCellStyle);
    }
    newCell.setCellStyle(newCellStyle);
  }
}

一切都按预期运行,生成我的XSSFWorkbook时进展顺利。
当我尝试打开它时出现问题:
我看到以下错误。

enter image description hereenter image description here

我的错误报告包含以下内容
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>error072840_01.xml</logFileName>
    <summary>Errors were detected in file 'XYZ.xlsx'</summary>
    <repairedRecords summary="Following is a list of repairs:">
        <repairedRecord>Repaired Records: Format from /xl/styles.xml part (Styles)</repairedRecord>
    </repairedRecords>
</recoveryLog>

经过这一切,我的表格打开得很好,但没有样式。我知道有一个创建样式数量的限制,并且已经计算了被创建的样式数量,我几乎只看到了4个被创建。我甚至知道这个问题是由于太多的样式引起的。

不幸的是,POI仅支持优化HSSFWorkbook(Apache POI删除工作簿中的CellStyle

如果有任何帮助来缓解这个问题将是很棒的。


你确定你正在使用最新版本吗?(写作时为3.11 beta 2) - Gagravarr
是的,我尝试使用了3.11 beta 2,但仍然遇到了同样的问题。之前我使用的是3.9版本。 - Shiv
3个回答

3

好的,经过调试POI代码以及样式应用方面的一些问题。

下面的操作解决了这个问题。

newCellStyle.getCoreXf().unsetBorderId();
      newCellStyle.getCoreXf().unsetFillId();

让我问你,这些更新的代码之前你有什么? - Osmar

2

我曾经遇到过同样的问题。

你应该尽量减少样式和字体的使用,因为每个实例都会被放入xl/styles.xml中。

为一个工作簿只创建一次样式和字体。


我没有多种风格,每本书只有一种。上述解决方案运作良好。 - Shiv
尽管Shiv的问题与工作簿上创建的样式总数无关,但这可能是其原因。对我而言,情况确实如此,您可以在工作簿中创建的样式最大上限约为50k至60k。 - Vivek

2
我使用Python库xlxswriter和Pandas时遇到了相同的问题。在停止尝试使用Pandas的date_format规范后,我停止了收到错误信息。
import pandas as pd

data = pd.read_excel('somefile.xlsx')
grp = data.groupby('Property Manager')

for i, (pm, g) in enumerate(grp):
    writer = pd.ExcelWriter(p + f.format(pm[:30]), engine='xlsxwriter') #,date_format='%m/%d/%Y')
    g[cols].to_excel(writer, sheet_name='Summary', index=False)
    writer.save()

1
我认为这不是同一个问题,只是类似的症状(这对于各种Excel错误非常普遍)。在pandas示例中,这可能是由日期格式'%m/%d/%Y'引起的,而这在Excel中无效。尝试使用'mm/dd/yy'之类的东西。 - jmcnamara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接