struts导入Excel进行解析
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
struts導(dǎo)入Excel進(jìn)行POI解析,源碼地址:http://download.csdn.net/detail/bq1073100909/7887635
非常感謝http://blog.csdn.net/cherishme1988/article/details/8068339,我是在他的博客上學(xué)習(xí)的,成功實(shí)現(xiàn)。
新建一個web項(xiàng)目test,添加struts框架,我使用的工具是myeclipse。
添加如下jar包:poi-3.0-rc4-20070503.jar;poi-contrib-3.0-rc4-20070503.jar;poi-ooxml-schemas-3.7-beta3.jar;poi-scratchpad-3.0-rc4-20070503.jar
在源碼中已經(jīng)添加,在WebRoot下WEB-INF下的lib中。
Excel表中的數(shù)據(jù)格式如下圖:
新建Person類:不過多解釋直接代碼上:
package bean;public class Person {private Integer id;private String name;private int age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String toString(){return this.id+" "+this.name+" "+this.age;} }修改index.jsp,使之可以上傳Excel文件: <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>excel導(dǎo)入測試</title></head><body><s:form theme="simple" action="uploadaction!upload" enctype="multipart/form-data" method="post">選擇文件:??<s:file name="ufile" accept="excel/*" id="ufilename" /> <br><s:submit value="提交"></s:submit></s:form></body> </html>
struts.xml的配置文件,簡單易懂,不多解釋:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts><package name="exceltest" namespace="/" extends="struts-default"><action name="uploadaction" class="org.action.ExcelAction"><result name="success">/success.jsp</result></action></package> </struts>下面進(jìn)入正題,action中的Excel導(dǎo)入,首先進(jìn)行文件的上傳,在WebRoot下面建文件夾upload,將上傳的文件保存在這里。 package org.action;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 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 org.myutil.ExcelUtil;import bean.Person;import com.opensymphony.xwork2.ActionSupport;public class ExcelAction extends ActionSupport {private String ufileFileName;private File ufile;public String upload(){String directory = "/upload";//定義文件路徑String targetFileName = ufileFileName;String targetDirectory = ServletActionContext.getServletContext().getRealPath(directory);//生成上傳對象File target = new File(targetDirectory,targetFileName);//如果出存在就覆蓋if(target.exists()){target.delete();System.out.println("文件已經(jīng)存在,將要覆蓋");}try {FileUtils.copyFile(ufile, target);} catch (IOException e) {e.printStackTrace();}List<Person> list = ExcelUtil.importExcel(ufileFileName);int size = 0;System.out.println(size = list.size());for(int i=0;i<size;i++){System.out.println(list.get(i).toString());}return "success";}public String getUfileFileName() {return ufileFileName;}public void setUfileFileName(String ufileFileName) {this.ufileFileName = ufileFileName;}public File getUfile() {return ufile;}public void setUfile(File ufile) {this.ufile = ufile;} }
ExcelUtil.importExcel(ufileFileName);就是對Excel進(jìn)行解析,傳入文件的名字,我單獨(dú)寫了一個類定義一個靜態(tài)方法對Excel進(jìn)行操作。
ExcelUtil.java代碼:
這樣就可以把Excel中的數(shù)據(jù)封裝到Person類實(shí)例化的實(shí)體中并保存到list進(jìn)行返回,如果進(jìn)行ssh開發(fā),建議把id去掉,因?yàn)楸4鏀?shù)據(jù)是不需要id,這個是自動增長,在switch中從1開始就可以了,然后進(jìn)行save(entity)存儲到數(shù)據(jù)庫。我做的這個例子主要是學(xué)習(xí)解析的原理。如果有其他的字段請自己添加。(其中對數(shù)據(jù)類型進(jìn)行轉(zhuǎn)換的時候要注意哦)
運(yùn)行如下:
結(jié)果如下:
轉(zhuǎn)載于:https://my.oschina.net/zjcx/blog/679589
總結(jié)
以上是生活随笔為你收集整理的struts导入Excel进行解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海天氣情況及空氣質量指數
- 下一篇: 【转】解决wine中文乱码的问题