struts2中apache的POI控件實現頁面數據導出為excel文件

来源:互联网 发布:出租车计价软件 编辑:程序博客网 时间:2024/06/05 09:16

原理是從頁面點擊鏈接觸發action,并在action方法中從session中取得list。這樣做的缺點是,如果頁面顯示數據時對數據使用了判斷、迭代數據顯示,那麼在action中同樣要去做同樣的工作。而我最理想的方法是將頁面生成的html代碼中的table里的數據直接導入到xls中,這樣就不需再做複雜的業務處理。

 

JSP頁面添加觸發action鏈接:

<a href="javascript:;" onClick="javascript:toXML();">通過poi導出XLS</a>

javascript函數:

 function toXML(){
  form1.action = "toXLS.action";

  form1.submit();
 }

 

 

action類:

package biz.ceeport.struts.action;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

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 com.opensymphony.xwork2.ActionSupport;

/**
 * @author winston
 * @version CreationTime: 2010-2-25
 * @class declaration:
 */
public class DownloadAction extends ActionSupport{
 
 public void toXLS() throws Exception {
  List list = (List)ServletActionContext.getRequest().getSession().getAttribute("mylist");
  //create HSSFWorkbook
  HSSFWorkbook workbook = new HSSFWorkbook();     
  //create HSSFSheet named user
  HSSFSheet sheet = workbook.createSheet("user");
  
     //create row
  HSSFRow rowTitle = sheet.createRow(0);
     //create cell
     HSSFCell cellTitle = rowTitle.createCell((short)(0));
     //filled cell
     cellTitle.setCellValue(getText("common_activityName"));
    
     cellTitle = rowTitle.createCell((short)1);
     cellTitle.setCellValue(getText("common_activityTime2"));
    
     cellTitle = rowTitle.createCell((short)2);
     cellTitle.setCellValue(getText("common_signupEndTime"));
    
     cellTitle = rowTitle.createCell((short)3);
     cellTitle.setCellValue(getText("common_signupNum"));
    
  for (int i = 0; i < 5; i++) {
   HashMap map = (HashMap)list.get(i);
   HSSFRow row = sheet.createRow(i+1);
   
   HSSFCell cell = row.createCell((short)0);
   cell.setCellValue(map.get("ACTIVITY_NAME").toString());
   
   cell = row.createCell((short)1);
   cell.setCellValue(map.get("END_TIME").toString());
   
   cell = row.createCell((short)2);
   cell.setCellValue(map.get("SHOW_END_TIME").toString());
   
   cell = row.createCell((short)3);
   cell.setCellValue(((HashMap)map.get("SIGNUPPEPOERNUM")).get("NUM").toString());
   
  }
  //set response content type
  HttpServletResponse response = ServletActionContext.getResponse();
  response.setContentType("application/vnd.ms-excel");
  //set the name of download file 
  response.addHeader("Content-Disposition","attachment;filename=350000.xls");
  //get response stream
  OutputStream out=response.getOutputStream();
  //add workbook to OutpustStream
  workbook.write(out);
  out.close();
 }
}

 

struts.xml中:

    <!-- 導出xls文檔-->
    <action name="toXLS" class="biz.ceeport.struts.action.DownloadAction" method="toXLS">
        <result name="success" type="chain">manageActilist</result>
           <result name="error">/error.jsp</result>
    </action>

 

測試運行后的結果是正確的,但中文亂碼

 

在CSDN上找到同類文章,解決了亂碼問題:

http://blog.csdn.net/flyever581549/archive/2008/11/11/3275850.aspx

cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 oppor9s锁屏密码忘了怎么办 oppor9s忘记锁屏图案怎么办 oppor9忘记锁屏图案怎么办 电脑锁屏卡住了怎么办 dell电脑密码忘了怎么办 dell电脑密码忘记了怎么办 手机锁屏为什么打不开怎么办 手机图形解锁忘记了怎么办 华硕电脑锁屏了怎么办 5s忘记解锁密码怎么办 苹果手机密码忘记了怎么办 苹果手机iphone已停用怎么办 手机被停用怎么办不用电脑 激活锁是别人的怎么办 6s开机密码忘了怎么办 苹果手机忘记解锁密码怎么办 忘记苹果触控id怎么办 苹果6app被锁了怎么办 捡的苹果id锁了怎么办 苹果手机id密码锁了怎么办 美版5s刷机后激活怎么办 vivo手机图案解锁忘记了怎么办 苹果5sid锁死了怎么办 苹果id账号被锁怎么办 苹果6s忘了ld怎么办 小米忘记图形解锁怎么办呢 索尼手机忘记解锁图案怎么办 oppoa37忘记锁屏密码怎么办 oppoa37锁屏密码忘了怎么办 oppor9图案密码忘了怎么办 oppor忘记锁屏图案怎么办 oppor手机忘记解锁图案怎么办 oppor手机忘记图案了怎么办 oppo手机屏幕锁忘了怎么办 三星手机屏幕锁忘了怎么办 手机主菜单坏了怎么办 索尼手机密码忘了怎么办 索尼笔记本密码忘了怎么办 索尼手机解锁密码忘了怎么办 索尼记录仪密码忘了怎么办 索尼z3手机忘记开机密码怎么办