【问题标题】:How to change the color of the comment indicator using Apache POI如何使用 Apache POI 更改评论指示器的颜色
【发布时间】:2020-05-07 18:21:36
【问题描述】:

我正在开发一个会计软件,我们在其中生成和接收 excel 文件作为输入,在每个文件中都有标题,一些标题需要描述,所以我们将其添加为单元格注释,当用户上传我们解析的文件时它并验证每个单元格,当一个单元格无效时,我们返回文件并在该单元格上添加注释,解释问题所在

像这样:

问题是我希望标题上的 cmets 是绿色而不是红色(这显然是默认颜色)

这是我们用来在单元格上添加评论的方法

public static void setComment(Cell cell, String commentString) {
    CreationHelper factory = cell.getSheet().getWorkbook().getCreationHelper();
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + NumberConstant.THREE);
    anchor.setRow1(cell.getRow().getRowNum());
    anchor.setRow2(cell.getRow().getRowNum() + NumberConstant.THREE);
    Drawing drawing = cell.getSheet().createDrawingPatriarch();
    Comment comment = cell.getCellComment();
    if (comment == null) {
        comment = drawing.createCellComment(anchor);
    }
    comment.setString(factory.createRichTextString(commentString));
    cell.setCellComment(comment);
}

环顾四周时,我发现 this link 显然可以完成工作,但它是在 VB 中,而不是使用 Apache POI

【问题讨论】:

  • 我不确定您是否真的可以更改指示器的颜色。该 VBA 方法只是在顶部添加一个三角形。这是你想做的吗?
  • 如果使用 Apache POI 可以实现,那么确定

标签: java excel spring-boot apache-poi


【解决方案1】:

如您提供的链接所述:

我们没有直接的方法来改变评论的颜色 指标快速轻松,但是,以下 VBA 代码可以提供帮助 您绘制一个三角形与每个评论指示器重叠 您在活动工作表上需要的特定颜色。

这是一个非常丑陋的解决方法。但是使用apache poi 也可以做到这一点。 Apache poi 还提供在绘图层中创建形状。当然HSSFXSSF 之间存在差异需要考虑。如果有人更改放置了三角形形状的单元格的列宽,整个事情就会中断。然后三角形的大小也会改变。但是VBA“解决方案”也是一样的。

例子:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.Units;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

class CreateExcelWithComments {

 static void createCellComment(Cell cell, String commentText) {
  // Create the anchor
  CreationHelper creationHelper = cell.getSheet().getWorkbook().getCreationHelper();
  ClientAnchor anchor = creationHelper.createClientAnchor();
  // When the comment box is visible, have it show in a 1 column x 3 rows space
  anchor.setCol1(cell.getColumnIndex() + 1);
  anchor.setCol2(cell.getColumnIndex() + 2);
  anchor.setRow1(cell.getRow().getRowNum());
  anchor.setRow2(cell.getRow().getRowNum() + 3);
  // Create the comment and set the text
  Drawing drawing = cell.getSheet().createDrawingPatriarch();
  Comment comment = drawing.createCellComment(anchor);
  RichTextString richTextString = creationHelper.createRichTextString(commentText);
  comment.setString(richTextString);
  // Assign the comment to the cell
  cell.setCellComment(comment);
 }

 static void createTriangleShapeTopRight(Cell cell, int width, int height, int r, int g, int b) {
  // Get cell width in pixels
  float columnWidth = cell.getSheet().getColumnWidthInPixels(cell.getColumnIndex());
  // Get row heigth in pixels
  float rowHeight = cell.getRow().getHeightInPoints() * Units.PIXEL_DPI / Units.POINT_DPI;
  // Create the anchor
  CreationHelper creationHelper = cell.getSheet().getWorkbook().getCreationHelper();
  ClientAnchor anchor = creationHelper.createClientAnchor();
  // Shape starts top, right - shape width and ends top + shape height, right of the cell
  anchor.setCol1(cell.getColumnIndex());
  if (anchor instanceof XSSFClientAnchor) {
   anchor.setDx1(Math.round((columnWidth - width)) * Units.EMU_PER_PIXEL);
  } else if (anchor instanceof HSSFClientAnchor) {
   //see https://stackoverflow.com/questions/48567203/apache-poi-xssfclientanchor-not-positioning-picture-with-respect-to-dx1-dy1-dx/48607117#48607117 for HSSF
   int DEFAULT_COL_WIDTH = 10 * 256;
   anchor.setDx1(Math.round((columnWidth - width) * Units.DEFAULT_CHARACTER_WIDTH / 256f * 14.75f * DEFAULT_COL_WIDTH / columnWidth));
  }
  anchor.setCol2(cell.getColumnIndex() + 1); // left of column index + 1 == right of this cell
  anchor.setDx2(0);
  anchor.setRow1(cell.getRow().getRowNum());
  anchor.setDy1(0);
  anchor.setRow2(cell.getRow().getRowNum());
  if (anchor instanceof XSSFClientAnchor) {
   anchor.setDy2(height * Units.EMU_PER_PIXEL);
  } else if (anchor instanceof HSSFClientAnchor) {
   //see https://stackoverflow.com/questions/48567203/apache-poi-xssfclientanchor-not-positioning-picture-with-respect-to-dx1-dy1-dx/48607117#48607117 for HSSF
   float DEFAULT_ROW_HEIGHT = 12.75f;
   anchor.setDy2(Math.round(height * Units.PIXEL_DPI / Units.POINT_DPI * 14.75f * DEFAULT_ROW_HEIGHT / rowHeight));
  }
  // Create the shape
  Drawing drawing = cell.getSheet().createDrawingPatriarch();
  if (drawing instanceof XSSFDrawing) {
   XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
   shape.setShapeType(ShapeTypes.RT_TRIANGLE);
   // Flip the shape horizontal and vertical
   shape.getCTShape().getSpPr().getXfrm().setFlipH(true);
   shape.getCTShape().getSpPr().getXfrm().setFlipV(true);
   // Set color
   shape.setFillColor(r, g, b);
  } else if (drawing instanceof HSSFPatriarch) {
   HSSFSimpleShape shape = ((HSSFPatriarch)drawing).createSimpleShape((HSSFClientAnchor)anchor);
   shape.setShapeType(HSSFShapeTypes.RightTriangle);
   // Flip the shape horizontal and vertical
   shape.setFlipHorizontal(true);
   shape.setFlipVertical(true);
   // Set color
   shape.setFillColor(r, g, b);
   shape.setLineStyle(HSSFShape.LINESTYLE_NONE);
  }
 }

 public static void main(String[] args) throws Exception {

  //Workbook workbook = new HSSFWorkbook(); String filePath = "./Excel.xls";
  Workbook workbook = new XSSFWorkbook(); String filePath = "./Excel.xlsx";

  Sheet sheet = workbook.createSheet();
  Row row; 
  Cell cell;

  row = sheet.createRow(3);
  cell = row.createCell(5);
  cell.setCellValue("F4");
  sheet.setColumnWidth(cell.getColumnIndex(), 10 * 256);   
  createCellComment(cell, "Cell comment for F4");
  createTriangleShapeTopRight(cell, 10, 10, 0, 255, 0);

  row = sheet.createRow(1);
  cell = row.createCell(1);
  cell.setCellValue("B2");
  sheet.setColumnWidth(cell.getColumnIndex(), 10 * 256);   
  createCellComment(cell, "Cell comment for B2");
  createTriangleShapeTopRight(cell, 10, 10, 0, 255, 0);

  try (FileOutputStream out = new FileOutputStream(filePath)) {
   workbook.write(out);
  }

  workbook.close();

 }
}

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    相关资源
    最近更新 更多