public short getIndex() {
return (short)this._cellXfId;
}
+ protected int getUIndex() {
+ return this._cellXfId;
+ }
/**
* Get the color to use for the left border
* @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);
}
* @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;
*/
@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);
}
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());
+ }
+ }
}