尝试打开 excel 时使用 POI "too many different cell format" 样式
Style with POI "too many different cell format" when trying to open excel
我正在使用 POI 从 Java 生成一个 Excel 文件。 Excel 电子表格包含太多表格。当我生成文件并尝试使用 Excel 打开它时,出现此错误:"too many different cell formats".
这是我创建样式的方式:
private XSSFCellStyle crearEstilo(String formato, Color colorLetra, Color colorFondo, Double size, Boolean bold, String bordes, String alineacion) {
XSSFCellStyle estilo = wb.createCellStyle();
// Formato
if (formato != null) {
estilo.setDataFormat(wb.createDataFormat().getFormat(formato));
}
// Letra
XSSFFont letra = wb.createFont();
letra.setFontName("Arial");
if (size != null) {
letra.setFontHeight(size);
}
// Bold
if (bold != null) {
letra.setBold(bold);
}
// Color de letra
if (colorLetra != null) {
XSSFColor color = new XSSFColor(colorLetra);
letra.setColor(color);
}
estilo.setFont(letra);
// Bordes
if (bordes != null) {
// Borde izquierdo
if (bordes.charAt(0) == '1') {
estilo.setBorderLeft(XSSFCellStyle.BORDER_THIN);
}
// Borde superior
if (bordes.charAt(1) == '1') {
estilo.setBorderTop(XSSFCellStyle.BORDER_THIN);
}
// Borde derecho
if (bordes.charAt(2) == '1') {
estilo.setBorderRight(XSSFCellStyle.BORDER_THIN);
}
// Borde inferior
if (bordes.charAt(3) == '1') {
estilo.setBorderBottom(XSSFCellStyle.BORDER_THIN);
}
}
// alineación
if (alineacion != null) {
char horizontalAlign = alineacion.charAt(0);
if (horizontalAlign == '1') {
estilo.setAlignment(HorizontalAlignment.LEFT);
} else if (horizontalAlign == '2') {
estilo.setAlignment(HorizontalAlignment.CENTER);
} else if (horizontalAlign == '3') {
estilo.setAlignment(HorizontalAlignment.RIGHT);
}
char verticalAlign = alineacion.charAt(1);
if (verticalAlign == '1') {
estilo.setVerticalAlignment(VerticalAlignment.TOP);
} else if (verticalAlign == '2') {
estilo.setVerticalAlignment(VerticalAlignment.CENTER);
} else if (verticalAlign == '3') {
estilo.setVerticalAlignment(VerticalAlignment.BOTTOM);
}
if (alineacion.length() > 2) {
char ajustarTexto = alineacion.charAt(2);
if (ajustarTexto == '1') {
estilo.setWrapText(true);
} else if (ajustarTexto == '0') {
estilo.setWrapText(false);
}
}
}
if (colorFondo != null) {
estilo.setFillForegroundColor(new XSSFColor(colorFondo));
estilo.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
return estilo;
}
这就是我在特定单元格中使用它的方式:
XSSFSheet hojaVariables = wb.createSheet("nombreHoja");
XSSFRow row_2 = hojaVariables.createRow(1);
setCellValue(row_2, 1, "Data", crearEstilo(null, null, new Color(204, 255, 255), 8d, false, "1101", "12"));
private void setCellValue(XSSFRow row, int colNum, String value, XSSFCellStyle estilo) {
XSSFCell celda = row.createCell(colNum);
celda.setCellType(Cell.CELL_TYPE_STRING);
celda.setCellValue(value);
celda.setCellStyle(estilo);
}
如 Apache POI Quick Guide 中所示,Workbook
可以容纳的 CellStyle
的数量是有限制的。
这个问题是因为您为每个 Cell
创建了一个 CellStyle
,即使 CellStyle
是相同的。
创建 CellStyle
一次,并重复使用多个 Cell
。
CellStyle myCellStyle = crearEstilo(null, null, new Color(204, 255, 255), 8d, false, "1101", "12");
稍后,在创建单元格时,您可以重复使用 CellStyle
:
setCellValue(row_2, 1, "Data", myCellStyle);
我正在使用 POI 从 Java 生成一个 Excel 文件。 Excel 电子表格包含太多表格。当我生成文件并尝试使用 Excel 打开它时,出现此错误:"too many different cell formats".
这是我创建样式的方式:
private XSSFCellStyle crearEstilo(String formato, Color colorLetra, Color colorFondo, Double size, Boolean bold, String bordes, String alineacion) {
XSSFCellStyle estilo = wb.createCellStyle();
// Formato
if (formato != null) {
estilo.setDataFormat(wb.createDataFormat().getFormat(formato));
}
// Letra
XSSFFont letra = wb.createFont();
letra.setFontName("Arial");
if (size != null) {
letra.setFontHeight(size);
}
// Bold
if (bold != null) {
letra.setBold(bold);
}
// Color de letra
if (colorLetra != null) {
XSSFColor color = new XSSFColor(colorLetra);
letra.setColor(color);
}
estilo.setFont(letra);
// Bordes
if (bordes != null) {
// Borde izquierdo
if (bordes.charAt(0) == '1') {
estilo.setBorderLeft(XSSFCellStyle.BORDER_THIN);
}
// Borde superior
if (bordes.charAt(1) == '1') {
estilo.setBorderTop(XSSFCellStyle.BORDER_THIN);
}
// Borde derecho
if (bordes.charAt(2) == '1') {
estilo.setBorderRight(XSSFCellStyle.BORDER_THIN);
}
// Borde inferior
if (bordes.charAt(3) == '1') {
estilo.setBorderBottom(XSSFCellStyle.BORDER_THIN);
}
}
// alineación
if (alineacion != null) {
char horizontalAlign = alineacion.charAt(0);
if (horizontalAlign == '1') {
estilo.setAlignment(HorizontalAlignment.LEFT);
} else if (horizontalAlign == '2') {
estilo.setAlignment(HorizontalAlignment.CENTER);
} else if (horizontalAlign == '3') {
estilo.setAlignment(HorizontalAlignment.RIGHT);
}
char verticalAlign = alineacion.charAt(1);
if (verticalAlign == '1') {
estilo.setVerticalAlignment(VerticalAlignment.TOP);
} else if (verticalAlign == '2') {
estilo.setVerticalAlignment(VerticalAlignment.CENTER);
} else if (verticalAlign == '3') {
estilo.setVerticalAlignment(VerticalAlignment.BOTTOM);
}
if (alineacion.length() > 2) {
char ajustarTexto = alineacion.charAt(2);
if (ajustarTexto == '1') {
estilo.setWrapText(true);
} else if (ajustarTexto == '0') {
estilo.setWrapText(false);
}
}
}
if (colorFondo != null) {
estilo.setFillForegroundColor(new XSSFColor(colorFondo));
estilo.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
return estilo;
}
这就是我在特定单元格中使用它的方式:
XSSFSheet hojaVariables = wb.createSheet("nombreHoja");
XSSFRow row_2 = hojaVariables.createRow(1);
setCellValue(row_2, 1, "Data", crearEstilo(null, null, new Color(204, 255, 255), 8d, false, "1101", "12"));
private void setCellValue(XSSFRow row, int colNum, String value, XSSFCellStyle estilo) {
XSSFCell celda = row.createCell(colNum);
celda.setCellType(Cell.CELL_TYPE_STRING);
celda.setCellValue(value);
celda.setCellStyle(estilo);
}
如 Apache POI Quick Guide 中所示,Workbook
可以容纳的 CellStyle
的数量是有限制的。
这个问题是因为您为每个 Cell
创建了一个 CellStyle
,即使 CellStyle
是相同的。
创建 CellStyle
一次,并重复使用多个 Cell
。
CellStyle myCellStyle = crearEstilo(null, null, new Color(204, 255, 255), 8d, false, "1101", "12");
稍后,在创建单元格时,您可以重复使用 CellStyle
:
setCellValue(row_2, 1, "Data", myCellStyle);