struts2输出并下载excel文件

Action文件:

[c-sharp]  view plain copy
  1. package com.credit.stats.model;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. import org.apache.poi.hssf.usermodel.HSSFCell;  
  4. import org.apache.poi.hssf.usermodel.HSSFRow;  
  5. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  6. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  7. import org.apache.struts2.interceptor.ServletRequestAware;  
  8. import org.apache.struts2.interceptor.ServletResponseAware;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. import java.io.ByteArrayInputStream;  
  12. import java.io.ByteArrayOutputStream;  
  13. import java.io.InputStream;  
  14. import java.util.ArrayList;  
  15. import java.util.List;  
  16. /** 
  17.  * Created by IntelliJ IDEA. 
  18.  * User: 晃同 
  19.  * Date: 2010-10-14 
  20.  * Time: 15:31:38 
  21.  * To change this template use File | Settings | File Templates. 
  22.  */  
  23. public class OutputExcelAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{  
  24.     protected HttpServletRequest request = null;  
  25.     protected HttpServletResponse response = null;  
  26.     public void setServletRequest(HttpServletRequest arg0) {  
  27.         this.request = arg0;  
  28.     }  
  29.     public void setServletResponse(HttpServletResponse arg0) {  
  30.         this.response = arg0;  
  31.     }  
  32.     //获取输出流  
  33.     public InputStream getInputExcel()  
  34.     {  
  35.         return (InputStream)request.getAttribute("excelStream");  
  36.     }  
  37.     @Override  
  38.     public String execute() throws Exception {  
  39.             List<String> list = new ArrayList<String>();  
  40.             list.add("张三");  
  41.             list.add("李四三");  
  42.             if(list != null){  
  43.                  int rows=0;  
  44.                  HSSFWorkbook wk = new HSSFWorkbook();  
  45.                  HSSFSheet sheet = wk.createSheet("FollowAgentStats");  
  46.                  HSSFRow row = sheet.createRow(rows++);  
  47.                  int column=0;  
  48.                  HSSFCell cell = row.createCell(column++,HSSFCell.CELL_TYPE_STRING);  
  49.                  cell.setCellValue("序号");  
  50.                  cell = row.createCell(column++,HSSFCell.CELL_TYPE_STRING);  
  51.                  cell.setCellValue("姓名");  
  52.                  for(String ls:list){  
  53.                      row = sheet.createRow(rows++);  
  54.                      column=0;  
  55.                      cell = row.createCell(column++,HSSFCell.CELL_TYPE_NUMERIC);  
  56.                      cell.setCellValue(rows - 1);  
  57.                      cell = row.createCell(column++,HSSFCell.CELL_TYPE_STRING);  
  58.                      cell.setCellValue(ls);  
  59.                  }  
  60.                  try{  
  61.                      ByteArrayOutputStream out=new ByteArrayOutputStream();  
  62.                      wk.write(out);  
  63.                      request.setAttribute("excelStream",new ByteArrayInputStream(out.toByteArray()));  
  64.                      request.setAttribute("filename""myexcel");//设置文件名  
  65.                      return SUCCESS;  
  66.                  }catch(Throwable th){  
  67.                      th.printStackTrace();  
  68.                      System.out.println("无法输出Excel文件");  
  69.                      return ERROR;  
  70.                  }  
  71.             }else{  
  72.                 System.out.println("统计数据不存在");  
  73.                 return ERROR;  
  74.             }  
  75.     }  
  76. }  

 

struts文件:

[xhtml]  view plain copy
  1. <action name="download" class="OutputExcelAction">              
  2.     <result name="success" type="stream">  
  3.         <param name="contentType">application/vnd.ms-excel</param>  
  4.         <param name="inputName">inputExcel</param>  
  5.         <param name="contentDisposition">attachment;filename="${#request['filename']}"</param>  
  6.         <param name="bufferSize">8192</param>  
  7.     </result>  
  8. </action>  

你可能感兴趣的:(struts2输出并下载excel文件)