利用Apache POI在已有的Excel文件中插入一行新的数据

在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表。那么在这个章节里面,我将会给大家演示一下,如何用Apache POI在已有的Excel文件中插入一行新的数据。具体代码,请看下面的例子。

  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((short0);  
  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. }  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CreatRowTest {
    //当前文件已经存在
    private String excelPath = "D:\\exceltest\\comments.xlsx";
    //从第几行插入进去
    private int insertStartPointer = 3;
    //在当前工作薄的那个工作表单中插入这行数据 
    private String sheetName = "Sheet1";

    /**
     * 总的入口方法
     */
    public static void main(String[] args) {
        CreatRowTest crt = new CreatRowTest();
        crt.insertRows();
    }
    /**
     * 在已有的Excel文件中插入一行新的数据的入口方法
     */
    public void insertRows() {
        XSSFWorkbook wb = returnWorkBookGivenFileHandle();
        XSSFSheet sheet1 = wb.getSheet(sheetName);
        XSSFRow row = createRow(sheet1, insertStartPointer);
        createCell(row);
        saveExcel(wb);

    }
    /**
     * 保存工作薄
     * @param wb
     */
    private void saveExcel(XSSFWorkbook wb) {
        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream(excelPath);
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 创建要出入的行中单元格
     * @param row
     * @return
     */
    private XSSFCell createCell(XSSFRow row) {
        XSSFCell cell = row.createCell((short) 0);
        cell.setCellValue(999999);
        row.createCell(1).setCellValue(1.2);
        row.createCell(2).setCellValue("This is a string cell");
        return cell;
    }
   /**
    * 得到一个已有的工作薄的POI对象
    * @return
    */
    private XSSFWorkbook returnWorkBookGivenFileHandle() {
        XSSFWorkbook wb = null;
        FileInputStream fis = null;
        File f = new File(excelPath);
        try {
            if (f != null) {
                fis = new FileInputStream(f);
                wb = new XSSFWorkbook(fis);
            }
        } catch (Exception e) {
            return null;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return wb;
    }
   /**
    * 找到需要插入的行数,并新建一个POI的row对象
    * @param sheet
    * @param rowIndex
    * @return
    */
    private XSSFRow createRow(XSSFSheet sheet, Integer rowIndex) {
        XSSFRow row = null;
        if (sheet.getRow(rowIndex) != null) {
            int lastRowNo = sheet.getLastRowNum();
            sheet.shiftRows(rowIndex, lastRowNo, 1);
        }
        row = sheet.createRow(rowIndex);
        return row;
    }



}


                </div>
  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值