导出excel

来源:互联网 发布:淘宝号刷多了会怎么样 编辑:程序博客网 时间:2024/04/28 18:41

1==========

package com.belstar.web.batchprint;
import org.apache.struts.action.*;

import com.belstar.dbservice.TInvoicesDAO;
import com.belstar.facade.system.maintain.IMaintainInvoice;
import com.belstar.facade.system.maintain.SystemMaintainFactory;
import com.belstar.util.InvoiceState;

import javax.servlet.http.*;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * <p>Title:DownloadAction </p>
 * <p>Description: QRRSMMS </p>
 * <p>Copyright: Copyright (c) 2004 jiahansoft</p>
 * <p>Company: jiahansoft</p>
 * @author wanghw
 * @version 1.0
 */

public class DownloadAction extends Action {
  public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception {
  
    try{
  List list=(List)request.getSession().getAttribute("downloadList");
     ////////////////////////////////////
  String fname = "test";//Excel文件名
  OutputStream os = response.getOutputStream();//取得输出流
  response.reset();//清空输出流
  response.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls");//设定输出文件头
  response.setContentType("application/msexcel");//定义输出类型
  /////////////////////////////////////
  ExcelBean eb = new ExcelBean();
  eb.expordExcel(list,os);//调用生成excel文件bean
    }catch(Exception e){
      System.out.println(e);
    }

    return mapping.findForward("display");
  }
}

2==========

package com.belstar.web.batchprint;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import com.belstar.util.InvoiceState;

import com.belstar.dbmodel.Invoice;

import jxl.*;
import jxl.write.*;

public class ExcelBean {
 public String expordExcel(List list,OutputStream os)throws Exception{
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件
  String tmptitle = "123123"; //标题
  jxl.write.WritableSheet wsheet = wbook.createSheet("第一页", 0); //sheet名称
  //设置excel标题
  jxl.write.WritableFont wfont = new jxl.write.WritableFont(
      WritableFont.ARIAL, 16,
      WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
      jxl.format.Colour.BLACK);
  jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
      wfont);
  jxl.write.Label wlabel1;
  wlabel1 = new jxl.write.Label(5, 0, tmptitle, wcfFC);
  wsheet.addCell(wlabel1);
  wfont = new jxl.write.WritableFont(
      WritableFont.ARIAL, 14,
      WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE,
      jxl.format.Colour.BLACK);
  wcfFC = new jxl.write.WritableCellFormat(
      wfont);
  jxl.write.Label wlabel;
  wlabel = new jxl.write.Label(0, 1, "hh");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(2, 1, "yy人");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(4, 1, "单号");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(6, 1, "日期");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(8, 1, "机构代码");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(10, 1, "员工号");
  wsheet.addCell(wlabel); //
  wlabel = new jxl.write.Label(12, 1, "状态");
  wsheet.addCell(wlabel); //
  String state="未知状态";
  for(int i=0;i<list.size();i++){
   //javabean
   Invoice invoice=(Invoice)list.get(i);
   wlabel = new jxl.write.Label(0, i+2, invoice.getInvoiceNum());
   wsheet.addCell(wlabel); //
   wlabel = new jxl.write.Label(2, i+2, invoice.getPolicyHoder());
   wsheet.addCell(wlabel); //
   wlabel = new jxl.write.Label(4, i+2, invoice.getInsuranceNum());
   wsheet.addCell(wlabel); //
   wlabel = new jxl.write.Label(6, i+2, dateFormat.format

(invoice.getDateOfIssue()));
   wsheet.addCell(wlabel); //
   wlabel = new jxl.write.Label(8, i+2, invoice.getCompanyCode());
   wsheet.addCell(wlabel); //
   wlabel = new jxl.write.Label(10, i+2, invoice.getPrintUserId());
   wsheet.addCell(wlabel); //
   if(invoice.getState().equals(InvoiceState.PRINT_SUC)){
    state="打印成功";
   }else if(invoice.getState().equals(InvoiceState.HEXIAO)){
    state="核销状态";
   }else if(invoice.getState().equals(InvoiceState.ZUOFEI)){
    state="作废状态";
   }else if(invoice.getState().equals(InvoiceState.UN_PRINT)){
    state="未打印";
   }else if(invoice.getState().equals(InvoiceState.CHECK_SUC)){
    state="待核销";
   }else{
    state="未知状态";
   }
   wlabel = new jxl.write.Label(12, i+2, state);
   wsheet.addCell(wlabel); //
  }
  wbook.write(); //写入文件
  wbook.close();
  os.close();
  return "success";
 }
}