(3) 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?
来源:互联网 发布:ssl nginx 编辑:程序博客网 时间:2024/06/06 03:27
有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的。
那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就用Apache POI官方提供的例子,然后加上一些注解,给大家看一下。本例子的测试代码是基于POI-3.12的。
执行完后,将会生成上图所示的Excel工作表单(sheet)
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. 创建一个单元格(2A单元格) Cell cell0 = sheet.createRow(1).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(); }}
1 0
- (3) 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?
- (7) 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图
- (4) 如何用Apache POI操作Excel文件-----发现了POI-3.12一个回归bug
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
- (1) 如何用Apache POI操作Excel文件-----入门
- (8) 如何用Apache POI操作Excel文件-----POI-3.10.1 的一个黑白颜色颠倒的bug以及解决方案
- (2) 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?
- (5) 如何用Apache POI操作Excel文件-----发现Apache的POI的Bug后,如何给Apache的POI报Bug?
- (9) 如何用Apache POI操作Excel文件-----应用条件样式(ConditionalFormatting)
- Apache POI 第三讲之对Excel单元格的操作
- 如何用Delphi在Excel文件中的某个单元格加个下拉框
- POI-----POI操作Excel-3、单元格
- Apache POI工具对excel文件操作的简单使用
- Apache POI 操作Excel文件
- poi 操作excel , 合并单元格后加边框
- apache POI 对Excel表的操作
- Apache POI对EXCEL的操作
- Apache POI Excel合并单元格
- hql 中的case when运用查询
- Dubbo:来自于阿里巴巴的分布式服务框架
- jndi
- [cmd]查看某端口被占用的程序
- SpringMVC入门学习(三)+Spring应用(整合)
- (3) 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?
- java文件下载
- PAT 1006 相关字符串函数应用
- ORACLE数据库中的表连接关系
- JAVA基础五——面向对象一
- eclipse设置连接网络
- ASP.NET Ajax client-side framework failed to load OR ‘Sys’ is undefined(ASP.NET Ajax 客户端框架未能加载)
- iOS set/get 方法使用注意
- coredata笔记2cook