| 发表于:2007-02-05 16:35:32 楼主 |
我的问题是这样的:我想将某个文件夹下的所有excel导进数据库,但是这些原始的excel都是存成了htm格式(excel能够存成该类型),如果要导进数据库,必须先将这些excel另存为xls文件,我想请教java中如何修改文件的类型,即从htm格式改为xls格式 源代码如下: public void importexcel(file dir,string file) throws sqlexception { if (dir.isdirectory()) { string[] s = dir.list(); for (int j = 0; j < s.length; j++) { if (file.equals(s[j])) { connection con = null; callablestatement cstmt = null; resultset rs = null; poifsfilesystem fs = null; hssfworkbook wb = null; try { fs = new poifsfilesystem(new fileinputstream(dir.getpath() + "\\ " + file)); wb = new hssfworkbook(fs); } catch (ioexception e) { e.printstacktrace(); } hssfsheet sheet = wb.getsheetat(0); hssfrow row = null; hssfcell cell = null; string name = " "; string name2 = " "; string name3 = " "; string flag= null; double value; string value2; int rownum, cellnum; int i; rownum = sheet.getlastrownum(); for (i = 0; i <= rownum; i++) { row = sheet.getrow(i); //cellnum = row.getlastcellnum(); cell = row.getcell((short) 0); name2 = cell.getstringcellvalue(); cell = row.getcell((short) 1); name3 = cell.getstringcellvalue(); cell = row.getcell((short) 2); name = cell.getstringcellvalue(); cell = row.getcell((short) 3); // system.out.println(cell.getcelltype()); value = cell.getnumericcellvalue(); try { con = dbconnection.getconnection(); cstmt = con.preparecall(iconstants.sp_importexcel_key); cstmt.setstring(1, flag); cstmt.setstring(2, name2); cstmt.setstring(3, name3); cstmt.setstring(4, name); cstmt.setdouble(5, value); cstmt.EXECute(); } catch (sqlexception e) { e.printstacktrace(); logger.error(e.getmessage()); throw e; } finally { try { if (cstmt != null) { cstmt.close(); } if (con != null) { con.close(); } } catch (sqlexception e) { e.printstacktrace(); logger.error(e.getmessage()); throw e; } } } } } else { importexcel(new file(dir, s[j]), file); } } } } |
|
|
|
|