zoukankan      html  css  js  c++  java
  • 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

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

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

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

    [java] view plain copy
     
    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. /** 
    8.  * Demonstrates how to work with excel cell comments. 
    9.  * <p> 
    10.  * Excel comment is a kind of a text shape, 
    11.  * so inserting a comment is very similar to placing a text box in a worksheet 
    12.  * </p> 
    13.  * 
    14.  * @author Yegor Kozlov 
    15.  */  
    16. public class CellComments {  
    17.     public static void main(String[] args) throws IOException {  
    18.         //1.创建一个工作簿对象  
    19.         XSSFWorkbook wb = new XSSFWorkbook();  
    20.   
    21.         //2.得到一个POI的工具类  
    22.         CreationHelper factory = wb.getCreationHelper();  
    23.   
    24.         //3. 创建一个工作表  
    25.         XSSFSheet sheet = wb.createSheet();  
    26.           
    27.         //4.得到一个换图的对象  
    28.         Drawing drawing = sheet.createDrawingPatriarch();  
    29.         //5. ClientAnchor是附属在WorkSheet上的一个对象,  其固定在一个单元格的左上角和右下角.  
    30.         ClientAnchor anchor = factory.createClientAnchor();  
    31.           
    32.         //6. 创建一个单元格(2A单元格)  
    33.         Cell cell0 = sheet.createRow(1).createCell(0);  
    34.         //6.1. 对这个单元格设置值  
    35.         cell0.setCellValue("Test");  
    36.         //6.2. 对这个单元格加上注解  
    37.         Comment comment0 = drawing.createCellComment(anchor);  
    38.         RichTextString str0 = factory.createRichTextString("Hello, World!");  
    39.         comment0.setString(str0);  
    40.         comment0.setAuthor("Apache POI");  
    41.         cell0.setCellComment(comment0);  
    42.           
    43.         //7. 创建一个单元格(4F单元格)  
    44.         Cell cell1 = sheet.createRow(3).createCell(5);  
    45.         //7.1. 对这个单元格设置值  
    46.         cell1.setCellValue("F4");  
    47.         //7.2. 对这个单元格加上注解  
    48.         Comment comment1 = drawing.createCellComment(anchor);  
    49.         RichTextString str1 = factory.createRichTextString("Hello, World!");  
    50.         comment1.setString(str1);  
    51.         comment1.setAuthor("Apache POI");  
    52.         cell1.setCellComment(comment1);  
    53.   
    54.         //8. 创建一个单元格(4F单元格)  
    55.         Cell cell2 = sheet.createRow(2).createCell(2);  
    56.         cell2.setCellValue("C3");  
    57.   
    58.         Comment comment2 = drawing.createCellComment(anchor);  
    59.         RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");  
    60.         //9。为注解设置字体  
    61.         Font font = wb.createFont();  
    62.         font.setFontName("Arial");  
    63.         font.setFontHeightInPoints((short)14);  
    64.         font.setBoldweight(Font.BOLDWEIGHT_BOLD);  
    65.         font.setColor(IndexedColors.RED.getIndex());  
    66.         str2.applyFont(font);  
    67.   
    68.         comment2.setString(str2);  
    69.         comment2.setAuthor("Apache POI");  
    70.         comment2.setColumn(2);  
    71.         comment2.setRow(2);  
    72.         //10. 保存成Excel文件  
    73.         String fname = "comments.xlsx";  
    74.         FileOutputStream out = new FileOutputStream(fname);  
    75.         wb.write(out);  
    76.         out.close();  
    77.   
    78.     }  
    79. }  
  • 相关阅读:
    VS2005快捷键(转)
    codeSmish使用《转》
    WinForm TextBox数据绑定
    NetTiers抛出"Unable To Load NetTiersServiceSection“的异常
    DELPHi第三方控件使用方法(摘录)
    遠程連接操作
    不同服务器数据库之间的数据操作
    delphi 关闭 MDI 子窗体
    VSS使用手册(转)
    delphi 快捷键
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5787050.html
Copyright © 2011-2022 走看看