javajdbc-odbc桥读取excel指导.doc
还剩
7页未读,
继续阅读
下载文档到电脑,马上远离加班熬夜!
亲,喜欢就下载吧,价低环保!
内容要点:
专门操作 EXCEL 文件的 Java API。Jakarta POI 和 Java Excel API 就是其中的两个 Open Source项目。感兴趣的可以从网上搜索一下,应该会有很多关于的资料。 ExcelAccess ea = new ExcelAccess();HashMap res = null;if (!ea.openConnection()) {System.err.println("open connection err.");System.exit(1);}// get emp# 1000's informationSystem.out.println("Get Emp# 1000:");res = ea.getEmpInfo(1000);if (res != null) {Collection c = res.keySet();Iterator i = c.iterator();while (i.hasNext()) {Object key = i.next();Object value = res.get(key);System.out.println(key + "\t" + value);}// while ends}// insert a new record into the tableint ret = ea.insertEmpInfo(1005, "Yanjiang", 'M',"PM", "[email protected]");if (ret == 1) {System.out.println("emp# 1005 inserting success.");}// close the connectionea.closeConnection();}}运行结果:Get Emp# 1000:Email [email protected] MTitle PMName Jesse, G.ID 1000.0emp# 1005 inserting success.总结到此这篇文章就结束了,这个例子实现了 Java读写 Excel文件,类似,我们可以去读写 dBase、Access 文件等。还有就是这个简单的例子充 分说明了当初设计 JDBC,ODBC 等驱动的英明之处。值得一提的是,现在已有了比较成 熟的 return result;}public int insertEmpInfo(int id, String name, char gender, String title, String email) {int result = -1;if (stmnt == null)return result;// generate the query stringString query = "INSERT INTO [Sheet1$] VALUES(";query += id + ", ";query += "'" + name + "', ";query += "'" + gender + "', ";query += "'" + title + "', ";query += "'" + email + "'";query += " )";try {// execute the queryresult = stmnt.executeUpdate(query);} catch (SQLException e) {e.printStackTrace();}return result;}/*** close the connection*/public boolean closeConnection() {try {if (stmnt != null)stmnt.close();i
发表评论
暂无评论,赶快抢占沙发吧。