2015-01-15 77 views
5

我們有很多由apache poi生成的excel報告。其中一些包含標題中的註釋。由於有很多報告,我們希望創建用於添加評論的通用解決方案。正如我們注意到,您可以根據代碼被添加到細胞中是這樣的:用apache-poi自動調整excel評論

public static void addComment(final Workbook workbook, final Sheet sheet, final Cell cell, final Row row, 
     final String comment) { 

    final CreationHelper factory = workbook.getCreationHelper(); 

    final Drawing drawing = sheet.createDrawingPatriarch(); 

    final ClientAnchor anchor = factory.createClientAnchor(); 
    anchor.setCol1(cell.getColumnIndex()); 
    anchor.setCol2(cell.getColumnIndex() + 3); 
    anchor.setRow1(row.getRowNum()); 
    anchor.setRow2(row.getRowNum() + 5); 

    final Comment cellComment = drawing.createCellComment(anchor); 
    final RichTextString richText = factory.createRichTextString(comment); 
    cellComment.setString(richText); 
    cell.setCellComment(cellComment); 
} 

我們還注意到,評論框的大小可以通過使用列/行索引集 - 這是我們主要的問題,因爲如果第一列100像素第二個有1000px,那麼註釋寬度將是1000px。這是我們的問題 - 是否有可能使用像素而不是使用apache poi的列/行索引來設置評論大小?或者也許有一種方法來自動計算評論大小與Apache poi?

回答

0

ClientAnchor支持偏移,至少在具體實施方式做:

public XSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, int col1, int row1, int col2, int row2)

DX1從左側偏移,DX2從右側偏移,DY1從頂部偏移,DY2從偏置底部(col1,row1開始行col,col2,row2結束行col,非包含)。

因此,要減少寬度,只需將一個非零值分配給dx2即可。

dx1,dx2,dy1和dy2是英制公制單位(EMU)。每個EMU有9525個像素,所以你必須使用相當大的值。