如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的數據?


在POI的第一節入門中,我們提供了兩個簡單的例子,一個是如何用Apache POI新建一個工作薄,另外一個例子是,如果用Apache POI新建一個工作表。那么在這個章節里面,我將會給大家演示一下,如何用Apache POI在已有的Excel文件中插入一行新的數據。具體代碼,請看下面的例子。

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM