时间:2021-07-01 10:21:17 帮助过:3人阅读
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
</dependencies>
(另:ojdbc的jar包由于版权问题无法直接引入,需要手动下载本次用的是ojdbc7.jar。)
2.文件(将1.xlsx(数据如下图)的数据 插入到 表a(deptno int,dname varchar,loc varchar))
DEPTNO | DNAME | LOC |
10 | ACCU | NEWYORK |
20 | BB | BB |
30 | VV | VV |
40 | CC | CC |
3.代码部分
package exceltest; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; 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 ExeclOperate { //获取数据库连接 public Connection conn(){ try { //第一步:加载JDBC驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //第二步:创建数据库连接 Connection con =DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger"); return con; }catch(ClassNotFoundException cnf){ System.out.println("driver not find:"+cnf); return null; }catch(SQLException sqle){ System.out.println("can‘t connection db:"+sqle); return null; } catch (Exception e) { System.out.println("Failed to load JDBC/ODBC driver."); return null; } } public void getExcel() throws Exception { InputStream is = new FileInputStream(new File("D:\\1.xlsx")); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); // 获取每一个工作薄 for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // 获取当前工作薄的每一行 for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) { //读取第一列数据 String a = getValue(xssfRow.getCell(0)); Integer one = Integer.parseInt(a.substring(0,a.indexOf("."))); //读取第二列数据 String two = getValue(xssfRow.getCell(1)); //读取第三列数据 String three = getValue(xssfRow.getCell(2)); String insert="insert into a values("+one+",‘"+two+"‘,‘"+three+"‘)"; System.out.println("SQL:"+insert); insert(insert); } } } } //转换数据格式 private String getValue(XSSFCell xssfRow) { if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) { return String.valueOf(xssfRow.getBooleanCellValue()); } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) { return String.valueOf(xssfRow.getNumericCellValue()); } else { return String.valueOf(xssfRow.getStringCellValue()); } } //添加数据 public int insert(String insert) throws SQLException{ Connection conn = this.conn(); int re = 0; try{ conn.setAutoCommit(false);//事务开始 Statement sm = conn.createStatement(); re = sm.executeUpdate(insert); if(re < 0){ //插入失败 conn.rollback(); //回滚 sm.close(); conn.close(); return re; } conn.commit(); //插入正常 sm.close(); conn.close(); return re; } catch(Exception e){ e.printStackTrace(); } conn.close(); return 0; } //测试 public static void main(String[] args) throws Exception { ExeclOperate e=new ExeclOperate(); e.getExcel(); System.out.println("导入完成!"); } }
本文出自 “cw” 博客,请务必保留此出处http://cw666.blog.51cto.com/12488150/1946379
java代码导入excel数据至oracle
标签:java excel