如何用Apache POI操作Excel文件-----如何对一个单元格加注解?
有的时候,我们需要通过操作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();
- }
- }
如何用Apache POI操作Excel文件-----如何对一个单元格加注解?的更多相关文章
- 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?
在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...
- 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图
有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来. 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图 ...
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...
- java使用Apache POI操作excel文件
官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...
- 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据
package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- java使用POI操作excel文件,实现批量导出,和导入
一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...
- 利用Apache POI操作Excel
最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...
- c#操作Excel模板,替换命名单元格或关键字形成报表
c#操作Excel模板,替换命名单元格或关键字形成报表 http://blog.sina.com.cn/s/blog_45eaa01a0102vqma.html一 建立Excel 模板文件 templ ...
随机推荐
- Tomcat 解决The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
解法: 修改tomcat下的web.xml, 搜索:JspServlet, 增加: <init-param> <param-name>mappedfile</pa ...
- Android之记住密码与自动登陆实现
本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...
- soft lockup和hard lockup介绍
转自:http://www.cnblogs.com/openix/p/4034530.html 转自:http://blog.csdn.net/panzhenjie/article/details/1 ...
- async/await 异步编程
前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入的,由于之前对于异步编程不是很了解,所以花费了一些时间学习一下相关的知识,并整理成这篇博客,如果在 ...
- 使用VS自带的报表RDLC结合报表控件ReportViewer使用
1.新建一个报表,设置报表之后,使用强类型的DataSet xsd 配置数据源,对报表中的使用最常用的是文本框和表格控件 2.新增WebForm窗体,拖一个ReportViewer控件,在WebFo ...
- win32_11gR2_database安装教程
- Dos del参数与作用(/f/s/q)
Dos del参数与作用(/f/s/q) C:\Documents and Settings>del /? 删除一个或数个文件. DEL [/P] [/F] [/S] [/Q] [/A[[:]a ...
- ☆☆在Eclipse中编译NDK的so文件(普通安卓项目转换为NDK项目的设定)
1 将Native的编译链接配置加入项目中 2 进行编译 3 项目支持Native后,在首尾分别新增了两个编译过程
- Touch 事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- SQL Server连接数据库失败,可能的问题!
SQL Server Configuration Manager中启动服务 SQL Server外围应用配置器中,打开远程IP连接属性 别的应该没什么问题了!