Excel限制了您可以使用的不同单元格样式的数量,而且这个数字令人惊讶地低.对于刚接触POI的人们来说,一个普遍的问题是,他们跳过了有关工作簿范围内的单元格样式的知识,而是为每个单元格创建一个单元格样式.这很快使它们超过了Excel中的限制…
您以前编写代码的地方可能看起来像
Sheet s = wb.createSheet();
for (int rn=0; rn<=10; rn++) {
Row r = s.createRow(rn);
for (int cn=0; cn<=4; cn++) {
Cell c = r.createCell(c);
c.setCellValue( getMyCellValue(rn,cn) );
CellStyle cs = wb.createCellStyle();
cs.setBold(true);
if (cn == 2) {
cs.setDataFormat( DataFormat.getFormat(yyyy/mm/dd) );
}
c.setCellStyle(cs);
}
}
相反,您需要将单元格样式创建从头开始,例如
CellStyle bold = wb.createCellStyle();
bold.setBold(true);
CellStyle boldDate = wb.createCellStyle();
boldDate.setBold(true);
boldDate.setDataFormat( DataFormat.getFormat(yyyy/mm/dd) );
Sheet s = wb.createSheet();
for (int rn=0; rn<=10; rn++) {
Row r = s.createRow(rn);
for (int cn=0; cn<=4; cn++) {
Cell c = r.createCell(c);
c.setCellValue( getMyCellValue(rn,cn) );
CellStyle cs = bold;
if (cn == 2) {
cs = boldDate;
}
c.setCellStyle(cs);
}
}