`
a498740995
  • 浏览: 33818 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

java解析excel

 
阅读更多

对xls格式的文件进行解析。使用jxl.jar。

此外还需要用到的jar包有:

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

 

 

后台获取代码:写道

List fileList=null;
DiskFileItemFactory fac= new DiskFileItemFactory ();
ServletFileUpload upload= new ServletFileUpload (fac);
try{
fileList=upload.parseRequest(req);
}catch(FileUploadException ex){
}
Iterator<FileItem> it= fileList.iterator();
while(it.hasNext()){
FileItem item= it.next();
if(!item.isFormField){
BufferedInputStream in= new BufferedInputStream (item.getInputStream());
}
}
============================================
rwb= Workbook.getWorkbook(is); //获得工作薄的个数
rwb.getNumberOfSheets();
//在Excel文档中,第一张工作表的缺省索引是0 Sheet st = rwb.getSheet("Table1");
//通用的获取cell值的方式,getCell(int column, int row) 行和列
int rows=st.getRows();
int cols=st.getColumns();
System.out.println("当前工作表的名字:"+st.getName());
System.out.println("总行数:"+rows);
System.out.println("总列数:"+cols);
for(int i=1;i<rows;i++){
Cell c1=st.getCell(0,i);
Cell c2=st.getCell(1,i);
Cell c3=st.getCell(2,i);
println "卡号:${c1.getContents()}、密码:${c2.getContents()}、日期:${c3.getContents()}" }

  =============================================

以上只能用于xls的解析。

对于xlsx的解析需要用:poi提供的jar包

用xssFWookbook.....

===============================

对于csv,txt:

利用最简单的读取一行,逗号隔开即可。

@Test public void readFile01() throws IOException { FileReader fr=new FileReader("E:/phsftp/evdokey/evdokey_201103221556.txt"); BufferedReader br=new BufferedReader(fr); String line=""; String[] arrs=null; while ((line=br.readLine())!=null) { arrs=line.split(","); System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]); } br.close(); fr.close(); }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics