• 如何用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. }  
  • 相关阅读:
    Codeforces Round #629 (Div. 3) (A ~ F)
    st表
    Educational Codeforces Round 81 (Rated for Div. 2)
    hihocoder#1996 : 01匹配
    P2056 [ZJOI2007]捉迷藏
    P2495 [SDOI2011]消耗战
    GUETOJ1335
    优先队列重载比较运算
    CCF认证201909-4 推荐系统
    P3178 [HAOI2015]树上操作
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5787046.html
Copyright © 2020-2023  润新知