java 读取excel存入数据库
2018-03-20 11:34
288 查看
package com.model; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import com.jfinal.plugin.activerecord.Db; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class PoiDemo { public String excelPath = "D:\\Users\\user\\Desktop\\piodemo.xls"; public void poimothed() throws FileNotFoundException{ try { //String encoding = "GBK"; File excel = new File(excelPath); if (excel.isFile() && excel.exists()) { //判断文件是否存在 String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!! Workbook wb=null; //根据文件后缀(xls/xlsx)进行判断 if ( "xls".equals(split[1])){ FileInputStream fis = new FileInputStream(excel); //文件流对象 wb = new HSSFWorkbook(fis); }else if ("xlsx".equals(split[1])){ }else { System.out.println("文件类型错误!"); return; } //开始解析 Sheet sheet = wb.getSheetAt(0); //读取sheet 0 int firstRowIndex = sheet.getFirstRowNum()+2; //第一行是列名,所以不读 int lastRowIndex = sheet.getLastRowNum(); System.out.println("firstRowIndex: "+firstRowIndex); System.out.println("lastRowIndex: "+lastRowIndex); String consolation_leader=null; String grouping_company=null; String accompany_leader=null; String liaison_man=null; String contacts_man=null; for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行 System.out.println("rIndex: " + rIndex); Row row = sheet.getRow(rIndex); if (row != null) { int firstCellIndex = row.getFirstCellNum(); int lastCellIndex = row.getLastCellNum(); String consolation_level=null; String name=null; String sex=null; String birthday=null; String join_party_time=null; String grouping_Basics=null; String address=null; String lntroduction=null; for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) { //遍历列 Cell cell = row.getCell(cIndex); if (cell != null&&!"".equals(cell.toString().trim())) { switch(cIndex) { case 1: consolation_leader=cell.toString(); System.out.println("1:"+consolation_leader); break; case 2: consolation_level=cell.toString(); System.out.println("2:"+consolation_level); break; case 3: 4000 name=cell.toString(); System.out.println("3:"+name); break; case 4: sex=cell.toString(); System.out.println("4:"+sex); break; case 5: birthday=cell.toString(); System.out.println("5:"+birthday); break; case 6: join_party_time=cell.toString(); System.out.println("6:"+join_party_time); break; case 7: grouping_Basics=cell.toString(); System.out.println("7:"+grouping_Basics); break; case 8: address=cell.toString(); System.out.println("8:"+address); break; case 9: lntroduction=cell.toString(); System.out.println("9:"+lntroduction); break; case 10: grouping_company=cell.toString(); System.out.println("10:"+grouping_company); break; case 11: accompany_leader=cell.toString(); System.out.println("11:"+accompany_leader); break; case 12: liaison_man=cell.toString(); System.out.println("12:"+liaison_man); break; case 13: contacts_man=cell.toString(); System.out.println("13:"+contacts_man); break; default: break; } } } insertMethod(consolation_leader,consolation_level,name,sex,birthday,join_party_time,grouping_Basics,address,lntroduction,grouping_company,accompany_leader,liaison_man,contacts_man); } } } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { e.printStackTrace(); } } public void insertMethod(String consolation_leader,String consolation_level,String name,String sex,String birthday,String join_party_time,String grouping_Basics,String address,String lntroduction,String grouping_company,String accompany_leader,String liaison_man,String contacts_man){ String sql="insert into condolences(consolation_leader,consolation_level,name,sex,birthday,join_party_time,grouping_Basics,address,lntroduction,grouping_company,accompany_leader,liaison_man,contacts_man) values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; Db.update(sql,consolation_leader,consolation_level,name,sex,birthday,join_party_time,grouping_Basics,address,lntroduction,grouping_company,accompany_leader,liaison_man,contacts_man); } }
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
相关文章推荐
- Java中上传excel文件并在服务器端读取存入数据库
- Java中上传excel文件并在服务器端读取存入数据库
- java获取excel中数据并存入数据库表中
- java 读取world的图片 并把图片路径存入数据库
- ava中上传excel文件并在服务器端读取存入数据库
- java读取mongoDB某个数据库的集合的指定字段值,并存入mysql数据库指定表中
- Java通过apache poi 读取excel(.xlsx)文件,并通过MyBbtis插入数据库中
- C#:c#读取Excel并显示出来,然后存入数据库.
- 用java将数据库表中内容读取出来并存入txt中
- java如何读取Excel并存入list中时去除重复记录
- java连接Excel数据库读取,写入,操纵Excel表格
- java 对excel操作 读取、写入、修改数据;导出数据库数据到excel
- java下载excel导入excel读取excel数据,将数据插入数据库
- C# 读取EXCEL中的数据并存入SQL Server 2005数据库
- Office编程(二)C#读取Excel并存入数据库,通过XML自定义表名,是否重建并插入数据
- android 读取excel表格数据,并存入数据库
- c#读取Excel并显示出来,然后存入数据库.
- Java读取Excel文件并写入数据库
- java读取数据库字段和值,输出到regedit,xml,text,excel,pdf的程序
- java 读取文件夹并存入数据库(TXT)