今天說下Java Web中常見的Excel的導入,一種是POI,一種是jxl。我們來用POI操作一下,寫一個Java 導入Excel的實例。前提是導入POI的相關jar包。
我們呢定義一個常見的Java bean,包括一些屬性和get/set方法。
package org.topcheer.biz.sys.model;
public class BcapBmsSignIMP {
private String actiontype;
private String actiondesc;
private String strdecode;
private String branchcode;
private String clientcode;
private String clientname;
private String contractdate;
private String contract;
private String contractamount;
private String maturity;
private String lincame;
private String lincamethod;
private String aginraup;
private String aginralo;
private String aginraloinpay;
private String remark;
private String businessid;
private String orgcode;
public String getStrdecode() {
return strdecode;
}
public void setStrdecode(String strdecode) {
this.strdecode = strdecode;
}
public String getActiontype() {
return actiontype;
}
public void setActiontype(String actiontype) {
this.actiontype = actiontype;
}
public String getActiondesc() {
return actiondesc;
}
public void setActiondesc(String actiondesc) {
this.actiondesc = actiondesc;
}
public String getBranchcode() {
return branchcode;
}
public void setBranchcode(String branchcode) {
this.branchcode = branchcode;
}
public String getClientcode() {
return clientcode;
}
public void setClientcode(String clientcode) {
this.clientcode = clientcode;
}
public String getClientname() {
return clientname;
}
public void setClientname(String clientname) {
this.clientname = clientname;
}
public String getContractdate() {
return contractdate;
}
public void setContractdate(String contractdate) {
this.contractdate = contractdate;
}
public String getContract() {
return contract;
}
public void setContract(String contract) {
this.contract = contract;
}
public String getContractamount() {
return contractamount;
}
public void setContractamount(String contractamount) {
this.contractamount = contractamount;
}
public String getMaturity() {
return maturity;
}
public void setMaturity(String maturity) {
this.maturity = maturity;
}
public String getLincame() {
return lincame;
}
public void setLincame(String lincame) {
this.lincame = lincame;
}
public String getLincamethod() {
return lincamethod;
}
public void setLincamethod(String lincamethod) {
this.lincamethod = lincamethod;
}
public String getAginraup() {
return aginraup;
}
public void setAginraup(String aginraup) {
this.aginraup = aginraup;
}
public String getAginralo() {
return aginralo;
}
public void setAginralo(String aginralo) {
this.aginralo = aginralo;
}
public String getAginraloinpay() {
return aginraloinpay;
}
public void setAginraloinpay(String aginraloinpay) {
this.aginraloinpay = aginraloinpay;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getBusinessid() {
return businessid;
}
public void setBusinessid(String businessid) {
this.businessid = businessid;
}
public String getOrgcode() {
return orgcode;
}
public void setOrgcode(String orgcode) {
this.orgcode = orgcode;
}
}
這個是要導入的數據類型,Excel中的一行對應一個BcapBmsSignIMP對象。
下面在我們實際Action需要的地方調用這個方法:ImportDataFromExcel即可。參數:vo:就是一個BcapBmsSignIMP對象的實例
is是頁面傳過來的Excel轉化的輸入流,後面的excelFilename是excel的名稱。
/**
* 判斷excel文件後綴名,生成不同的Workbook
* @param is
* @param excelFileName
* @return
* @throws IOException
*/
public Workbook createWorkBook(InputStream is,String excelFileName) throws IOException {
if(excelFileName.endsWith(".xls")){
return new HSSFWorkbook(is);
}else if(excelFileName.endsWith(".xlsx")){
return new XSSFWorkbook(is);
}
return null;
}
/**
* 根據sheet索引號獲取對應的sheet
*
* @param workBook
* @param sheetIndex
* @return
*/
public Sheet getSheet(Workbook workBook,int sheetIndex){
return workBook.getSheetAt(0);
}
/**
* 將sheet中的數據保存到list中,
* 1、使用此方法時 vo的屬性個數必須和excel文件每行數據的列數相同且一一對應,vo屬性的所有類型都爲String
* 2、在action調用此方法是 需聲明
* private File excelFile;上傳的文件
* private String excelFileFileName;保存原始的文件名
* 兩個屬性
* 3、頁面的file控件 name需對應File的名稱
* @param vo javaBean
* @param is 輸入流
* @param excelFileName 要導入的excel名稱
*/
public List<Object> importDataFromExcel(Object vo,InputStream is,String excelFileName){
List<Object> list = new ArrayList<Object>();
try{
//1、創建工作簿
Workbook workBook = this.createWorkBook(is, excelFileName);
//2、創建工作表sheet
Sheet sheet = this.getSheet(workBook, 0);
//3、獲取sheet中數據行數
int rows = sheet.getPhysicalNumberOfRows();
int cells = sheet.getRow(0).getPhysicalNumberOfCells();//獲取表頭的單元格個數
//利用反射得到該對象的所有屬性
Field[] fields = vo.getClass().getDeclaredFields();
for(int i=1;i< rows ; i++){//第一行爲標題行,從第二行開始取數據
Row row = sheet.getRow(i);
int index =0;
//利用反射,根據javabean屬性的先後順序,動態調用setXxx()方法給屬性賦值
while(index < cells){
Cell cell = row.getCell(index);
if(null == cell){
cell = row.createCell(index);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
String value = null == cell.getStringCellValue()?"":cell.getStringCellValue();
Field field = fields[index];
String fieldName = field.getName();
String methodName = "set"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
Method setMethod = vo.getClass().getMethod(methodName,new Class[]{String.class} );
setMethod.invoke(vo, new Object[]{value});
index++;
}
if(isHasValues(vo)){//對象屬性有值
list.add(vo);
vo = vo.getClass().getConstructor(new Class[]{}).newInstance(new Object[]{});//重新創建一個vo對象
}
}
}catch(Exception e){
e.printStackTrace();
log.error(e);
}finally{
try {
is.close();//關閉輸入流
} catch (IOException e) {
log.error(e);
}
}
return list;
}