浏览代码

Do not fail on different count of categories and points and fix cloning charts

We see many cases where documents have charts with a difference here, so
downgrade the exception to a warning for now

Also handle cloning charts in workbooks where the parts have unexpected names
by making sure that the chosen part-name is not yet taken.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894676 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_0
Dominik Stadler 2 年前
父节点
当前提交
7a5bc4227b

+ 5
- 1
poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChartData.java 查看文件

import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
*/ */
@Beta @Beta
public abstract class XDDFChartData { public abstract class XDDFChartData {
private static final Logger LOGGER = LogManager.getLogger(XDDFChartData.class);

protected XDDFChart parent; protected XDDFChart parent;
protected List<Series> series; protected List<Series> series;
private XDDFCategoryAxis categoryAxis; private XDDFCategoryAxis categoryAxis;
if (categoryData != null && values != null) { if (categoryData != null && values != null) {
int numOfPoints = category.getPointCount(); int numOfPoints = category.getPointCount();
if (numOfPoints != values.getPointCount()) { if (numOfPoints != values.getPointCount()) {
throw new IllegalStateException("Category and values must have the same point count, but had " +
LOGGER.warn("Category and values must have the same point count, but had " +
numOfPoints + " categories and " + values.getPointCount() + " values."); numOfPoints + " categories and " + values.getPointCount() + " values.");
} }
} }

+ 14
- 1
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java 查看文件

import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName; import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
protected RelationPart createChartRelationPart() { protected RelationPart createChartRelationPart() {
XSSFWorkbook wb = getSheet().getWorkbook(); XSSFWorkbook wb = getSheet().getWorkbook();
XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory(); XSSFFactory factory = wb == null ? XSSFFactory.getInstance() : wb.getXssfFactory();
int chartNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.CHART.getContentType())
OPCPackage pkg = getPackagePart().getPackage();
int chartNumber = pkg.getPartsByContentType(XSSFRelation.CHART.getContentType())
.size() + 1; .size() + 1;


// some broken files have incorrectly named package parts,
// so we need to avoid duplicates here by checking and increasing
// the part-number
try {
while (pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.CHART.getFileName(chartNumber))) != null) {
chartNumber++;
}
} catch (InvalidFormatException e) {
throw new IllegalStateException("Failed for " + chartNumber, e);
}

return createRelationship(XSSFRelation.CHART, factory, chartNumber, false); return createRelationship(XSSFRelation.CHART, factory, chartNumber, false);
} }



二进制
test-data/spreadsheet/clone_sheet.xlsx 查看文件


正在加载...
取消
保存