fixed XSSFWorkbook.createSheet to properly increment sheetId when sheetId sequence is not continious

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@768389 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-04-24 18:25:19 +00:00
parent 5ed977001d
commit a08f4d02c2
4 changed files with 19 additions and 2 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.5-beta6" date="2009-??-??">
<action dev="POI-DEVELOPERS" type="fix">47089 - Fixed XSSFWorkbook.createSheet to properly increment sheetId</action>
<action dev="POI-DEVELOPERS" type="fix">46568 - Fixed XSLFPowerPointExtractor to properly process line breaks</action>
<action dev="POI-DEVELOPERS" type="fix">39056 - Fixed POIFSFileSystem to set CLSID of root when constructing instances from InputStream</action>
<action dev="POI-DEVELOPERS" type="fix">47054 - Fixed cloneStyleFrom to avoid exception when cloning styles of the same family</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.5-beta6" date="2009-??-??">
<action dev="POI-DEVELOPERS" type="fix">47089 - Fixed XSSFWorkbook.createSheet to properly increment sheetId</action>
<action dev="POI-DEVELOPERS" type="fix">46568 - Fixed XSLFPowerPointExtractor to properly process line breaks</action>
<action dev="POI-DEVELOPERS" type="fix">39056 - Fixed POIFSFileSystem to set CLSID of root when constructing instances from InputStream</action>
<action dev="POI-DEVELOPERS" type="fix">47054 - Fixed cloneStyleFrom to avoid exception when cloning styles of the same family</action>

View File

@ -453,7 +453,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
CTSheet sheet = addSheet(sheetname);
int sheetNumber = getNumberOfSheets() + 1;
int sheetNumber = 1;
for(XSSFSheet sh : sheets) sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
wrapper.sheet = sheet;
sheet.setId(wrapper.getPackageRelationship().getId());

View File

@ -227,5 +227,18 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
assertEquals(2, st.getFills().size());
assertEquals(1, st.getBorders().size());
}
public void testIncrementSheetId() throws Exception {
XSSFWorkbook wb = getTestDataProvider().createWorkbook();
int sheetId = (int)wb.createSheet().sheet.getSheetId();
assertEquals(1, sheetId);
sheetId = (int)wb.createSheet().sheet.getSheetId();
assertEquals(2, sheetId);
//test file with gaps in the sheetId sequence
wb = getTestDataProvider().openSampleWorkbook("47089.xlsm");
int lastSheetId = (int)wb.getSheetAt(wb.getNumberOfSheets() - 1).sheet.getSheetId();
sheetId = (int)wb.createSheet().sheet.getSheetId();
assertEquals(lastSheetId+1, sheetId);
}
}