资源简介
struts1 poi Excel批量导入支持xls和xlsx-源码java
有需要的能帮助到
代码片段和文件信息
package poi.excel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class ExcelPoiAction extends DispatchAction{
public ActionForward upload(ActionMapping mapping ActionForm actionForm
HttpServletRequest request HttpServletResponse response) throws Exception {
ExcelPoizForm form =(ExcelPoizForm)actionForm;
try {
//提示消息
String message=““;
Workbook workbook =this.createWorkBook(form.getFile().getInputStream()form.getFile().getFileName());
//读取第一个sheet
Sheet sheet = workbook.getSheetAt(0);
//读取数据从第二行开始,第一列岗位名称,第二列岗位描述第三列岗位编号
for(int i=1; i<=sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
System.out.println(row.getCell(4));
}
request.setAttribute(“msgresult“ “上传完成!“+message);
} catch (FileNotFoundException e) {
request.setAttribute(“msgresult“ “导入失败!“);
System.out.println(“导入失败“);
} catch (IOException e) {
request.setAttribute(“msgresult“ “导入失败!“);
System.out.println(“导入失败“);
}catch(Exception e){
request.setAttribute(“msgresult“ “导入失败!“);
System.out.println(“导入失败“);
}
finally{
return mapping.findForward(“success“);
}
}
/* * 判断是xls文件还是xlsx文件 */
public Workbook createWorkBook(InputStream isString fileName) throws IOException{
if(fileName.toLowerCase().endsWith(“xls“)){
return new HSSFWorkbook(is);
}
if(fileName.toLowerCase().endsWith(“xlsx“)){
return new XSSFWorkbook(is);
}
return null;
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2177 2013-12-26 21:11 struts1 poi Excel批量导入支持xls和xlsx-源码java\ExcelPoiAction.java
文件 317 2013-12-26 20:40 struts1 poi Excel批量导入支持xls和xlsx-源码java\ExcelPoizForm.java
文件 1039 2013-12-26 20:46 struts1 poi Excel批量导入支持xls和xlsx-源码java\poiExcelDemo.jsp
文件 734 2013-12-26 21:29 struts1 poi Excel批量导入支持xls和xlsx-源码java\struts-config.xm
目录 0 2013-12-26 21:19 struts1 poi Excel批量导入支持xls和xlsx-源码java\包
目录 0 2013-12-26 21:29 struts1 poi Excel批量导入支持xls和xlsx-源码java
----------- --------- ---------- ----- ----
4267 6
评论
共有 条评论