我以前用于应用Bold Font的XSSFFont对象适用于某些单元格而不适用于其余单元格。
某些单元格在代码中设置为粗体,但在excel文件中显示为unbold(粗体按钮在excel中突出显示,但单元格不粗体)请参见下面的图像
但是,如果我在未分类(与现金水平相同)下的证券(如40)很少,则现金以粗体显示,如下所示。在第一张图片中,Unclassified有很多证券(520个) 见下图
我的代码是
private void writeCellToExcel(Workbook workBook, ExportExcelCell cell,
XSSFRow excelRow, int cellNumber, boolean isLastCell) {
XSSFCell excelCell = (XSSFCell) excelRow.createCell(cellNumber);
XSSFRichTextString cellValue = new XSSFRichTextString(cell.getText());
XSSFCellStyle style = (XSSFCellStyle) workBook.createCellStyle();
XSSFFont font = (XSSFFont) workBook.createFont();
font.setFontName(ExportExcelConstants.EXPORT_EXCEL_DEFAULT_FONT_NAME);
font.setFontHeight(ExportExcelConstants.EXPORT_EXCEL_DEFAULT_FONT_SIZE);
applyCellStyleAndMerging(cell, cellNumber, isLastCell, excelCell,
cellValue, style, font);
// sheet.autoSizeColumn(cellNumber);
}
private void applyCellStyleAndMerging(ExportExcelCell cell, int cellNumber,
boolean isLastCell, XSSFCell excelCell,
XSSFRichTextString cellValue, CellStyle style, XSSFFont font) {
applyCellStyleAndData(cell, excelCell, cellNumber, cellValue, style,
font);
int cellColSize = cell.getCellColSpan();
int cellRowSize = cell.getCellRowSpan();
// in case the cell might be more than one column or more than one
// row
if (cellColSize > 1 || cellRowSize > 1) {
applyCellMerging(cellNumber, isLastCell, cellColSize, cellRowSize);
}
}
private void applyCellStyleAndData(ExportExcelCell cell,
XSSFCell excelCell, int cellNumber, XSSFRichTextString cellValue,
CellStyle style, XSSFFont font) {
applyCellFontStyle(cell, cellValue, style, font);
applyCellAlignmentIndentionAndValue(cell, excelCell, cellNumber,
cellValue, style);
}
private void applyCellFontStyle(ExportExcelCell cell,
XSSFRichTextString cellValue, CellStyle style, XSSFFont font) {
String fontStyle = cell.getStyle();
if (ExportExcelConstants.EXPORT_EXCEL_CELL_FONT_BOLD
.equalsIgnoreCase(fontStyle)) {
font.setBold(true);
style.setFont(font);
} else if (ExportExcelConstants.EXPORT_EXCEL_CELL_FONT_SHEET_HEAD
.equalsIgnoreCase(fontStyle)) {
int colunindex = cell.getText().indexOf(":");
font.setBold(true);
cellValue.applyFont(0, colunindex, font);
} else if (ExportExcelConstants.EXPORT_EXCEL_CELL_FONT_ITALIC
.equalsIgnoreCase(fontStyle)) {
font.setItalic(true);
style.setFont(font);
} else if (ExportExcelConstants.EXPORT_EXCEL_CELL_FONT_UNDERLINE
.equalsIgnoreCase(fontStyle)) {
font.setUnderline(XSSFFont.U_SINGLE);
style.setFont(font);
} else {
style.setFont(font);
}
}
private void applyCellAlignmentIndentionAndValue(ExportExcelCell cell,
XSSFCell excelCell, int cellNumber, XSSFRichTextString cellValue,
CellStyle style) {
String cellAlignment = cell.getCellAlignment();
style.setAlignment(getCellAlignment(cellAlignment));
style.setVerticalAlignment(ExportExcelConstants.EXPORT_EXCEL_DEFAULT_CELL_VERTICAL_ALIGNMENT);
excelCell.setCellValue(cellValue);
excelCell.setCellStyle(style);
}
private void applyCellMerging(int cellNum, boolean isLastCell,
int cellColSize, int cellRowSize) {
int firstrowMergedIndex = rowNumber + 1;
int lastrowMergedIndex = 0;
int cellMerged = cellNum;
ArrayList<Integer> columnMerged = new ArrayList<Integer>();
if (cellColSize > 1 && cellRowSize == 1) {
sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber,
cellNum, cellNum + cellColSize - 1));
} else if (cellColSize == 1 && cellRowSize > 1) {
sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber
+ cellRowSize - 1, cellNum, cellNum));
lastrowMergedIndex = rowNumber + cellRowSize - 1;
if (isLastCell) {
rowNumber = rowNumber + cellRowSize - 1;
}
columnMerged.add(cellNum);
while (firstrowMergedIndex <= lastrowMergedIndex) {
mergedRowsCells.put(firstrowMergedIndex, columnMerged);
firstrowMergedIndex++;
}
} else {
sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber
+ cellRowSize - 1, cellNum, cellNum + cellColSize - 1));
lastrowMergedIndex = rowNumber + cellRowSize - 1;
if (isLastCell) {
rowNumber = rowNumber + cellRowSize - 1;
}
while (firstrowMergedIndex <= lastrowMergedIndex) {
while (cellMerged <= cellNum + cellColSize - 1) {
columnMerged.add(cellMerged);
cellMerged++;
}
mergedRowsCells.put(firstrowMergedIndex, columnMerged);
firstrowMergedIndex++;
}
}
}
private Short getCellAlignment(String cellAlignment) {
if (cellAlignment == null) {
return CellStyle.ALIGN_LEFT;
}
Short alignment = null;
switch (cellAlignment) {
case "left":
alignment = CellStyle.ALIGN_LEFT;
break;
case "center":
alignment = CellStyle.ALIGN_CENTER_SELECTION;
break;
case "right":
alignment = CellStyle.ALIGN_RIGHT;
break;
case "fill":
alignment = CellStyle.ALIGN_FILL;
break;
case "default":
alignment = CellStyle.ALIGN_LEFT;
break;
}
return alignment;
}
private int getFirstCellToWrite(XSSFRow excelRow, boolean isSubRow,
int nestedLevel) {
if (!isSubRow) {
if (mergedRowsCells.containsKey(rowNumber)) {
ArrayList<Integer> mergedCells = mergedRowsCells.get(rowNumber);
return mergedCells.get(mergedCells.size() - 1) + 1;
}
return 0;
}
return nestedLevel;
}
域对象是
public class ExportExcelCell {
@JsonProperty("text")
private String text;
@JsonProperty("style")
private String style;
@JsonProperty("cellRowSpan")
private Integer cellRowSpan;
@JsonProperty("cellColSpan")
private Integer cellColSpan;
@JsonProperty("cellAlignment")
private String cellAlignment;
public ExportExcelCell() {
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public Integer getCellRowSpan() {
return cellRowSpan;
}
public void setCellRowSpan(int cellRowSpan) {
this.cellRowSpan = cellRowSpan;
}
public Integer getCellColSpan() {
return cellColSpan;
}
public void setCellColSpan(int cellColSpan) {
this.cellColSpan = cellColSpan;
}
public String getCellAlignment() {
return cellAlignment;
}
public void setCellAlignment(String exportExcelDefaultCellAlignment) {
this.cellAlignment = exportExcelDefaultCellAlignment;
}
@Override
public String toString() {
return "ExportExcelCell [text=" + text + ", style=" + style
+ ", cellRowSpan=" + cellRowSpan + ", cellColSpan="
+ cellColSpan + ", cellAlignment=" + cellAlignment + "]";
}
}