zoukankan      html  css  js  c++  java
  • (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug。 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一行数据,不会有任何问题。但是如果被插入的工作表(sheet)里面的单元格只要包含任何的注解(comment)的时候,这个时候插入一行数据的时候,就会破坏这个文件。当程序执行完后,如果打开被插入数据的Excel文件,我们将会发现,其会弹出下面的对话框。

    程序代码如下,

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.tibco.poi.xssf;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.io.FileNotFoundException;  
    6. import java.io.FileOutputStream;  
    7. import java.io.IOException;  
    8.   
    9. import org.apache.poi.xssf.usermodel.XSSFCell;  
    10. import org.apache.poi.xssf.usermodel.XSSFRow;  
    11. import org.apache.poi.xssf.usermodel.XSSFSheet;  
    12. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
    13.   
    14. public class CreatRowTest {  
    15.     //当前文件已经存在  
    16.     private String excelPath = "D:\exceltest\comments.xlsx";  
    17.     //从第几行插入进去  
    18.     private int insertStartPointer = 3;  
    19.     //在当前工作薄的那个工作表单中插入这行数据   
    20.     private String sheetName = "Sheet1";  
    21.   
    22.     /** 
    23.      * 总的入口方法 
    24.      */  
    25.     public static void main(String[] args) {  
    26.         CreatRowTest crt = new CreatRowTest();  
    27.         crt.insertRows();  
    28.     }  
    29.     /** 
    30.      * 在已有的Excel文件中插入一行新的数据的入口方法 
    31.      */  
    32.     public void insertRows() {  
    33.         XSSFWorkbook wb = returnWorkBookGivenFileHandle();  
    34.         XSSFSheet sheet1 = wb.getSheet(sheetName);  
    35.         XSSFRow row = createRow(sheet1, insertStartPointer);  
    36.         createCell(row);  
    37.         saveExcel(wb);  
    38.   
    39.     }  
    40.     /** 
    41.      * 保存工作薄 
    42.      * @param wb 
    43.      */  
    44.     private void saveExcel(XSSFWorkbook wb) {  
    45.         FileOutputStream fileOut;  
    46.         try {  
    47.             fileOut = new FileOutputStream(excelPath);  
    48.             wb.write(fileOut);  
    49.             fileOut.close();  
    50.         } catch (FileNotFoundException e) {  
    51.             e.printStackTrace();  
    52.         } catch (IOException e) {  
    53.             e.printStackTrace();  
    54.         }  
    55.   
    56.     }  
    57.     /** 
    58.      * 创建要出入的行中单元格 
    59.      * @param row 
    60.      * @return 
    61.      */  
    62.     private XSSFCell createCell(XSSFRow row) {  
    63.         XSSFCell cell = row.createCell((short) 0);  
    64.         cell.setCellValue(999999);  
    65.         row.createCell(1).setCellValue(1.2);  
    66.         row.createCell(2).setCellValue("This is a string cell");  
    67.         return cell;  
    68.     }  
    69.    /** 
    70.     * 得到一个已有的工作薄的POI对象 
    71.     * @return 
    72.     */  
    73.     private XSSFWorkbook returnWorkBookGivenFileHandle() {  
    74.         XSSFWorkbook wb = null;  
    75.         FileInputStream fis = null;  
    76.         File f = new File(excelPath);  
    77.         try {  
    78.             if (f != null) {  
    79.                 fis = new FileInputStream(f);  
    80.                 wb = new XSSFWorkbook(fis);  
    81.             }  
    82.         } catch (Exception e) {  
    83.             return null;  
    84.         } finally {  
    85.             if (fis != null) {  
    86.                 try {  
    87.                     fis.close();  
    88.                 } catch (IOException e) {  
    89.                     e.printStackTrace();  
    90.                 }  
    91.             }  
    92.         }  
    93.         return wb;  
    94.     }  
    95.    /** 
    96.     * 找到需要插入的行数,并新建一个POI的row对象 
    97.     * @param sheet 
    98.     * @param rowIndex 
    99.     * @return 
    100.     */  
    101.     private XSSFRow createRow(XSSFSheet sheet, Integer rowIndex) {  
    102.         XSSFRow row = null;  
    103.         if (sheet.getRow(rowIndex) != null) {  
    104.             int lastRowNo = sheet.getLastRowNum();  
    105.             sheet.shiftRows(rowIndex, lastRowNo, 1);  
    106.         }  
    107.         row = sheet.createRow(rowIndex);  
    108.         return row;  
    109.     }  
    110.   
    111.       
    112.   
    113. }  


    不过,值得高兴的是,这个bug已经在POI-3.12的版本解决了

  • 相关阅读:
    Android 开发 深入理解Handler、Looper、Messagequeue 转载
    Android 开发 Handler的基本使用
    Java 学习 注解
    Android 开发 AlarmManager 定时器
    Android 开发 框架系列 百度语音合成
    Android 开发 框架系列 Google的ORM框架 Room
    Android 开发 VectorDrawable 矢量图 (三)矢量图动画
    Android 开发 VectorDrawable 矢量图 (二)了解矢量图属性与绘制
    Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图
    Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5786952.html
Copyright © 2011-2022 走看看