java - POI Excel 单元格突出显示“已修复的记录 : Format From/xl/style. xml 部分(样式)

标签 java css excel apache-poi vba

当我使用 Ms-excel-2007 打开处理后的文件(.xlsx 文件)时,我一直在执行结束后在 excel 工作表中突出显示单元格,我面临两个弹出窗口。这里是指定的代码和图像:

  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

最佳答案

这种行为仍然(或再次)存在。我使用的是 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;
    }

关于java - POI Excel 单元格突出显示“已修复的记录 : Format From/xl/style. xml 部分(样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31067209/

相关文章:

html - VBA 宏 : problem to click a button (with class) on internet website (through internet explorer)

excel - 双击复制单元格并自动粘贴到另一张纸上的不同单元格

java - Mac 雪豹上的 Intellij 13 无法编译

java - 将 CharArray 传递给方法

css:放入此字段集中时属性发生了惊人的变化

javascript - 仅使用 javascript 和 html 读取 csv 或 excel (xlsx) 文件?

Java:安全套接字接受

java - 为什么主线程在这里没有被抢占?

image - 在填充上有渐变背景封面按钮

html - 怎样把 table Angular 弄圆