Excel导入数据库工具包 | efly 原创 (参与分:724,专家分:1000) 发表:2002-11-1 下午4:11 更新:2002-11-4 上午9:36 版本:1.0 阅读:5276次 |
|
设计目的 使用该工具包,能够将MS Excel表格数据导入到具有相应结构的关系型数据库表格。通过扩展接口能对Excel表格的每一列或行的数据进行操作,剔除坏数据,修复或者替换特定单元的数据,从而实现了Excel数据自由导入具有外键关联或引用的表格。该工具包在导入数据的同时还能生成结构简单的Xml数据文件。
类结构
 使用说明 扩展接口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:\work4\9-23\expert.xml F:\work4\9-23\_expert2.xml com.uncnet.dbexcel.flopper.YpzbExpertFp oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@127.0.0.1:1521:oracle scott tiger F:\work4\9-23\data\diploma.txt F:\work4\9-23\data\hospital.txt |
|
转载于:https://www.cnblogs.com/sunsonbaby/archive/2004/10/22/55699.html