用struts2来导入excel文件并显示到页面上

一、先把需要的包导入到工程里如下:

struts jar

commons-fileupload-1.2.1

commons-io-1.3.2

commons-logging-1.0.4

commons-logging-1.1

freemarker-2.3.13

ognl-2.6.11

struts2-core-2.1.6

xwork-2.1.2

 

Poi jar:

poi-3.6-20091214

二、写导入Excellent的jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@  taglib prefix="s" uri="/struts-tags" %>


   <!-- 导入页面 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body>
 <%-- 记住这里需要设置enctype="multipart/form-data"--%>   
<s:form action="userInfo" method="post" enctype="multipart/form-data">  
    导入Excel文件:<s:file name="excelFile"></s:file> <br/> 
    <s:submit value="导入"></s:submit> 
  </s:form> 

  </body>
</html>

三、写导入Excel文件的action:

package cn.ichina.action;
import java.io.File; 


import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 

 
import cn.ichina.bean.ExcelWorkSheet;
import cn.ichina.bean.Userinfo;
import com.opensymphony.xwork2.ActionSupport; 
/**
 * 导入的action
 * @author Administrator
 *
 */
public class UserAction extends ActionSupport{
     private File excelFile; //上传的文件  
      
     private String excelFileFileName; //保存原始文件名     
     private ExcelWorkSheet<Userinfo> excelWorkSheet;  //将Excel文件解析完毕后信息存放到这个对象中
     //判断文件类型  
     public Workbook createWorkBook(InputStream is) throws IOException{ 
         if(excelFileFileName.toLowerCase().endsWith("xls")){ 
             return new HSSFWorkbook(is); 
         } 
         if(excelFileFileName.toLowerCase().endsWith("xlsx")){ 
             //return new XSSFWorkbook(is); 
         } 
         return null; 
     } 
      
     public String execute() throws Exception{ 
         Workbook book = createWorkBook(new FileInputStream(excelFile)); 
         //book.getNumberOfSheets();  判断Excel文件有多少个sheet  
         Sheet sheet =  book.getSheetAt(0);   
         excelWorkSheet = new ExcelWorkSheet<Userinfo>(); 
         //保存工作单名称  
         Row firstRow = sheet.getRow(0); 
         Iterator<Cell> iterator = firstRow.iterator(); 
          
         //保存列名  
         List<String> cellNames = new ArrayList<String>(); 
         while (iterator.hasNext()) { 
             cellNames.add(iterator.next().getStringCellValue()); 
         } 
         excelWorkSheet.setColumns(cellNames);
         //遍历
         for (int i = 1; i <= sheet.getLastRowNum(); i++) { 
             Row ros = sheet.getRow(i); 
             Userinfo user = new Userinfo(); 
             user.setId((int)ros.getCell(0).getNumericCellValue()); 
             user.setName(ros.getCell(1).getStringCellValue()); 
             user.setPass(ros.getCell(2).getStringCellValue()); 
             user.setLastname(ros.getCell(3).getStringCellValue()); 
             user.setAddres(ros.getCell(4).getStringCellValue()); 
             user.setRemark(ros.getCell(5).getStringCellValue()); 
             excelWorkSheet.getData().add(user); 
         } 
         for (int i = 0; i < excelWorkSheet.getData().size(); i++) { 
             Userinfo info = excelWorkSheet.getData().get(i); 
             System.out.println(info.getLastname()); 
         } 
         return SUCCESS; 
     } 

    
    
     public File getExcelFile() { 
         return excelFile; 
     } 
  
     public void setExcelFile(File excelFile) { 
         this.excelFile = excelFile; 
     } 
  
     public String getExcelFileFileName() { 
         return excelFileFileName; 
     } 
  
     public void setExcelFileFileName(String excelFileFileName) { 
         this.excelFileFileName = excelFileFileName; 
     } 
  
     public ExcelWorkSheet<Userinfo> getExcelWorkSheet() { 
         return excelWorkSheet; 
     } 
  
     public void setExcelWorkSheet(ExcelWorkSheet<Userinfo> excelWorkSheet) { 
         this.excelWorkSheet = excelWorkSheet; 
     } 
}

 

四、配置sturts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

 
 <package name="excelOutPut" extends="struts-default">
  <action name="userInfo" class="cn.ichina.action.UserAction">
   <result>/InputExcel.jsp</result>
  </action>
 </package>
</struts>

 

五、把导入的文件全部显示到页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!-- 显示页面 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

  </head>
 
  <body>
     <p> 

<!--标题头-->
    <s:iterator value="excelWorkSheet.columns"> 
        <s:property /> &nbsp;&nbsp;  
    </s:iterator> 
  </p> 
    <!--遍历显示的内容-->
  <s:iterator var="user" value="excelWorkSheet.data"> 
    <p> 
        <s:property value="#user.id"/> &nbsp; &nbsp; &nbsp; 
        <s:property value="#user.name"/> &nbsp; &nbsp; &nbsp; 
        <s:property value="#user.pass"/> &nbsp; &nbsp; &nbsp; 
        <s:property value="#user.lastname"/> &nbsp; &nbsp;&nbsp;  
        <s:property value="#user.addres"/> &nbsp; &nbsp;&nbsp;   
        <s:property value="#user.remark"/> &nbsp;    
    </p> 
  </s:iterator> 
   
  </body>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值