如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

摘要:
有的时候,我们需要通过操作ApachePOI,在生成Cell数据的同时,能对其生成的Cell,加上注解,类似于下面的。借花献佛,我就用ApachePOI官方提供的例子,然后加上一些注解,给大家看一下。");comment0.setString;comment0.setAuthor;cell0.setCellComment;//7.创建一个单元格Cellcell1=sheet.createRow.createCell;//7.1.对这个单元格设置值cell1.setCellValue;//7.2.对这个单元格加上注解Commentcomment1=drawing.createCellComment;RichTextStringstr1=factory.createRichTextString("Hello,World!

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

如何用Apache POI操作Excel文件-----如何对一个单元格加注解?第1张

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

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

  1. importorg.apache.poi.ss.usermodel.*;
  2. importorg.apache.poi.xssf.usermodel.XSSFSheet;
  3. importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
  4. importjava.io.IOException;
  5. importjava.io.FileOutputStream;
  6. /**
  7. *Demonstrateshowtoworkwithexcelcellcomments.
  8. *<p>
  9. *Excelcommentisakindofatextshape,
  10. *soinsertingacommentisverysimilartoplacingatextboxinaworksheet
  11. *</p>
  12. *
  13. *@authorYegorKozlov
  14. */
  15. publicclassCellComments{
  16. publicstaticvoidmain(String[]args)throwsIOException{
  17. //1.创建一个工作簿对象
  18. XSSFWorkbookwb=newXSSFWorkbook();
  19. //2.得到一个POI的工具类
  20. CreationHelperfactory=wb.getCreationHelper();
  21. //3.创建一个工作表
  22. XSSFSheetsheet=wb.createSheet();
  23. //4.得到一个换图的对象
  24. Drawingdrawing=sheet.createDrawingPatriarch();
  25. //5.ClientAnchor是附属在WorkSheet上的一个对象,其固定在一个单元格的左上角和右下角.
  26. ClientAnchoranchor=factory.createClientAnchor();
  27. //6.创建一个单元格(2A单元格)
  28. Cellcell0=sheet.createRow(1).createCell(0);
  29. //6.1.对这个单元格设置值
  30. cell0.setCellValue("Test");
  31. //6.2.对这个单元格加上注解
  32. Commentcomment0=drawing.createCellComment(anchor);
  33. RichTextStringstr0=factory.createRichTextString("Hello,World!");
  34. comment0.setString(str0);
  35. comment0.setAuthor("ApachePOI");
  36. cell0.setCellComment(comment0);
  37. //7.创建一个单元格(4F单元格)
  38. Cellcell1=sheet.createRow(3).createCell(5);
  39. //7.1.对这个单元格设置值
  40. cell1.setCellValue("F4");
  41. //7.2.对这个单元格加上注解
  42. Commentcomment1=drawing.createCellComment(anchor);
  43. RichTextStringstr1=factory.createRichTextString("Hello,World!");
  44. comment1.setString(str1);
  45. comment1.setAuthor("ApachePOI");
  46. cell1.setCellComment(comment1);
  47. //8.创建一个单元格(4F单元格)
  48. Cellcell2=sheet.createRow(2).createCell(2);
  49. cell2.setCellValue("C3");
  50. Commentcomment2=drawing.createCellComment(anchor);
  51. RichTextStringstr2=factory.createRichTextString("XSSFcansetcellcomments");
  52. //9。为注解设置字体
  53. Fontfont=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("ApachePOI");
  61. comment2.setColumn(2);
  62. comment2.setRow(2);
  63. //10.保存成Excel文件
  64. Stringfname="comments.xlsx";
  65. FileOutputStreamout=newFileOutputStream(fname);
  66. wb.write(out);
  67. out.close();
  68. }
  69. }

免责声明:文章转载自《如何用Apache POI操作Excel文件-----如何对一个单元格加注解?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇setTag()与getTag()的使用介绍[Linux] Linux C编程一站式学习 Part.1下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

行,列,单元格颜色设定

在ALV的开发过程中经常需要给行,列,单元格设置不同的颜色,用来帮助用户区分一些意外或重要的信息,比如某些物料缺少库存,某订单的状态是未清之类的。本文就着重介绍如何给SALV设置颜色。 颜色设定的有以下三种: 1,单元格颜色 为特定的单元格设置颜色,这需要在ALV输出内表中添加一个专门保存颜色的字段,类型为lvc_t_scol,设置完颜色后(包括列名字,...

CSS实现横向滑动

html <div class="header1"> <div class="header-nei"> <div>第一场</div> <div>第二场</div> <div>第三场</div> <div>第四场</div>...

NPOI 自定义单元格背景颜色-Excel

NPOI针对office2003使用HSSFWorkbook,对于offce2007及以上使用XSSFWorkbook;今天我以HSSFWorkbook自定义颜色为例说明,Office2007的未研究呢 在NPOI中默认的颜色类是HSSFColor,它内置的颜色有几十种供我们选择,如果不够怎么办,不能修改底层的HSSFColor类; 大概解决思路: 1、将...

WritableWorkbook 详细用例 (转)

原文出处:http://lz881228.blog.163.com/blog/static/114197324201341755951817/     java 操作 Excel 最常用的就是JXL(java excel api)和POI,今先看下JXL吧。首先可以到http://www.andykhan.com/jexcelapi/download.ht...

【Apache】在Apache中利用ServerAlias设置虚拟主机接收多个域名和设置域名泛解析

ServerAlias:服务器别名,在Apache中可以用于设置虚拟主机接收到个域名,也可以用于接收泛解析的域名。具体的设置方法如下: 一、用于设置虚拟主机接收多个域名 一个虚拟主机常常会接收多个域名解析,比如:一个虚拟主机要同时介绍doctor-c.net, doctor-c.com两个域名,或者是两个二级域名,如:www.doctor-c.net, w...

Apache Solr配置

Solr配置 Solr的主要功能是全文检索,该功能分为两个过程:创建索引和对索引进行搜索; 在创建索引之前,需要重点关注两个配置文件:SOLR_HOME/collection1/conf/schema.xml(定义Document的结构类似定义DB的表结构) & solrconfig.xml(solr运行配置如请求如何被处理);在Solr创建索引...