首页 > Web开发 > 详细

(4) 如何用Apache POI操作Excel文件-----发现了POI-3.12一个回归bug

时间:2015-05-29 18:06:36      阅读:2353      评论:0      收藏:0      [点我收藏+]

好不容易盼望POI-3.12的最终版本能出来,这个月终于出来,于是先堵为快,从这个的地址(https://poi.apache.org/download.html#POI-3.12)下载最新的POI-3.12版本。

然后,跑了一个程序,对一个Excel的第一个单元格(A0)加上注解(comments),不幸的是,其抛出了下面的错误,

Exception in thread "main" java.lang.IllegalArgumentException: Multiple cell comments in one cell are not allowed, cell: A1
at org.apache.poi.xssf.usermodel.XSSFDrawing.createCellComment(XSSFDrawing.java:318)
at org.apache.poi.xssf.usermodel.XSSFDrawing.createCellComment(XSSFDrawing.java:52)
at com.tibco.poi.xssf.CellComments.main(CellComments.java:49)


然后有用同样的代码,在POI-3.10的版本上测试了一下,没有发现任何问题,所以,可知这是一个回归问题。下面是重现这个问题的代码(用的是我上篇文章的代码,只不过把第33行的代码做了一下小小修改,http://blog.csdn.net/chancein007/article/details/46238217,把注解(comment)加在了A1单元格 )。那么遇到这样的回归问题,该如何处理呢?这个时候,我们可以给Apache POI的Bugzilla系统新建一个bug,这样他们看到后,也许在下一个版本就帮我们解决了。具体如何开Bugzilla的bug,请看下一个章节。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.FileOutputStream;

/**
 * Demonstrates how to work with excel cell comments.
 * <p>
 * Excel comment is a kind of a text shape,
 * so inserting a comment is very similar to placing a text box in a worksheet
 * </p>
 *
 * @author Yegor Kozlov
 */
public class CellComments {
    public static void main(String[] args) throws IOException {
    	//1.创建一个工作簿对象
    	XSSFWorkbook wb = new XSSFWorkbook();

    	//2.得到一个POI的工具类
        CreationHelper factory = wb.getCreationHelper();

        //3. 创建一个工作表
        XSSFSheet sheet = wb.createSheet();
        
        //4.得到一个换图的对象
        Drawing drawing = sheet.createDrawingPatriarch();
        //5. ClientAnchor是附属在WorkSheet上的一个对象,  其固定在一个单元格的左上角和右下角.
        ClientAnchor anchor = factory.createClientAnchor();
        
        //6. 创建一个单元格(A0单元格)
        Cell cell0 = sheet.createRow(0).createCell(0);
        //6.1. 对这个单元格设置值
        cell0.setCellValue("Test");
        //6.2. 对这个单元格加上注解
        Comment comment0 = drawing.createCellComment(anchor);
        RichTextString str0 = factory.createRichTextString("Hello, World!");
        comment0.setString(str0);
        comment0.setAuthor("Apache POI");
        cell0.setCellComment(comment0);
        
        //7. 创建一个单元格(4F单元格)
        Cell cell1 = sheet.createRow(3).createCell(5);
        //7.1. 对这个单元格设置值
        cell1.setCellValue("F4");
        //7.2. 对这个单元格加上注解
        Comment comment1 = drawing.createCellComment(anchor);
        RichTextString str1 = factory.createRichTextString("Hello, World!");
        comment1.setString(str1);
        comment1.setAuthor("Apache POI");
        cell1.setCellComment(comment1);

        //8. 创建一个单元格(4F单元格)
        Cell cell2 = sheet.createRow(2).createCell(2);
        cell2.setCellValue("C3");

        Comment comment2 = drawing.createCellComment(anchor);
        RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
        //9。为注解设置字体
        Font font = wb.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short)14);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setColor(IndexedColors.RED.getIndex());
        str2.applyFont(font);

        comment2.setString(str2);
        comment2.setAuthor("Apache POI");
        comment2.setColumn(2);
        comment2.setRow(2);
        //10. 保存成Excel文件
        String fname = "comments.xlsx";
        FileOutputStream out = new FileOutputStream(fname);
        wb.write(out);
        out.close();

    }
}




(4) 如何用Apache POI操作Excel文件-----发现了POI-3.12一个回归bug

原文:http://blog.csdn.net/chancein007/article/details/46238469

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!