惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
腾讯CDC
博客园_首页
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
SecWiki News
SecWiki News
美团技术团队
P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
D
DataBreaches.Net
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Schneier on Security
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Forbes - Security
Forbes - Security
D
Docker
T
Tenable Blog
S
Secure Thoughts
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

博客园 - 叶子绿了

freemarker显示含有html代码的内容 .net sql 防注入 httpmodule jquery ajax 传递js对象到后台 Struts2下多文件的上传与下载 dwr框架 Oracle 9i中包含Connect by 子句的查询向Oracle 10g移植后运行时错误及解决方法 JQuery Uploadify 基于JSP的无刷新上传实例 写了个用jquery控制select只读(select选项可以供用户查看但不能改变初始选中值) C#之模态窗口关闭 repeater相同行合并 在Web站点中创建和使用Rss源(动态) silverlight xml查询 silverlight 3 数据绑定及分页 解决ASP.NET中Image控件不能自动刷新 解决UpdatePanel无法直接弹出窗口的问题 导入Excel数值读不到,找不到可安装的 ISAM错误! asp.net2.0 上传大容量文件第三方控件radupload 浏览器不能正常解析CSS代码的解决 ajax在用户注册中的应用,类似淘宝网
[转载]poi导出excel,可以自定义保存路径
叶子绿了 · 2014-03-11 · via 博客园 - 叶子绿了

poi导出excel比js导出excel安全性更好,在使用poi导出excel时,先要导入poi-3.5-FINAL-20090928.jar包到你项目的lib目录下,我这里选择是3.5版的

1.action类

package com.szallway.phr2.portal.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.szallway.phr2.portal.factory.ServiceFactory;
import com.szallway.phr2.portal.po.Account;
import com.szallway.phr2.portal.po.Counter;
import com.szallway.phr2.portal.po.Friend;
import com.szallway.phr2.portal.po.Healthbackground;
import com.szallway.phr2.portal.po.Permission;
import com.szallway.phr2.portal.po.Profile;
import com.szallway.phr2.portal.service.CounterService;
import com.szallway.phr2.portal.service.FriendService;
import com.szallway.phr2.portal.service.HealthbackgroundService;
import com.szallway.phr2.portal.service.PermissionService;
import com.szallway.phr2.portal.service.ProfileService;
import com.szallway.phr2.portal.util.ProfileIDGender;

public class FriendAction extends ActionSupport implements SessionAware,RequestAware, ModelDriven{
 private Map<String, Object> session;
 private Map<String, Object> request;
    private Friend friend;
    private String areacode;
    private int profid;
    private InputStream excelStream;  //定义一输入流
    private String fileName; //文件名  

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public InputStream getExcelStream() {
  return excelStream;
 }

 public void setExcelStream(InputStream excelStream) {
  this.excelStream = excelStream;
 }

 public int getProfid() {
  return profid;
 }

 public void setProfid(int profid) {
  this.profid = profid;
 }

 public String getAreacode() {
  return areacode;
 }

 public void setAreacode(String areacode) {
  this.areacode = areacode;
 }

 public Friend getFriend() {
  return friend;
 }
  
 public void setFriend(Friend friend) {
  this.friend = friend;
 }

 @Override
 public void setSession(Map<String, Object> session) {
  // TODO Auto-generated method stub
         this.session = session;
 }

 @Override
 public Object getModel() {
  // TODO Auto-generated method stub
  friend = new Friend();
  return friend;
 }
 FriendService fser = ServiceFactory.getFriendServieInstance();
 
 //查看所属档案的常用联系人
 public String selectfriendlist()throws Exception
 {
  Profile profile = (Profile)session.get("profile");
  List list = null;
  try{
   list = fser.byprofileid(profile.getProfileid());
  }catch(Exception e){
   e.printStackTrace();
  }
   // 没有紧急联系人,就添加

  session.put("friendlist", list);
  request.put("tabletype", "friendlist");
  request.put("sjtabletype", "sjfriendlist");
  request.put("pagination", "");
  
  return SUCCESS;
 }
 
 //导出所属档案的常用联系人为excel
 public String exportFriend() throws Exception{
  HSSFWorkbook hssfworkbook = new HSSFWorkbook();
  HSSFSheet hssfsheet = hssfworkbook.createSheet();
  HSSFRow hssfrow = hssfsheet.createRow(0);
  //定义表头
  hssfrow.createCell((short)0).setCellValue("姓名");
  hssfrow.createCell((short)1).setCellValue("性别");
  hssfrow.createCell((short)2).setCellValue("所属关系");
  hssfrow.createCell((short)3).setCellValue("手机号码");
  hssfrow.createCell((short)4).setCellValue("电子邮件");
  Profile profile = (Profile)session.get("profile");
  int i = 1;
  Iterator it =  fser.byprofileid(profile.getProfileid()).iterator();   //根据档案id查看他所有的常用联系人,并迭代
  //添加表内容
  while (it.hasNext()) {
   Friend friend = (Friend) it.next();
   hssfrow = hssfsheet.createRow(i);
   hssfrow.createCell((short)0).setCellValue(friend.getFname());
   hssfrow.createCell((short)1).setCellValue(friend.getFgender());
   hssfrow.createCell((short)2).setCellValue(friend.getFrelationship());
   hssfrow.createCell((short)3).setCellValue(friend.getFmobilephone());
   hssfrow.createCell((short)4).setCellValue(friend.getFemail());
   i++;
  }
  FileOutputStream fileoutputstream = null;
  try {
  // fileoutputstream = new FileOutputStream("d:\Friend.xls");
//   hssfworkbook.write(fileoutputstream);
//   fileoutputstream.close();
         this.fileName = "friend";
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      hssfworkbook.write(baos); 
          baos.flush(); 
          byte[] aa = baos.toByteArray(); 
        //  String filename = "friend";
          excelStream = new ByteArrayInputStream(aa,0, aa.length); 
          baos.close(); 

  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   e.printStackTrace();
  }  
  return SUCCESS;
 } 
 @Override
 public void setRequest(Map<String, Object> request) {
  // TODO Auto-generated method stub
  this.request = request;
 }

}
2.struts内定义action

  <!-- 导出常用联系人为excel -->
  <action name="exportFriend" class="com.szallway.phr2.portal.action.FriendAction" method="exportFriend">
  <!--    <result name="success" type="redirectAction">selectfriendlist</result>   -->
      <result name="success" type="stream">  
        <!-- 文件类型 --> 
        <param name="contentType"> application/vnd.ms-excel</param> 
        <!-- excelStream 与对应action中的输入流的名字要一致 --> 
        <param name= " inputName"> excelStream</param>  
        <!-- 文件名 与action中fileName一致 --> 
        <param name="contentDisposition">attachment;filename=" ${fileName}.xls"</param>  
        </result>
  </action>

3.jsp页面

在jsp页面只需加入<a href="exportFriend.action">导出excel</a>这样一连接或者表单按钮

<form action="exportFriend.action" method="post"><input type="submit" value="导出excel"/></from>

4.效果图

 
[转载]poi导出excel,可以自定义保存路径

http://blog.sina.com.cn/s/blog_8394b21401012zbk.html