POI Excel单元格高亮显示“修复记录:从/xl/style.xml部分(样式)格式化

3

我一直在研究如何在执行结束后,在打开处理过的文件(.xlsx文件)时在Excel表格中突出显示单元格。但是,当我使用Ms-excel-2007打开文件时,出现了两个弹窗。下面是相关代码和图片:

  HSSFWorkbook xlsWB = new HSSFWorkbook(file1InputStream);

  XSSFWorkbook xlsxWB = new XSSFWorkbook(file2InputStream);

  CellStyle xlsxStyle = getCellStyle(xlsxWB);

  int numbeoOfSheetsFile1 = xlsWB.getNumberOfSheets();
  int numbeoOfSheetsFile2 = xlsxWB.getNumberOfSheets();

  for (int sheetIndex = 0; sheetIndex < numbeoOfSheetsFile1 && sheetIndex < numbeoOfSheetsFile2; sheetIndex++) {

   HSSFSheet sheetFile1 = xlsWB.getSheetAt(sheetIndex);
   XSSFSheet sheetFile2 = xlsxWB.getSheetAt(sheetIndex);

   int noOfRowsSheetFile1 = sheetFile1.getLastRowNum();
   int noOfRowsSheetFile2 = sheetFile2.getLastRowNum();

   if (noOfRowsSheetFile1 < noOfRowsSheetFile2) {
    for (int vRow = noOfRowsSheetFile1; vRow <= noOfRowsSheetFile2 - 1; vRow++) {
     sheetFile2.createRow(vRow).setRowStyle(xlsxStyle);
     //sheetFile2.getRow(vRow).setRowStyle(xlsxStyle);
    }
   }
   if (noOfRowsSheetFile1 > noOfRowsSheetFile2) {
    for (int vRow = noOfRowsSheetFile2 + 1; vRow <= noOfRowsSheetFile1; vRow++) {
     sheetFile2.createRow(vRow).setRowStyle(xlsxStyle);
     //sheetFile2.getRow(vRow).setRowStyle(xlsxStyle);
    }
   }

   for (int vRow = 0; vRow <= noOfRowsSheetFile1; vRow++) {
    HSSFRow rwFile1 = sheetFile1.getRow(vRow);
    XSSFRow rwFile2 = sheetFile2.getRow(vRow);

    int noOfColSheetFile1 = sheetFile1.getRow(vRow).getLastCellNum();

    int noOfColSheetFile2 = 0;
    try {
     noOfColSheetFile2 = sheetFile2.getRow(vRow).getLastCellNum();
    } catch (NullPointerException e) {
     rwFile2 = sheetFile2.createRow(vRow);
     noOfColSheetFile2 = 0;
    }

    // Coloring of mismatch columns
    if (noOfColSheetFile1 < noOfColSheetFile2) {
     for (int vCol = noOfColSheetFile1 + 1; vCol <= noOfColSheetFile2; vCol++) {
      rwFile2.createCell(vCol);
      //rwFile2.getCell(vCol).setCellStyle(xlsxStyle);
     }
    }


    if (noOfColSheetFile1 > noOfColSheetFile2) {
     for (int vCo2 = noOfColSheetFile2 + 1; vCo2 <= noOfColSheetFile1; vCo2++) {
      rwFile2.createCell(vCo2);
      //rwFile2.getCell(vCo2).setCellStyle(xlsxStyle);
     }
    }
    for (int vCol = 0; vCol < noOfColSheetFile1; vCol++) {
     //System.out.println("vCol>>" + vCol + "--vRow>>" + vRow);
     HSSFCell cellFile1 = rwFile1.getCell(vCol);
     XSSFCell cellFile2 = rwFile2.getCell(vCol);
     String cellFile1Value = null;
     if (null != cellFile1) {
      cellFile1Value = cellFile1.toString();

     }
     String cellFile2Value = null;
     if (null != cellFile2) {
      cellFile2Value = cellFile2.toString();
     }

  if ((null == cellFile1Value && null != cellFile2Value) || (null != cellFile1Value && null == cellFile2Value)          || (null != cellFile1Value && cellFile1Value.compareTo(cellFile2Value) != 0)) {

  if ((null != cellFile1Value && !cellFile1Value.isEmpty()) || (null != cellFile2Value && !          cellFile2Value.isEmpty())) { 
    CellValues cellValues = new CellValues();
    cellValues.setValue1(cellFile1Value);
    cellValues.setValue2(cellFile2Value);
    cellValues.setCellRow(vRow);
    cellValues.setCellColumn(vCol);
    scenarioResultDetails.addDifference(cellValues);
    XSSFCell cellDiff = rwFile2.getCell(vCol);
    if (null == cellDiff) {
     cellDiff = rwFile2.createCell(vCol);
    }
    cellDiff.setCellStyle(xlsxStyle);
   }
  }
    }
   }
  }
  String fileDiff = file2.replace(".xls", "_diff.xls");
  FileOutputStream fileOut = new FileOutputStream(fileDiff);
  xlsxWB.write(fileOut);
  if (null != fileOut) {
   try {
    fileOut.close();
   } catch (Exception e) {
   }
  }
  if (scenarioResultDetails.getDifferencesList().size() > 0) {
   scenarioResultDetails.setResult(false);
   scenarioResultDetails.setResultText("Not Matched");
   scenarioResultDetails.setDiffExcel(fileDiff);
  } else {
   scenarioResultDetails.setResult(true);
   scenarioResultDetails.setResultText("Matched");
  }
  return scenarioResultDetails;
 }

 public static void openFile(String fileName){
  ReportGenerator.getDriver().get("file://"+fileName);
 }
 private static CellStyle getCellStyle(XSSFWorkbook xssfWorkbook){
  CellStyle style = xssfWorkbook.createCellStyle();
  Font font = xssfWorkbook.createFont();
  font.setColor(IndexedColors.BLACK.getIndex());
  style.setFont(font);
  style.setFillForegroundColor(HSSFColor.YELLOW.index);
  style.setFillBackgroundColor(HSSFColor.YELLOW.index);
  style.setFillPattern(CellStyle.ALIGN_RIGHT);
  return style;
 }
}

enter image description here

enter image description here


你正在使用哪个版本的Apache POI?如果不是最新的版本,你尝试升级了吗? - Gagravarr
我正在使用Apache POI 3.9版本,尝试将其升级到3.12版本,但未成功。 - yoganandh
1个回答

3

这种行为仍然存在(或再次出现)。我正在使用NuGet NPOI包的2.4.1版本,该版本显然修复了FontHeight属性的错误,但也引入了此问题。

问题在于,由xssfWorkbook.createFont()返回的字体大小非常小。您必须明确设置它,如下所示:

    IFont font = excel.CreateFont();
    font.FontHeightInPoints = 11;

当我覆盖一个之前被修复过的文件时,我遇到了类似的错误。确保每次更改代码时都写入一个干净/新的文件,以便不会受到先前的损坏的影响。

完整示例:

    /// <summary>
    /// Return style for header cells.
    /// </summary>
    /// <returns></returns>
    private static ICellStyle GetHeaderStyle(this XSSFWorkbook excel)
    {
        IFont font = excel.CreateFont();
        font.IsBold = true;
        //Added this explicitly, initial font size is tiny
        font.FontHeightInPoints = 11;

        ICellStyle style = excel.CreateCellStyle();
        style.FillForegroundColor = IndexedColors.Grey25Percent.Index;
        style.FillPattern = FillPattern.SolidForeground;
        style.FillBackgroundColor = IndexedColors.Grey25Percent.Index;          

        style.BorderBottom = style.BorderLeft = style.BorderRight = style.BorderTop = BorderStyle.Thin;
        style.BottomBorderColor = style.LeftBorderColor = style.RightBorderColor = style.TopBorderColor = IndexedColors.Black.Index;

        style.SetFont(font);
        return style;
    }

OP的问题是关于Java版本的POI而不是NPOI,但是感谢您发布这个帖子,因为它很快解决了我的问题。 - Jérôme MEVEL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接