您的位置:程序门 -> java -> j2se / 基础类



请教关于如何修改文件类型的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教关于如何修改文件类型的问题
发表于: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);  

}  
}  

}  
}  
发表于:2007-02-05 16:54:141楼 得分:0
自己顶一下
发表于:2007-02-05 17:33:472楼 得分:0
你可以试一下,新建一个.xml,然后把原来的文件的内容写到这个文件里,这样转换一下,不知道可不可行


快速检索

最新资讯
热门点击