java中解析excel 批量插入数据库

Facade 层 实现类 (@Service("samePeriodModelImportFacade"))

1、  获取cells 的方法

public Cells getCells(File file){

       Cells cells = null;

       License license = new License();

     InputStream licenseStream = this.getClass().getResourceAsStream("/aspose.lic");

try{

    license.setLicense(licenseStream);

   FileInputStream fr = new FileInputStream(file);
   Workbook wb = new Workbook(fr);
   Worksheet worksheet = wb.getWorksheets().get(0);
   cells = worksheet.getCells();

}catch(Exception e){

  e.printStackTrace();

扫描二维码关注公众号,回复: 5507057 查看本文章

}

return cells;

}

2、解析excel 的方法

public String analyExcel(File file){

String dis_no=null;

String dis_mp=null;

List<Object[ ]>  insertParamList =new ArrayList<Object[ ]>();

  Cells cells = this.getCells( file);

  for(int i=1;i<cells.getMaxDataRow()+1;i++){         // 获取要数据的最大的行数

   for(int j=0; j<35;j++){        //获取列数   例如35列

      String cellsValue = cells.getRows().get(i).get(j).getStringValue();  // 获取的每一个单元格的数据  返回的是String 类型

    if(j==0){

        dis_no =cellsValue;   //  i=1 j=0

 }

if( j==23){

  dis_mp=cellsValue;   // i =1 j=23

  }

 }

 insertParamList.add(new Object[] {dis_no,dis_mp});

 

}

 

samePeriodModelImportDomainService.insertPspCellModel(insertParamList);

 

return "success";

}

service层实现类@Repository("samePeriodModelImportDomainService")

//批量插入数据库

public void insertPspCellModel(List<Object[]> insertParamList) {

String sql = "insert into psp_model(dis_no,dis_mp) values(?,?)";

this.getJdbcTemplate().batchUpdate(sql, insertCellParamList);

}

猜你喜欢

转载自www.cnblogs.com/chengxuyuanIng/p/10515595.html