最近经常有人需求要把我excel 导入生产数据库oracle,其实dba最烦的就是用户从前台页面看上去很简单的数据结构,以为按页面的格式提供excel就可以轻松要求你迅速导入,熟不知页面显示后台所关联好多表时,哪有那么容易,其实导入方法很多,sqlldr、外部表、透明网关或用第三方导入建个临时表
那样做也最少要两步完成不是么?如果还经常性的怎么办呢?以前有人问我怎么学数据库,我个人认为就是用你全部的智慧让复杂的工作简单化,不知道不觉就锻炼了你的水平,昨晚上有时间,捡起只剩下概念的java,最终目标是要实现我这个需求,os下一条command 自动实现,平时太忙,手又生,以下是先实现的第一步,把excel里的数据动态生成sql,有很多不足的地方,希望老鸟指正
———-建一个exceltooracle.java的文件,代码如下—————
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import jxl.*;
public class exceltooracle {
public static void main(String args[]) {
copyright();
if(args.length<1) {
System.out.println("error-001:输入格式错误!");
System.out.println("for Example: java exceltooracle c:\\aa\\dd\\aa.xls");
System.exit(-1);
}
exceltooracle t = new exceltooracle();
String efile = "未初始化";
efile=args[0].toString();
efile=efile.endsWith("xls")?efile:efile+".xls";
t.reade(null, efile);
}
public void reade(String tns, String filepath) {
try {
InputStream is = new FileInputStream(filepath);
jxl.Workbook rwb = Workbook.getWorkbook(is);
int sheets = rwb.getNumberOfSheets();
Sheet rs = rwb.getSheet(0);
String rsname = rs.getName();
int columns = rs.getColumns();
int rows = rs.getRows();
String sqlstr = "";
String c;
System.out.println("info:");
System.out.println("This file:"+filepath+" have " + sheets
+ " sheets,now reloading the first sheet only!");
System.out.println(rsname + " reloading...");
System.out.println("This sheet have " + columns
+ " columns,and have " + rows + " rows");
System.out.println("Create Table sql:");
System.out.println("----------------------------");
sqlstr = "create table anbob (";
for (int j = 0; j < columns; j++) {
sqlstr += "id" + j + " varchar2(100)";
if (j < columns - 1)
sqlstr += ",";
}
sqlstr += ");";
System.out.println(sqlstr);
/*
* for (int i = 1; i < rows; i++) { for (int j = 0; j < columns;
* j++) { System.out.print(rs.getCell(j, i).getContents() + "|"); }
* System.out.println(); }
*/
System.out.println("Insert table sql:");
System.out.println("------------------------------");
for (int i = 1; i < rows; i++) {
sqlstr = "insert into anbob values(";
for (int j = 0; j < columns; j++) {
c = rs.getCell(j, i).getContents();
if ("" == c)
c = "null";
sqlstr += c;
if (j < columns - 1)
sqlstr += ",";
}
sqlstr += ");";
System.out.println(sqlstr);
}
rwb.close();
} catch (FileNotFoundException notfound) {
System.out
.println("error-002:Excel file not exists or not XLS type file !please input a real path");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyright(){
System.out.println("ExcelToOracle java 0.1.0.1"+"\n"+"-------------------------");
System.out.println("Copyright www.anbob.com"+"\n"+"qq:397464815");
}
}
第二步去网上下一个ibm的API,jxl.jar文件很小,这个好像很老,昨开发的同事说可以看看apache 的poi,我是用jxl,ibm官网有下,csdn也有。下载后编译前,要把这个jar加入clashpath否则编绎不通过
比如你下载jxl在D:下,可以加到环境变量里或临时cmd下 >set classpath=d:\jxl.ar;%classpath%
classpath这个开发的应该都知道,不过上手就用eclipse IDE一辈子不通也不怪你
第三步如果机器有jdk,而不是jre,编译java文件 cmd >javac d:\xxxx\exceltooracle.java,如果没有任何提示,恭喜通过了
下面我共享一个我编译好的,下载后只需要设置环境变量有装jre就可操作
cmd>java exceltooracle excelfile路径
如java exceltooracle d:\aa.xls or java exceltooracle d:\aa
提示只支持excel 2003以前版本.toad好像也不支持 2007以后
Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.