[英]How to change the color of the comment indicator using Apache POI
我正在開發一個會計軟件,我們在其中生成和接收 excel 文件作為輸入,在每個文件中都有標題,一些標題需要描述,所以我們將其添加為單元格注釋,當用戶上傳文件時,我們對其進行解析並驗證每個單元格,當一個單元格無效時,我們會返回該文件並在該單元格上添加注釋,說明問題所在
問題是我希望標題上的注釋是綠色而不是紅色(這顯然是默認顏色)
這是我們用來在單元格上添加評論的方法
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);
}
環顧四周時,我發現這個鏈接顯然可以完成工作,但它是在 VB 中,而不是使用 Apache POI
如您提供的鏈接中所述:
我們沒有直接的方法可以快速輕松地更改評論指示器的顏色,但是,下面的 VBA 代碼可以幫助您在活動工作表上繪制一個三角形與每個評論指示器重疊,並使用您需要的特定顏色。
這是一個非常丑陋的解決方法。 但是使用apache poi
可以做同樣的事情。 Apache poi
還提供在繪圖層中創建形狀。 當然, HSSF
和XSSF
之間存在差異需要考慮。 如果有人更改放置三角形形狀的單元格的列寬,整個事情就會中斷。 然后三角形的大小也會改變。 但這也與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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.