Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSSFFont不';不适用于Java POI的所有单元格_Java_Excel_Apache Poi - Fatal编程技术网

XSSFFont不';不适用于Java POI的所有单元格

XSSFFont不';不适用于Java POI的所有单元格,java,excel,apache-poi,Java,Excel,Apache Poi,我用来应用粗体字体的XSSFFont对象对某些单元格有效,而对其余单元格无效 某些单元格在代码中设置为粗体,但在excel文件中显示为未绑定(粗体按钮在excel中高亮显示,但单元格不粗体),请参见下图 但是,如果我有一些未分类的证券(如40种)(与现金处于同一级别),现金将以粗体格式显示,如下所示。在第一张图中,未分类证券有很多证券(520个) 见下图 我的代码是 private void writeCellToExcel(Workbook workBook, ExportExcelCel

我用来应用粗体字体的XSSFFont对象对某些单元格有效,而对其余单元格无效

某些单元格在代码中设置为粗体,但在excel文件中显示为未绑定(粗体按钮在excel中高亮显示,但单元格不粗体),请参见下图

但是,如果我有一些未分类的证券(如40种)(与现金处于同一级别),现金将以粗体格式显示,如下所示。在第一张图中,未分类证券有很多证券(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;



   }

我基本上尽可能检查了您的代码,因为您没有提供常量类
ExportExcelConstants
和字段
mergedRowsCells
,但是设置文本格式的代码似乎是正确的。在我的测试用例中,我用粗体字体填充了约2500行文本,由您的代码生成。。一切都很顺利。所以我想,这不是ApachePOI的问题。猜测,因为我不得不模拟一些
ExportExcelCell
实例,并且不知道它是否与您正在使用的那些实例相当。我建议您仔细调试代码。为了您的评论,实际上我找到了问题的原因,这是唯一字体的最大数量(每个单元格的字体)工作簿中的单元格限制为32767,由于我有超过32767个单元格,并且我为每个单元格创建了唯一的字体,因此超过32767的单元格无法获得正确的字体
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 + "]";

   }


}