博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
有时候我们在项目中需要录入大量的数据,Excel操作
阅读量:5089 次
发布时间:2019-06-13

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

有时候我们在项目中需要录入大量的数据,Excel操作

1:首先需要在项目中导入架包:

jxl.jar,

poi-3.6-20091214.jar

poi-3.8-beta5-20111217.jar
poi-examples-3.6-20091214.jar
poi-examples-3.8-beta5-20111217.jar
poi-excelant-3.8-beta5-20111217.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-3.8-beta5-20111217.jar
poi-ooxml-schemas-3.6-20091214.jar
poi-ooxml-schemas-3.8-beta5-20111217.jar
poi-scratchpad-3.6-20091214.jar
poi-scratchpad-3.8-beta5-20111217.jar

2:需要些一个操作Excel的工具实体类

package com.fxr.po;import java.util.ArrayList;import java.util.List;public class ExcelWorkSheet
{ private String sheetName;//页 private List
data=new ArrayList
();//数据 private List
columns;//列 /** * @return the sheetName */ public String getSheetName() { return sheetName; } /** * @param sheetName the sheetName to set */ public void setSheetName(String sheetName) { this.sheetName = sheetName; } /** * @return the data */ public List
getData() { return data; } /** * @param data the data to set */ public void setData(List
data) { this.data = data; } /** * @return the columns */ public List
getColumns() { return columns; } /** * @param columns the columns to set */ public void setColumns(List
columns) { this.columns = columns; } }

在action中写具体的逻辑实现

package com.fxr.action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.fxr.dao.SubjectDAO;import com.fxr.dao.SubjectDAOImpl;import com.fxr.po.ExcelWorkSheet;import com.fxr.po.Subject;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;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.struts2.ServletActionContext;public class ImportExcelAction extends ActionSupport{                  private File excelFile;//Excel文件    private String excelFileFileName;//文件的名字    private ExcelWorkSheet
excelWorkSheet;//页 private Workbook createWorkBook(InputStream is) throws Exception{ if(getExcelFileFileName().toLowerCase().endsWith("xls")){ return new HSSFWorkbook(is); } if(getExcelFileFileName().toLowerCase().endsWith("xlsx")) { return new XSSFWorkbook(is); } return null; } public ImportExcelAction() { } @Override public String execute() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); System.out.println(request); ServletActionContext.getRequest().setCharacterEncoding("utf-8"); Workbook workbook=createWorkBook(new FileInputStream(excelFile)); Sheet sheet=(Sheet) workbook.getSheetAt(0); excelWorkSheet=new ExcelWorkSheet
(); excelWorkSheet.setSheetName(sheet.getSheetName()); Row firstRow=sheet.getRow(0); Iterator
iterator=firstRow.iterator(); List
cellNames=new ArrayList
(); while(iterator.hasNext()){ cellNames.add(iterator.next().getStringCellValue()); } excelWorkSheet.setColumns(cellNames); for (int i = 1; i <=sheet.getLastRowNum(); i++) { Row row=sheet.getRow(i); Subject subject=new Subject(); subject.setSubjectTitle(row.getCell(0).getStringCellValue()); subject.setSubjectOptionA(row.getCell(1).getStringCellValue()); subject.setSubjectOptionB(row.getCell(2).getStringCellValue()); subject.setSubjectOptionC(row.getCell(3).getStringCellValue()); subject.setSubjectOptionD(row.getCell(4).getStringCellValue()); subject.setSubjectAnswer(row.getCell(5).getStringCellValue()); subject.setSubjectParse(row.getCell(6).getStringCellValue()); SubjectDAO subjectDao = new SubjectDAOImpl(); subjectDao.addSubject(subject); excelWorkSheet.getData().add(subject); } return SUCCESS; } /** * @return the excelFile */ public File getExcelFile() { return excelFile; } /** * @param excelFile the excelFile to set */ public void setExcelFile(File excelFile) { this.excelFile = excelFile; } public ExcelWorkSheet
getExcelWorkSheet() { return excelWorkSheet; } /** * @param excelWorkSheet the excelWorkSheet to set */ public void setExcelWorkSheet(ExcelWorkSheet
excelWorkSheet) { this.excelWorkSheet = excelWorkSheet; } /** * @return the excelFileFileName */ public String getExcelFileFileName() { return excelFileFileName; } /** * @param excelFileFileName the excelFileFileName to set */ public void setExcelFileFileName(String excelFileFileName) { this.excelFileFileName = excelFileFileName; }}

前台JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib  prefix="s" uri="/struts-tags" %>            
试题信息的导入

试题信息的导入

导入excel文件

插入成功的JSP

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@taglib  prefix="s" uri="/struts-tags" %>            
导入成功

 

转载于:https://www.cnblogs.com/aicpcode/p/4179960.html

你可能感兴趣的文章
经典入门_排序
查看>>
Redis Cluster高可用集群在线迁移操作记录【转】
查看>>
二、spring中装配bean
查看>>
VIM工具
查看>>
javascript闭包
查看>>
@Column标记持久化详细说明
查看>>
创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备...
查看>>
mysql8.0.13下载与安装图文教程
查看>>
站立会议08(冲刺2)
查看>>
url查询参数解析
查看>>
http://coolshell.cn/articles/10910.html
查看>>
[转]jsbsim基础概念
查看>>
DIV和SPAN的区别
查看>>
第一次使用cnblogs
查看>>
C#语法糖之 session操作类 asp.net
查看>>
2015 Multi-University Training Contest 3
查看>>
使用Gitblit 在windows 上部署你的Git Server
查看>>
217. Contains Duplicate
查看>>
vue2.0 关于Vue实例的生命周期
查看>>
jenkins 更换主数据目录
查看>>