使用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();
}
}