Excel数据批量导入到数据库2
1.导包(共3个)
2.jsp
<s:form action="ReadExcel.action" method="post" enctype="multipart/form-data">
<s:file name="file" ></s:file>
<s:submit></s:submit>
</s:form>
3.action代码
package com.chao.action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; import com.chao.db.Students; import com.chao.service.ActionManager; import com.opensymphony.xwork2.ActionSupport; public class ReadExcel extends ActionSupport { /** * */ ActionManager mgr; private static final long serialVersionUID = 1L; File file; String Excel_Path; String upload; private String fileFileName; private String fileContentType; Students students; List<Students> studentslist=new ArrayList<Students>(); public ActionManager getMgr() { return mgr; } public void setMgr(ActionManager mgr) { this.mgr = mgr; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getExcel_Path() { return Excel_Path; } public void setExcel_Path(String excel_Path) { Excel_Path = excel_Path; } public Students getStudents() { return students; } public void setStudents(Students students) { this.students = students; } public List<Students> getStudentslist() { return studentslist; } public void setStudentslist(List<Students> studentslist) { this.studentslist = studentslist; } public String execute() throws Exception{ try{ if(!fileContentType.toString().equals("application/vnd.ms-excel")){//上传文件格式只能为excel return ERROR; } File excel=new File(ServletActionContext.getServletContext().getRealPath("upload")); if(!excel.exists()){ excel.mkdir(); } FileUtils.copyFile(file,new File(excel+"\\"+fileFileName)); FileUtils.copyFile(file,new File(excel,fileFileName)); //读取excel InputStream is = new FileInputStream(file); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); students=new Students(); //遍历sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } //遍历每个行 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { HSSFCell number = hssfRow.getCell(0); HSSFCell password = hssfRow.getCell(1); HSSFCell name = hssfRow.getCell(2); HSSFCell sex = hssfRow.getCell(3); HSSFCell academy = hssfRow.getCell(4); HSSFCell major = hssfRow.getCell(5); HSSFCell classs = hssfRow.getCell(6); HSSFCell grade = hssfRow.getCell(7); //System.out.println(number+" "+password+" "+name+" "+sex+" "+academy+major+classs+grade); students.setNumber(number.toString()); students.setPassword(password.toString()); students.setName(name.toString()); students.setSex(sex.toString()); students.setAcademy(academy.toString()); students.setMajor(major.toString()); students.setClasss(classs.toString()); students.setGrade(grade.toString()); //System.out.println(students.getNumber()+" "+students.getPassword()+" "+students.getName()+" "+students.getSex()+" "+students.getAcademy()+students.getMajor()+students.getClasss()+students.getGrade()); mgr.Save(students); System.out.println(number+" "+name+" "+"插入成功"); } } } FileUtils.deleteDirectory(new File(ServletActionContext.getServletContext().getRealPath("upload"))); //删除文件夹 return SUCCESS; }catch (Exception e) { // TODO: handle exception System.out.print(e); FileUtils.deleteDirectory(new File(ServletActionContext.getServletContext().getRealPath("upload"))); //删除文件夹 return SUCCESS; } } }