jxl生成excel并下载

我采用的是用jxl生成excel文件,然后使用jspsmartupload下载的本地。

以下是生成excel的过程:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.sict.employeeManage.struts.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.liferay.portal.struts.PortletAction;
import com.sict.employeeManage.data.SzEmployee;
import com.sict.employeeManage.help.HibernateUtil2;

/**
 * MyEclipse Struts
 * Creation date: 03-27-2009
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class DownLoadAction extends PortletAction {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 ArrayList list=null;
 public void processAction(
   ActionMapping mapping, ActionForm form, PortletConfig config,
   ActionRequest request, ActionResponse res)
  throws Exception {
  String s=request.getParameter("sql");
  Session session = null;
  try {
   session = HibernateUtil2.openSession();
   session.beginTransaction();
   String sqlQuery="from SzEmployee s "+s;
   Query query=session.createQuery(sqlQuery);
   list=(ArrayList)query.list();
   session.flush();
   session.getTransaction().commit();
  }
  catch (Exception ex) {
   ex.printStackTrace();
  }
  finally {
   HibernateUtil2.closeSession(session);
  }
  
  try{
  expordExcel(list);
  }catch(Exception e){
   e.printStackTrace();
  }
  setForward(request,"download");
 }
 
 public String expordExcel(List elist)
    throws Exception {
  File file = new File("C:/szghTomcat6.0/webapps/employeeManage/excel/test.xls");
  WritableWorkbook wbook = Workbook.createWorkbook(file); // 建立excel文件
  String tmptitle = "职工统计上报"; // 标题
  WritableSheet wsheet = wbook.createSheet("职工表", 0); // sheet名称
  // 设置excel标题
  WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,
          WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
          Colour.BLACK);
  WritableCellFormat wcfFC = new WritableCellFormat(wfont);
  wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
  wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,
          WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
          Colour.BLACK);
  wcfFC = new WritableCellFormat(wfont);
  // 开始生成主体内容               
  wsheet.addCell(new Label(0, 2, "ID"));
  wsheet.addCell(new Label(1, 2, "序号"));
  wsheet.addCell(new Label(2, 2, "姓 名"));
  wsheet.addCell(new Label(3, 2, "性别"));
  wsheet.addCell(new Label(4, 2, "出生日期"));
  wsheet.addCell(new Label(5, 2, "政治面貌"));
  wsheet.addCell(new Label(6, 2, "学 历"));
  wsheet.addCell(new Label(7, 2, "参加工作时间"));
  wsheet.addCell(new Label(8, 2, "是否会员"));
  wsheet.addCell(new Label(9, 2, "单位类别"));
  wsheet.addCell(new Label(10, 2, "产业类别"));
  for(int i=3;i<elist.size()+3;i++)
  {
      wsheet.addCell(new Label(0, i, ((SzEmployee)elist.get(i-3)).getDwbm().toString()));
      wsheet.addCell(new Label(2, i, ((SzEmployee)elist.get(i-3)).getXm().toString()));
      wsheet.addCell(new Label(1, i, (i-2)+""));
      wsheet.addCell(new Label(3, i, ((SzEmployee)elist.get(i-3)).getXb().toString()));
      wsheet.addCell(new Label(4, i, ((SzEmployee)elist.get(i-3)).getCsrq().toString()));
      wsheet.addCell(new Label(5, i, ((SzEmployee)elist.get(i-3)).getZzmm().toString()));
      wsheet.addCell(new Label(6, i, ((SzEmployee)elist.get(i-3)).getXl().toString()));
      wsheet.addCell(new Label(7, i, ((SzEmployee)elist.get(i-3)).getCjgzjj().toString()));
      wsheet.addCell(new Label(8, i, ((SzEmployee)elist.get(i-3)).getSfhy().toString()));
      wsheet.addCell(new Label(9, i,((SzEmployee)elist.get(i-3)).getDwlb().toString() ));
      wsheet.addCell(new Label(10, i, "公共管理和社会组织"));
  }       
  // 主体内容生成结束       
  wbook.write(); // 写入文件
  wbook.close();
  return "success";
  
 }

下面是jspsmartupload的下载过程:

package com.sict.employeeManage.operation;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;


public class DownLoad extends HttpServlet{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 ServletConfig config=null;
 public void init(ServletConfig config) throws ServletException{
  super.init(config);
  this.config=config;
 }
 
 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    // 新建一个SmartUpload对象
   SmartUpload su = new SmartUpload();
    // 初始化
   su.initialize(config, request, response);
    // 设定contentDisposition为null以禁止浏览器自动打开文件,
    //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
    //doc时,浏览器将自动用word打开它。扩展名为pdf时,
    //浏览器将用acrobat打开。
   su.setContentDisposition(null);
    // 下载文件
   try{
   su.downloadFile("C:/szghTomcat6.0/webapps/employeeManage/excel/test.xls");
   }catch(Exception e){
    e.printStackTrace();
   }
 }
 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
  doPost(request,response);
 } 
}

说明:此处由于我是在liferay中处理,采用的方法可能跟普通的不大一样。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值