1. 程式人生 > >java代碼導入excel數據至oracle

java代碼導入excel數據至oracle

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))

DEPTNODNAMELOC
10
ACCUNEWYORK
20BBBB
30VVVV
40CCCC


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