论坛首页 入门技术论坛

使用JXL读Excel文件

浏览 3977 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-09-15  

使用JXL读Excel文件

以前用过POI解析Excel,但是没有保留下来。今天用JXL解析了一个Excel文件,发现不能.xlsx的,会报jxl.read.biff.BiffException: Unable to recognize OLE stream。但是解析.xls可以。
package com.css.util.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class ExcelParser {

 
 public ExcelParser() {
  super();
  // TODO Auto-generated constructor stub
 }

 public  static List getBOFromExcelFile(String fileAbsolutePathName){
  List boList = new ArrayList();
  CMSBean cmsBean ;
  try {
   
   Workbook book = Workbook.getWorkbook(new File(fileAbsolutePathName));
   Sheet sheet = book.getSheet(0);
   int rowNum = sheet.getRows();
   int columnNum = sheet.getColumns();
   for(int i=1;i<rowNum;i++){
    cmsBean = new CMSBean();
    for(int j=0;j<columnNum;j++){
     Cell cell = sheet.getCell(j, i);
     if(j == 0)
      cmsBean.setUserName(cell.getContents());
     if(j == 1)
      cmsBean.setPassword(cell.getContents());
     if(j == 2)
      cmsBean.setRole(cell.getContents());
     if(j == 3)
      cmsBean.setEmail(cell.getContents());
    }
    boList.add(cmsBean);
   }
   book.close();
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
   
  }  
  return boList;
 }
 
 public  static void main(String args[]){
  List list  = ExcelParser.getBOFromExcelFile("E:\\temp\\Book1.xlsx");
  if(list != null && list.size()>0){
   for(int i=0;i<list.size();i++){
    CMSBean cmBean = (CMSBean)list.get(i);
    System.out.println(cmBean);
    
   }
  }
 }
}


package com.css.util.excel;

public class CMSBean {

 private String userName;
 private String password;
 private String role;
 private String email;
 public CMSBean(String userName, String password, String role, String email) {
  super();
  this.userName = userName;
  this.password = password;
  this.role = role;
  this.email = email;
 }
 public CMSBean() {
  super();
  // TODO Auto-generated constructor stub
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getRole() {
  return role;
 }
 public void setRole(String role) {
  this.role = role;
 }
 public String getUserName() {
  return userName;
 }
 public void setUserName(String userName) {
  this.userName = userName;
 }
 
 public String toString(){
  return this.getUserName() + ":" + this.getPassword() + ":" + this.getRole() + ":" + this.getEmail();
  
 }
}

   发表时间:2007-11-27  
javaeye就是垃圾,人家提问题,除了投人家入门贴,也没人帮忙,感觉帮派气氛很浓厚,初级会员发的帖子基本都会沦为隐藏帖或者入门贴,我就弄不懂啊。。。。
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics