博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Excel数据批量导入到数据库2
阅读量:5039 次
发布时间:2019-06-12

本文共 4330 字,大约阅读时间需要 14 分钟。

1.导包(共3个)

2.jsp

  

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
studentslist=new ArrayList
(); 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
getStudentslist() { return studentslist; } public void setStudentslist(List
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; } }}

  

转载于:https://www.cnblogs.com/feifeishi/p/6134473.html

你可能感兴趣的文章
iframe的一些介绍
查看>>
mapper.xml实现oracle的分页语句
查看>>
[51nod1267]4个数和为0(预处理+双指针)
查看>>
[poj2151]Check the difficulty of problems概率dp
查看>>
深入浅出之Smarty模板引擎工作机制(一)
查看>>
UE4物理动画使用
查看>>
线程间操作无效: 从不是创建控件“listBox1”的线程访问它
查看>>
2014.09.11 cURL
查看>>
Jython概要
查看>>
文件(图片)转base64
查看>>
剑指 offer set 2 从头到尾打印链表
查看>>
博客开通第69天
查看>>
博客开通第九天
查看>>
Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析
查看>>
8086中的七种寻址方式
查看>>
秒杀系统解决方案
查看>>
Java动态代理机制
查看>>
【Git】Git工具常用命令
查看>>
ipmsg 绑定tcp错误
查看>>
九型人格判定
查看>>