1.引jar包jxl.jar(放在lib路径下面),这个包网上自己下载哈

2.读取execl中表数据的代码
package test;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import Util.JDBC;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
* excel数据导入到oracle
*
* @author de
*/
public class excelToOracle {
static Object[] myArray = new Object[100];
JDBC jdbc = new JDBC();
// ‘table表名数
static int type = 0;
// ‘获取TABLE值
String tableValue = "";
public static void main(String[] args) throws Exception {
// ‘execl文件名 (本博客测试路径:execl文件路径 E:\oracleDB\testDB.xls)
System.out.println("请输入文件名(testDB):");
Scanner fileName = new Scanner(System.in);
// ‘获取文件名
String excelName = fileName.nextLine();
// ‘拼接文件路径
String file = "E:\\oracleDB\\" + excelName + ".xls";
System.out.println("file: " + file);
excelToOracle in = new excelToOracle();
// ‘excel文件路径,数据库表名
in.insert(file, "dbName");
}
/**
*
* @param path 要解析的excel文件路径
* @param dataTable 要写入到数据库中的表名
* @throws BiffException
* @throws IOException
*/
public void insert(String path, String dataTable) throws BiffException, IOException {
File file = new File(path);
// ‘创建新的Excel 工作簿
Workbook rwb = null;
// ‘读取execl文件名
rwb = Workbook.getWorkbook(file);
// ‘得到工作簿中的第一个表索引即为excel下的sheet1,sheet2,sheet3...
Sheet sheet = rwb.getSheets()[0];
// ‘得到所有的行
int rsRows = sheet.getRows();
// ‘每个单元格中的数据
String simNumber = "";
// ‘所有的列数
int rsColumns = 0;
// ‘表最大的列数
int columnsMax = 0;
// ‘最大行数
int rsRowsMax = 0;
// ‘sql
String sql;
// ‘获取end值
String endValue = "";
String columnValue = "";
String colnumValue = "";
String firstColumnDb = "";
// ‘循环 获取END
for (int m = 0; m < rsRows; m++) {
// ‘获取END
Cell cell2 = sheet.getCell(0, m);
endValue = cell2.getContents();
// System.out.println("endValue: " + endValue);
// ‘获取END的位置
if (endValue.equals("END")) {
// ‘获取END所在的行数
rsRowsMax = m;
}
}
// ‘rsRowsMax 循环
for (int n = 0; n < rsRowsMax; n++) {
// ‘获取表 TABLE的位置
Cell cell3 = sheet.getCell(0, n);
tableValue = cell3.getContents();
// ‘判断 table
if (tableValue.equals("TABLE")) {
System.out.println("tableNUM: " + n);
// ‘获取表名
Cell dataTableCell = sheet.getCell(1, n);
dataTable = dataTableCell.getContents();
// ‘获取表名 insert之前 删除表中所有数据
deleteTable(dataTable);
// ‘获取表COLUMN
Cell columnCell = sheet.getCell(0, n + 1);
columnValue = columnCell.getContents();
// ‘判断 值是否为COLUMN
if (columnValue.equals("COLUMN")) {
// ‘得到所有的列
rsColumns = sheet.getColumns();
System.out.println("rsColumns列数: " + rsColumns);
// ‘循环当前表的列数
for (int ColNum = 0; ColNum < rsColumns; ColNum++) {
// ‘获取当前表的字段
Cell colnumCell = sheet.getCell(ColNum, n + 1);
colnumValue = colnumCell.getContents();
if (colnumValue != "") {
// ‘获取当前表最大的列数赋给columnsMax
columnsMax = ColNum;
} else {
break;
}
}
}
// ‘拼接要插入的列
String str = "";
// ‘循环列数获取表头字段
for (int j = 1; j <= columnsMax; j++) {
// ‘获取表头字段
Cell cell = sheet.getCell(j, n + 1);
simNumber = cell.getContents();
// ‘拼接表头字段
if (j == columnsMax) {
str += simNumber;
} else {
str += simNumber + ",";
}
}
// ‘循环行数获取表数据
for (n = n + 3; n < rsRowsMax; n++) {
// ‘获取当前表第一列数据
Cell firstCell = sheet.getCell(0, n);
firstColumnDb = firstCell.getContents();
System.out.println("n行数 : " + n);
System.out.println("firstColumnDb : " + firstColumnDb);
// ‘判断第一列数据 是否为null 且 为 end
if (firstColumnDb != "" && !firstColumnDb.equals(constants.end)) {
// ‘拼接sql
sql = "insert into " + dataTable + "(" + str + ") values(";
System.out.println("str: " + str);
// ‘循环行数获取数据
for (int j = 1; j <= columnsMax; j++) {
Cell cell = sheet.getCell(j, n);
simNumber = cell.getContents();
System.out.println("simNumber : " + simNumber);
// ‘拼接表数据
if (j == columnsMax) {
sql += "'" + simNumber + "'";
} else {
sql += "'" + simNumber + "',";
}
}
} else {
break;
}
sql += " )";
// ‘执行sql
jdbc.executeUpdate(sql);
}
jdbc.closeStmt();
jdbc.closeConnection();
}
}
}
// ‘删除数据
public void deleteTable(String dataTable) {
// ‘删除sql
String delSql = "delete from " + dataTable;
System.out.println(delSql);
jdbc.executeUpdate(delSql);
}
}
3.数据库连接的工具类
package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
/** Oracle数据库连接 URL */
private final static String DB_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
/** Oracle数据库连接驱动 */
private final static String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
/** 数据库用户名 */
private final static String DB_USERNAME = "root";
/** 数据库密码 */
private final static String DB_PASSWORD = "root123";
/**
* 获取数据库连接
*
* @return
*/
public Connection getConnection() {
/** 声明Connection连接对象 */
Connection conn = null;
try {
/** 使用 Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它 */
Class.forName(DB_DRIVER);
/** 通过 DriverManager的getConnection()方法获取数据库连接 */
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
// System.out.println("数据库连接成功!");
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
/**
* 查询数据部分
*
* @return ResultSet
*/
public ResultSet executeQuery(String sqlStr) {
if (sqlStr == null || sqlStr.length() == 0)
return null;
try {
this.getConnection();
rs = stmt.executeQuery(sqlStr);
return rs;
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
}
/**
* 更新数据部分
*
* @return 更新是否成功
*/
public boolean executeUpdate(String sqlStr) {
if (sqlStr == null || sqlStr.length() == 0)
return false;
try {
this.getConnection();
stmt.executeUpdate(sqlStr);
return true;
} catch (SQLException ex) {
ex.printStackTrace();
return false;
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeStmt() {
try {
if (stmt != null) {
stmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭数据库连接
*
* @param connect
*/
public void closeConnection() {
try {
if (conn != null) {
/** 判断当前连接连接对象如果没有被关闭就调用关闭方法 */
if (!conn.isClosed()) {
conn.close();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
4.代码没问题右击运行程序,在控制台输入EXECL文件名,回车。数据导入Oracle数据库

本文如有问题请多多指教
此方法只适合【.xls】的格式文件。