[转载]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.效果图

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

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

posted on 2014-03-11 15:29  叶子绿了  阅读(2552)  评论(0编辑  收藏  举报

导航