[转载]poi导出excel,可以自定义保存路径
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,可以自定义保存路径的更多相关文章
- POI导出EXCEL经典实现
1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则 ...
- Java POI 导出EXCEL经典实现 Java导出Excel
转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
- POI导出EXCEL经典实现(转)
http://www.cnblogs.com/xwdreamer/archive/2011/07/20/2296975.html 1.Apache POI简介 Apache POI是Apache软件基 ...
- poi导出excel数据量过大
问题:使用poi导出excel,数据量过大导致内存溢出 解决思路:1.多sheet导出 2.生成多个excel打包下载 3.生成csv下载 本文使用的是第二个思路,代码如下: poiUtil工具类 p ...
- java解决poi导出excel文字水印,导出excel不可操作问题
首先需求是用户提出导出excel数据需使用水印备注其用途: 其实就是在导出excel的同时带有自定义文字水印的导出. 那么我们首先想到的肯定是以一个什么样的思路去解决该问题,首先查找poi导出exce ...
- poi导出excel
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
- POI导出excel的简单demo
目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
随机推荐
- FTP操作
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- PayPal 开发详解(二):开启【自动返回】和【数据传输】
1.使用我们的商家测试帐号登录 sandbox :http://www.sandbox.paypal.com Business帐号登录 2.登录以后点击:[我的paypal]->[用户信息]-& ...
- Eclipse 中打不开android sdk managerf
今天配置android sdk 的时候,出现了android sdk 打不开的情况.无论直接点击 sdk manager.exe 还是从eclipse启动,都不起作用,双重启(重启eclipse和ad ...
- 为什么swing不适合做桌面软件
http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...
- ping通网关 ping不能外网 DNS无法解析
###ping通网关 ping不能外网 DNS无法解析 客户上不了网 DNS解析不了 首先登陆机器 先查看IP 然后看dns是否正常 然后测试ping网关 ping外网 nslookup ...
- decode行转列,case when,
1.行转列 转之前:
- PHP学习之路 (2)
我们如果坚持用windows做PHP开发,就会错误不断,所以,无论如何都要学习Linux的使用和安装.说实话<鸟哥的linux私房菜>是一本非常不错的书,但是如果你想照着书上面一步一步安装 ...
- Css 书写规范【转】
1. 不同浏览器元素的默认属性有所不同,使用Reset可重置浏览器元素的一些默认属性,以达到浏览器的兼容. /** 清除内外边距 **/ body, h1, h2, h3, h4, h5, h6, h ...
- EMVTag系列1《数据分组》
数据分组的设计在个人化过程中承担着重要的作用.数据分组标识符(DGI)是两字节十六进制数.数据分组标识的第一个字节等于'01'到'1E',表明数据存储的SFI.第二个字节表明SFI记录的记录编号.其他 ...
- [INS-41112] Specified network interface doesnt maintain connectivi
OS: Oracle Linux Server release 6.3 DB: Oracle 11.2.0.3 安装11.2.0.3.0的RAC,在安装GRID时报错: [INS-41112] Spe ...