Bug 56468 - Writing a workbook more than once corrupts the file
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1594721 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
599489e6fa
commit
d0320ad77d
@ -148,6 +148,9 @@ public class POIXMLProperties {
|
|||||||
xmlOptions.setSaveSuggestedPrefixes(map);
|
xmlOptions.setSaveSuggestedPrefixes(map);
|
||||||
|
|
||||||
OutputStream out = extPart.getOutputStream();
|
OutputStream out = extPart.getOutputStream();
|
||||||
|
if (extPart.getSize() > 0) {
|
||||||
|
extPart.clear();
|
||||||
|
}
|
||||||
ext.props.save(out, xmlOptions);
|
ext.props.save(out, xmlOptions);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
@ -599,7 +599,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
|
|||||||
throw new IllegalArgumentException("relationshipType");
|
throw new IllegalArgumentException("relationshipType");
|
||||||
ArrayList<PackagePart> retArr = new ArrayList<PackagePart>();
|
ArrayList<PackagePart> retArr = new ArrayList<PackagePart>();
|
||||||
for (PackageRelationship rel : getRelationshipsByType(relationshipType)) {
|
for (PackageRelationship rel : getRelationshipsByType(relationshipType)) {
|
||||||
retArr.add(getPart(rel));
|
PackagePart part = getPart(rel);
|
||||||
|
if (part != null) {
|
||||||
|
retArr.add(part);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return retArr;
|
return retArr;
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,6 @@ import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
|
|||||||
import org.apache.poi.openxml4j.opc.internal.PartMarshaller;
|
import org.apache.poi.openxml4j.opc.internal.PartMarshaller;
|
||||||
import org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager;
|
import org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager;
|
||||||
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
|
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
|
||||||
import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller;
|
|
||||||
import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
|
import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
|
||||||
import org.apache.poi.openxml4j.util.ZipEntrySource;
|
import org.apache.poi.openxml4j.util.ZipEntrySource;
|
||||||
import org.apache.poi.openxml4j.util.ZipFileZipEntrySource;
|
import org.apache.poi.openxml4j.util.ZipFileZipEntrySource;
|
||||||
@ -444,9 +443,8 @@ public final class ZipPackage extends Package {
|
|||||||
this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).size() == 0 ) {
|
this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).size() == 0 ) {
|
||||||
logger.log(POILogger.DEBUG,"Save core properties part");
|
logger.log(POILogger.DEBUG,"Save core properties part");
|
||||||
|
|
||||||
// We have to save the core properties part ...
|
// Add core properties to part list ...
|
||||||
new ZipPackagePropertiesMarshaller().marshall(
|
addPackagePart(this.packageProperties);
|
||||||
this.packageProperties, zos);
|
|
||||||
// ... and to add its relationship ...
|
// ... and to add its relationship ...
|
||||||
this.relationships.addRelationship(this.packageProperties
|
this.relationships.addRelationship(this.packageProperties
|
||||||
.getPartName().getURI(), TargetMode.INTERNAL,
|
.getPartName().getURI(), TargetMode.INTERNAL,
|
||||||
|
@ -321,13 +321,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Process the named ranges
|
// Process the named ranges
|
||||||
namedRanges = new ArrayList<XSSFName>();
|
reprocessNamedRanges();
|
||||||
if(workbook.isSetDefinedNames()) {
|
|
||||||
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
|
|
||||||
namedRanges.add(new XSSFName(ctName, this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (XmlException e) {
|
} catch (XmlException e) {
|
||||||
throw new POIXMLException(e);
|
throw new POIXMLException(e);
|
||||||
}
|
}
|
||||||
@ -1307,7 +1301,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
names.setDefinedNameArray(nr);
|
names.setDefinedNameArray(nr);
|
||||||
|
if(workbook.isSetDefinedNames()) {
|
||||||
|
workbook.unsetDefinedNames();
|
||||||
|
}
|
||||||
workbook.setDefinedNames(names);
|
workbook.setDefinedNames(names);
|
||||||
|
|
||||||
|
// Re-process the named ranges
|
||||||
|
reprocessNamedRanges();
|
||||||
} else {
|
} else {
|
||||||
if(workbook.isSetDefinedNames()) {
|
if(workbook.isSetDefinedNames()) {
|
||||||
workbook.unsetDefinedNames();
|
workbook.unsetDefinedNames();
|
||||||
@ -1315,6 +1315,15 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void reprocessNamedRanges() {
|
||||||
|
namedRanges = new ArrayList<XSSFName>();
|
||||||
|
if(workbook.isSetDefinedNames()) {
|
||||||
|
for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameList()) {
|
||||||
|
namedRanges.add(new XSSFName(ctName, this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void saveCalculationChain(){
|
private void saveCalculationChain(){
|
||||||
if(calcChain != null){
|
if(calcChain != null){
|
||||||
int count = calcChain.getCTCalcChain().sizeOfCArray();
|
int count = calcChain.getCTCalcChain().sizeOfCArray();
|
||||||
|
@ -17,10 +17,12 @@
|
|||||||
|
|
||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
@ -60,6 +62,7 @@ import org.apache.poi.ss.usermodel.Sheet;
|
|||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
import org.apache.poi.ss.util.AreaReference;
|
import org.apache.poi.ss.util.AreaReference;
|
||||||
|
import org.apache.poi.ss.util.CellRangeAddress;
|
||||||
import org.apache.poi.ss.util.CellReference;
|
import org.apache.poi.ss.util.CellReference;
|
||||||
import org.apache.poi.xssf.XSSFITestDataProvider;
|
import org.apache.poi.xssf.XSSFITestDataProvider;
|
||||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||||
@ -1464,4 +1467,23 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
|
|||||||
double rounded = cv.getNumberValue();
|
double rounded = cv.getNumberValue();
|
||||||
assertEquals(0.1, rounded, 0.0);
|
assertEquals(0.1, rounded, 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bug56468() throws Exception {
|
||||||
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
XSSFSheet sheet = wb.createSheet();
|
||||||
|
XSSFRow row = sheet.createRow(0);
|
||||||
|
XSSFCell cell = row.createCell(0);
|
||||||
|
cell.setCellValue("Hi");
|
||||||
|
sheet.setRepeatingRows(new CellRangeAddress(0, 0, 0, 0));
|
||||||
|
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(8096);
|
||||||
|
wb.write(bos);
|
||||||
|
byte firstSave[] = bos.toByteArray();
|
||||||
|
bos.reset();
|
||||||
|
wb.write(bos);
|
||||||
|
byte secondSave[] = bos.toByteArray();
|
||||||
|
|
||||||
|
assertThat(firstSave, equalTo(secondSave));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user