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.效果图

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

[转载]poi导出excel,可以自定义保存路径的更多相关文章

  1. POI导出EXCEL经典实现

    1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...

  2. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  3. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

  4. POI导出EXCEL经典实现(转)

    http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...

  5. poi导出excel数据量过大

    问题:使用poi导出excel,数据量过大导致内存溢出 解决思路:1.多sheet导出 2.生成多个excel打包下载 3.生成csv下载 本文使用的是第二个思路,代码如下: poiUtil工具类 p ...

  6. java解决poi导出excel文字水印,导出excel不可操作问题

    首先需求是用户提出导出excel数据需使用水印备注其用途: 其实就是在导出excel的同时带有自定义文字水印的导出. 那么我们首先想到的肯定是以一个什么样的思路去解决该问题,首先查找poi导出exce ...

  7. poi导出excel

    Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...

  8. POI导出excel的简单demo

    目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...

  9. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

随机推荐

  1. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...

  2. Windowsphone本地应用信息与市场信息的获取

    本地信息都存放在 WMAppManifest 里面,获取就不用说了...知道位置 就知道怎么获取了.. 主要是讲那个 市场上面的详情怎么获取,就是API调用显示在这个页面里面的详情: public v ...

  3. Ubuntu系统使用记录(持续更新)

    本篇文章记录在虚拟机上跑Ubuntu16.04遇到的一系列问题,熟悉一下Ubuntu的相关操作,进入终端的方法ctrl+alt+t. 1.修改屏幕分辨率,进入系统默认的是800x600 即便能够进入s ...

  4. Linux使用有线上网教程

    本人亲测Linux(Ubuntu kylin 14.04)有线上网方法,下面是步骤: 一,运行Terminal(终端),输入  sudo pppoeconf  命令,设置账号和密码后,其他的全选yes ...

  5. debug版本和release版本的区别?

    好久没有做web项目了,这项目完成了要发布网站,不好忘了 以前操作过的? 还好脑子还是有点印象 现还是 写个文档吧记录吧 免得 以后作别的了又忘了 那可不妙啊 网站发布步骤:1.先将

  6. 显示和隐藏Mac隐藏文件的终端命令

    打开终端,输入以下命令: 显示mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏mac隐藏文件的命令 ...

  7. Webservice SOAP传输序列化总结 以及webservice之序列化以及反序列化实例

    一.所有Webservice中传递的对象都必须能够序列化,这个是作为在网络之间传输的必要条件.XML WebService和SOAP标准支持的数据类型如下: 1.基本数据类型. 标准类型,如:int ...

  8. Silverlight中DataPager控件扩展

    大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到Da ...

  9. 一些CSS"bug"

    1.img三像像素问题 解决办法:img{display:block;} or img{vertical-align:middle;} 问题原因:img是行内元素,默认的垂直对齐方式 baseline ...

  10. 7-ZIP实现数据高度压缩

    From:http://www.cnblogs.com/killerlegend/p/3746395.html Author:KillerLegend Date:2013.5.22 选中文件,鼠标右键 ...