您的位置:首页 > 数据库 > Oracle

java代码导入excel数据至oracle(poi方式)

2017-07-11 16:26 525 查看
本文处理的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;
}
}
//读取excel
public void getExcel() throws Exception {
InputStream is = new FileInputStream(new File("D:\\1.xlsx"));
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
// 获取每一个工作薄(sheet)
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("导入完成!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java excel