mirror of https://github.com/apache/poi.git
Avoid short wrapping on cell styles and formats > 32,767 in XSSF - format supports up to 64,000 of them #57880
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1677371 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6a0cc1587b
commit
fac8191235
|
@ -603,6 +603,9 @@ public class XSSFCellStyle implements CellStyle {
|
|||
public short getIndex() {
|
||||
return (short)this._cellXfId;
|
||||
}
|
||||
protected int getUIndex() {
|
||||
return this._cellXfId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the color to use for the left border
|
||||
|
@ -974,6 +977,15 @@ public class XSSFCellStyle implements CellStyle {
|
|||
* @param fmt the index of a data format
|
||||
*/
|
||||
public void setDataFormat(short fmt) {
|
||||
// XSSF supports >32,767 formats
|
||||
setDataFormat(fmt&0xffff);
|
||||
}
|
||||
/**
|
||||
* Set the index of a data format
|
||||
*
|
||||
* @param fmt the index of a data format
|
||||
*/
|
||||
public void setDataFormat(int fmt) {
|
||||
_cellXf.setApplyNumberFormat(true);
|
||||
_cellXf.setNumFmtId(fmt);
|
||||
}
|
||||
|
|
|
@ -51,6 +51,14 @@ public class XSSFDataFormat implements DataFormat {
|
|||
* @return string represented at index of format or null if there is not a format at that index
|
||||
*/
|
||||
public String getFormat(short index) {
|
||||
return getFormat(index&0xffff);
|
||||
}
|
||||
/**
|
||||
* get the format string that matches the given format index
|
||||
* @param index of a format
|
||||
* @return string represented at index of format or null if there is not a format at that index
|
||||
*/
|
||||
public String getFormat(int index) {
|
||||
String fmt = stylesSource.getNumberFormatAt(index);
|
||||
if(fmt == null) fmt = BuiltinFormats.getBuiltinFormat(index);
|
||||
return fmt;
|
||||
|
|
|
@ -843,6 +843,15 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
|||
*/
|
||||
@Override
|
||||
public XSSFCellStyle getCellStyleAt(short idx) {
|
||||
return getCellStyleAt(idx&0xffff);
|
||||
}
|
||||
/**
|
||||
* Get the cell style object at the given index
|
||||
*
|
||||
* @param idx index within the set of styles
|
||||
* @return XSSFCellStyle object at the index
|
||||
*/
|
||||
public XSSFCellStyle getCellStyleAt(int idx) {
|
||||
return stylesSource.getStyleAt(idx);
|
||||
}
|
||||
|
||||
|
|
|
@ -2439,4 +2439,39 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
|
|||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* .xlsx supports 64000 cell styles, the style indexes after
|
||||
* 32,767 must not be -32,768, then -32,767, -32,766
|
||||
*/
|
||||
@Test
|
||||
public void bug57880() throws Exception {
|
||||
int numStyles = 33000;
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
XSSFSheet s = wb.createSheet("TestSheet");
|
||||
XSSFDataFormat fmt = wb.getCreationHelper().createDataFormat();
|
||||
for (int i=1; i<numStyles; i++) {
|
||||
short df = fmt.getFormat("test"+i);
|
||||
// Format indexes will be wrapped beyond 32,676
|
||||
assertEquals(164+i, df&0xffff);
|
||||
// Create a style and use it
|
||||
XSSFCellStyle style = wb.createCellStyle();
|
||||
assertEquals(i, style.getUIndex());
|
||||
style.setDataFormat(df);
|
||||
XSSFCell c = s.createRow(i).createCell(0, Cell.CELL_TYPE_NUMERIC);
|
||||
c.setCellStyle(style);
|
||||
c.setCellValue(i);
|
||||
}
|
||||
|
||||
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
|
||||
fmt = wb.getCreationHelper().createDataFormat();
|
||||
s = wb.getSheetAt(0);
|
||||
for (int i=1; i<numStyles; i++) {
|
||||
XSSFCellStyle style = wb.getCellStyleAt((short)i);
|
||||
assertNotNull(style);
|
||||
assertEquals(i, style.getUIndex());
|
||||
assertEquals(164+i, style.getDataFormat()&0xffff);
|
||||
assertEquals("test"+i, style.getDataFormatString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue