日期:2014-05-18 浏览次数:21647 次
<s:form action="userInfo" method="post" enctype="multipart/form-data"> 导入Excel文件:<s:file name="excelFile"></s:file> <br/> <s:submit value="导入"></s:submit> </s:form>
package com.boxun.bean; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.record.formula.functions.T; public class ExcelWorkSheet<T> { private String sheetName; private List<T> data = new ArrayList<T>(); //数据行 private List<String> columns; //列名 public String getSheetName() { return sheetName; } public void setSheetName(String sheetName) { this.sheetName = sheetName; } public List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } public List<String> getColumns() { return columns; } public void setColumns(List<String> columns) { this.columns = columns; } }
package com.boxun.action; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.boxun.bean.ExcelWorkSheet; import com.boxun.bean.Userinfo; import com.boxun.biz.IUserBiz; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private IUserBiz userBiz; public void setUserBiz(IUserBiz userBiz) { this.userBiz = userBiz; } private File excelFile; //上传的文件 private String excelFileFileName; //保存原始文件名 //将Excel文件解析完毕后信息存放到这个对象中 private ExcelWorkSheet<Userinfo> excelWorkSheet; public File getExcelFile() { return excelFile; } public void setExcelFile(File excelFile) { this.excelFile = excelFile; } public String getExcelFileFileName() { return excelFileFileName; } public void setExcelFileFileName(String excelFileFileName) { this.excelFileFileName = excelFileFileName; } public ExcelWorkSheet<Userinfo> getExcelWorkSheet() { return excelWorkSheet; } public void setExcelWorkSheet(ExcelWorkSheet<Userinfo> excelWorkSheet) { this.excelWorkSheet = excelWorkSheet; } //判断文件类型 public Workbook createWorkBook(InputStream is) throws IOException{ if(excelFileFileName.toLowerCase().endsWith("xls")){ return new HSSFWorkbook(); } if(excelFileFileName.toLowerCase().endsWith("xlsx")){ retu