標籤:java excel
本文處理的excel格式為xlsx:
1.建立maven項目
<dependencies>
<dependency>
<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