[转载]Excel导入数据库工具包

Excel导入数据库工具包
设计目的 使用该工具包,能够将MS Excel表格数据导入到具有相应结构的关系型数据库表格。通过扩展接 口能对Excel表格的每一列或行的数据进行操作,剔除坏数据,修复或者替换特定单元的数据,从而实现了Excel数据自由导入具有外键关联或引用的表 格。该工具包在导入数据的同时还能生成结构简单的Xml数据文件。

类结构
dbexcel.jpg

使用说明
扩展接口com.uncnet.dbexcel.Flopper,实现相应的方法
方法:String conversionOp(
Connection connection,
String arg,
int index,
Document doc,
Element table,
Element row)
throws Exception;
描述:对Excel列的单元进行数据转换并操作数据库表格。
参数:Connection connection,数据库连接;
String arg,Excel列单元的数据;
int index,Excel表格列序号,从1开始;
Document doc,UNC Xml的Document元素;
Element table,UNC Xml的元素;
Element row,UNC Xml的元素。
返回:经过从Excel Xml到UNC Xml转换的数据。
代码示例:
/**
* @see com.uncnet.dbexcel.Flopper#conversionOp(Connection, String, int, Document, Element, Element)
*/
public String conversionOp(
Connection connection,
String arg,
int index,
Document doc,
Element table,
Element row) {
String text = null;
if (arg == null)
arg = "";
try {
if (connection == null)
return "No Connection";
stmt = connection.createStatement();
if (col_num++ % COLUMNS == 0)
ps_in = new String[COLUMNS + 1];

switch (index) {
case 1 :
{
text = arg;
ps_in[0] = arg;
break;
}
case 8 :
{
text = arg;
ps_in[8] = findForDict(null, arg, stmt, 8);
break;
}
case 10 :
{
text = arg;
if (!arg.equals(""))
ps_in[10] = arg.substring(1);
else
ps_in[10] = arg;
break;
}
default :
{
text = arg;
ps_in[index] = arg;
}
}
if (col_num % COLUMNS == 0)
isToCmt = true;
else
isToCmt = false;
if (isToCmt) {
pstmt = connection.prepareStatement(sqlscript);
connection.setAutoCommit(false);
row_num++;
boolean isToExe = true;
try {
Integer.parseInt(ps_in[0]);
Integer.parseInt(ps_in[1]);
} catch (NumberFormatException nfe) {
isToExe = false;
}
StringTokenizer ster = null;
Date date = null;
int year, month, day;
for (int i = 0; i < COLUMNS + 1; i++) {
if (i == 12 || i == 13) {
ster = new StringTokenizer(ps_in[i], "-");
year = Integer.parseInt(ster.nextToken());
month = Integer.parseInt(ster.nextToken());
day = Integer.parseInt(ster.nextToken());
date = new Date(year, month, day);
pstmt.setDate(i + 1, date);
} else
pstmt.setString(i + 1, ps_in[i]);
System.err.print(ps_in[i] + " ");
}
System.err.println();
if (isToExe) {
pstmt.execute();
connection.commit();
} else
System.err.print("Encountering null.");
connection.setAutoCommit(true);
if (pstmt != null)
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
try {
if (stmt != null)
stmt.close();
if (isToCmt && pstmt != null)
pstmt.close();
} catch (Exception e) {
System.err.println(e);
}
}
if (text == null) {
table.removeChild(row);
}

return text;
}

方法:void setParameter(String[] para);
描述:设置程序参数
参数:String[] para

方法:Connection getConnection();
描述:建立数据库连接
返回:数据库连接

方法:void closeConnection(Connection conn);
描述:关闭数据库连接
参数:Connection conn,数据库连接

方法:void beginConvert();
描述:开始转换前的操作

方法:void endOfConvert();
描述:转换完成后的操作

方法:void afterConnected(Connection conn);
描述:数据库建立连接后的操作

方法:void beforeClosed(Connection conn);
描述:关闭数据库连接前的操作

接口Flopper扩展类
类:com.uncnet.dbexcel.flopper.DBFlopper
描述:实现了数据库的连接与关闭和参数的设置

类:com.uncnet.dbexcel.flopper.DictFlopper
描述:字典类,实现了从文件查找字典表的对应数据

程序的执行
主程序入口:com.uncnet.dbexcel.TextController的main方法
调用参数:Excel Xml文件路径,输出UNC Xml文件路径,Flopper扩展类名字空间,其它扩展参数...
参数示例:
F:work49-23expert.xml F:work49-23\_expert2.xml com.uncnet.dbexcel.flopper.YpzbExpertFp oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@127.0.0.1:1521:oracle scott tiger F:work49-23datadiploma.txt F:work49-23datahospital.txt

附件
工具包

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-132035/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-132035/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个将excel文件导入到SQLServer表中的程序 一 双击Input.exe运行程序,将弹出一个窗口,这时请你在"数据库名"后面的 输入栏中输入数据库名(如果是千方百剂就是输入帐套名).你如果没对数 据库的登录进行特殊修改的话,那"用户名和密码"就没必要修改了. 二 填好以上输入框后,真接单击"连接数据库",如果连接成功,将弹出"数据库 连接成功,你现在可以导入数据"的对话框,你按"OK"后将弹出新的一个数据 导入的窗口. 三 在这个窗口上单击"打开EXCEL文件"按钮,然后选择你要导入Excel文件, 按打开(这时如果你数据比较多的话你可能要多等一会儿时间),之后就弹 出一个让你选择Excel工作区的窗口,你可以在下拉框中选择你数据所在的 Excel工作区了.选完以后按确定,你可以看到你Excel里的数据已经在"Excel 数据信息"里面了.而且还可以看到多了一列"不导入"的选项了.你如果哪一行 的数据不导入的话你可以打勾,这一行将不被导入. 四 做完以上三步后,请在"表名"后面的下拉框中选择你所要导入的表的名称. 选完后,你得到"数据转换信息如下"这一栏配置数据转换的对应关系. 五 双击Excel字段处从下拉框中选择excel的列,双击表字段处从下拉框中选 择SQL表的列,然后看这列是否是"关键字",是的话打勾,不是不打勾.选择 完第一行后,就按方向键的向下键,继续第二行的选择,直到配置完Excel列 和表字段的对应关系为止. 六 按"导入数据"按钮系统会自动将页面转到"转换信息"这一页面.你将可以看 到第几行导入成功,或第几行导入失改的信息.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值