有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的。

那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就用Apache POI官方提供的例子,然后加上一些注解,给大家看一下。本例子的测试代码是基于POI-3.12的。

执行完后,将会生成上图所示的Excel工作表单(sheet)

  1. import org.apache.poi.ss.usermodel.*;
  2. import org.apache.poi.xssf.usermodel.XSSFSheet;
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  4. import java.io.IOException;
  5. import java.io.FileOutputStream;
  6. /**
  7. * Demonstrates how to work with excel cell comments.
  8. * <p>
  9. * Excel comment is a kind of a text shape,
  10. * so inserting a comment is very similar to placing a text box in a worksheet
  11. * </p>
  12. *
  13. * @author Yegor Kozlov
  14. */
  15. public class CellComments {
  16. public static void main(String[] args) throws IOException {
  17. //1.创建一个工作簿对象
  18. XSSFWorkbook wb = new XSSFWorkbook();
  19. //2.得到一个POI的工具类
  20. CreationHelper factory = wb.getCreationHelper();
  21. //3. 创建一个工作表
  22. XSSFSheet sheet = wb.createSheet();
  23. //4.得到一个换图的对象
  24. Drawing drawing = sheet.createDrawingPatriarch();
  25. //5. ClientAnchor是附属在WorkSheet上的一个对象,  其固定在一个单元格的左上角和右下角.
  26. ClientAnchor anchor = factory.createClientAnchor();
  27. //6. 创建一个单元格(2A单元格)
  28. Cell cell0 = sheet.createRow(1).createCell(0);
  29. //6.1. 对这个单元格设置值
  30. cell0.setCellValue("Test");
  31. //6.2. 对这个单元格加上注解
  32. Comment comment0 = drawing.createCellComment(anchor);
  33. RichTextString str0 = factory.createRichTextString("Hello, World!");
  34. comment0.setString(str0);
  35. comment0.setAuthor("Apache POI");
  36. cell0.setCellComment(comment0);
  37. //7. 创建一个单元格(4F单元格)
  38. Cell cell1 = sheet.createRow(3).createCell(5);
  39. //7.1. 对这个单元格设置值
  40. cell1.setCellValue("F4");
  41. //7.2. 对这个单元格加上注解
  42. Comment comment1 = drawing.createCellComment(anchor);
  43. RichTextString str1 = factory.createRichTextString("Hello, World!");
  44. comment1.setString(str1);
  45. comment1.setAuthor("Apache POI");
  46. cell1.setCellComment(comment1);
  47. //8. 创建一个单元格(4F单元格)
  48. Cell cell2 = sheet.createRow(2).createCell(2);
  49. cell2.setCellValue("C3");
  50. Comment comment2 = drawing.createCellComment(anchor);
  51. RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
  52. //9。为注解设置字体
  53. Font font = wb.createFont();
  54. font.setFontName("Arial");
  55. font.setFontHeightInPoints((short)14);
  56. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  57. font.setColor(IndexedColors.RED.getIndex());
  58. str2.applyFont(font);
  59. comment2.setString(str2);
  60. comment2.setAuthor("Apache POI");
  61. comment2.setColumn(2);
  62. comment2.setRow(2);
  63. //10. 保存成Excel文件
  64. String fname = "comments.xlsx";
  65. FileOutputStream out = new FileOutputStream(fname);
  66. wb.write(out);
  67. out.close();
  68. }
  69. }

如何用Apache POI操作Excel文件-----如何对一个单元格加注解?的更多相关文章

  1. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

    在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...

  2. 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图

    有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来. 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图 ...

  3. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  4. java使用Apache POI操作excel文件

    官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...

  5. 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据

    package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  6. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  7. java使用POI操作excel文件,实现批量导出,和导入

    一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...

  8. 利用Apache POI操作Excel

    最近在做接口,有个功能是利用Excel导入汽车发动机所需零件信息到线上系统中.简单回顾一下之前学过的用java操作Excel. 1.maven配置Apache POI pom.xml中配置POIjar ...

  9. c#操作Excel模板,替换命名单元格或关键字形成报表

    c#操作Excel模板,替换命名单元格或关键字形成报表 http://blog.sina.com.cn/s/blog_45eaa01a0102vqma.html一 建立Excel 模板文件 templ ...

随机推荐

  1. Tomcat 解决The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

    解法: 修改tomcat下的web.xml,     搜索:JspServlet, 增加: <init-param> <param-name>mappedfile</pa ...

  2. Android之记住密码与自动登陆实现

    本文主要讲述了利用sharedpreference实现记住密码与自动登陆功能 根据checkbox的状态存储用户名与密码 将结果保存在自定义的application中,成为全局变量 布局文件 < ...

  3. soft lockup和hard lockup介绍

    转自:http://www.cnblogs.com/openix/p/4034530.html 转自:http://blog.csdn.net/panzhenjie/article/details/1 ...

  4. async/await 异步编程

    前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入的,由于之前对于异步编程不是很了解,所以花费了一些时间学习一下相关的知识,并整理成这篇博客,如果在 ...

  5. 使用VS自带的报表RDLC结合报表控件ReportViewer使用

    1.新建一个报表,设置报表之后,使用强类型的DataSet  xsd 配置数据源,对报表中的使用最常用的是文本框和表格控件 2.新增WebForm窗体,拖一个ReportViewer控件,在WebFo ...

  6. win32_11gR2_database安装教程

  7. Dos del参数与作用(/f/s/q)

    Dos del参数与作用(/f/s/q) C:\Documents and Settings>del /? 删除一个或数个文件. DEL [/P] [/F] [/S] [/Q] [/A[[:]a ...

  8. ☆☆在Eclipse中编译NDK的so文件(普通安卓项目转换为NDK项目的设定)

    1 将Native的编译链接配置加入项目中 2 进行编译 3  项目支持Native后,在首尾分别新增了两个编译过程

  9. Touch 事件的分发和消费机制

    Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...

  10. SQL Server连接数据库失败,可能的问题!

    SQL Server Configuration Manager中启动服务 SQL Server外围应用配置器中,打开远程IP连接属性 别的应该没什么问题了!