struts导入Excel进行解析
為什么80%的碼農都做不了架構師?>>> ??
struts導入Excel進行POI解析,源碼地址:http://download.csdn.net/detail/bq1073100909/7887635
非常感謝http://blog.csdn.net/cherishme1988/article/details/8068339,我是在他的博客上學習的,成功實現。
新建一個web項目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
在源碼中已經添加,在WebRoot下WEB-INF下的lib中。
Excel表中的數據格式如下圖:
新建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導入測試</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>下面進入正題,action中的Excel導入,首先進行文件的上傳,在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("文件已經存在,將要覆蓋");}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進行解析,傳入文件的名字,我單獨寫了一個類定義一個靜態方法對Excel進行操作。
ExcelUtil.java代碼:
這樣就可以把Excel中的數據封裝到Person類實例化的實體中并保存到list進行返回,如果進行ssh開發,建議把id去掉,因為保存數據是不需要id,這個是自動增長,在switch中從1開始就可以了,然后進行save(entity)存儲到數據庫。我做的這個例子主要是學習解析的原理。如果有其他的字段請自己添加。(其中對數據類型進行轉換的時候要注意哦)
運行如下:
結果如下:
轉載于:https://my.oschina.net/zjcx/blog/679589
總結
以上是生活随笔為你收集整理的struts导入Excel进行解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海天氣情況及空氣質量指數
- 下一篇: 【转】解决wine中文乱码的问题