--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (revision 1592771) +++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (working copy) @@ -1307,7 +1307,18 @@ i++; } names.setDefinedNameArray(nr); + if(workbook.isSetDefinedNames()) { + workbook.unsetDefinedNames(); + } workbook.setDefinedNames(names); + + // Re-process the named ranges + namedRanges = new ArrayList(); + if(workbook.isSetDefinedNames()) { + for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) { + namedRanges.add(new XSSFName(ctName, this)); + } + } } else { if(workbook.isSetDefinedNames()) { workbook.unsetDefinedNames(); --- src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultipleWrites.java (revision 0) +++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultipleWrites.java (working copy) @@ -0,0 +1,77 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ==================================================================== */ +package org.apache.poi.xssf.usermodel; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import junit.framework.TestCase; + +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.*; + +public class TestMultipleWrites extends TestCase +{ + // Assert that multiple writes of a workbook output the same result. + public void testMultipleWritesResultInSameWorkbookWritten() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); + XSSFRow row = sheet.createRow(0); + XSSFCell cell = row.createCell(0); + cell.setCellValue("Hi"); + + // Ensure an entry for the XSSFName collection so that is tested too. + sheet.setRepeatingRows(new CellRangeAddress(0, 0, 0, 0)); + + ByteArrayOutputStream baos = null; + ByteArrayOutputStream baos2 = null; + InputStream is = null; + InputStream is2 = null; + try { + baos = new ByteArrayOutputStream(8192); + wb.write(baos); + is = new ByteArrayInputStream(baos.toByteArray()); + + baos2 = new ByteArrayOutputStream(8192); + wb.write(baos2); + is2 = new ByteArrayInputStream(baos2.toByteArray()); + + // Files are the same size + assertEquals(baos.size(), baos2.size()); + + // Files are a binary match + int i1; + int i2; + while (((i1 = is.read()) != -1)&&((i2 = is2.read()) != -1)) { + assertEquals(i1, i2); + } + } + finally { + if(baos!=null) + baos.close(); + if(baos2!=null) + baos2.close(); + if(is!=null) + is.close(); + if(is2!=null) + is2.close(); + } + } +}