完美解決POI寫批注報錯comments in one cell are not allowed, cell....問題


解決方案:

示例代碼如下:

  /**
     * 給某一格設置注釋
     * @param sheet
     * @param rowIndex
     * @param colIndex
     * @param value
     */
    public static void setCellCommon(Sheet sheet, int rowIndex, int colIndex, String value) {
        Row row = sheet.getRow(rowIndex);
        if (row == null) {
//            log.debug(ExcelConfig.ERROR + " setCellCommon," + sheet.getSheetName() + "第" + rowIndex + "為NULL");
            return;
        }
        Cell cell = row.getCell(colIndex);
        if (cell == null) {
            cell = row.createCell(colIndex);
        }
        if(value == null){
            cell.removeCellComment();
            return;
        }
        Drawing drawing = sheet.createDrawingPatriarch();
        CreationHelper factory = sheet.getWorkbook().getCreationHelper();
        ClientAnchor anchor = factory.createClientAnchor();

        //(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
        //前四個參數是坐標點,后四個參數是編輯和顯示批注時的大小.
        //以上參數不設置時會有默認值,當一個被重復設置批注時會報錯Multiple cell comments in one cell are not allowed
        //故在設置批注前檢查錨點位置有無批注,有的話移除
        Row row1 = sheet.getRow(anchor.getRow1());
        if(row1 != null){
            Cell cell1 = row1.getCell(anchor.getCol1());
            if(cell1 != null){
                cell1.removeCellComment();
            }
        }

        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString(value);
        comment.setString(str);
        comment.setAuthor("Auto+");
        cell.setCellComment(comment);
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM