weblogic 从数据库生成excel文件(打包发布方式)

来源:互联网 发布:医院数据安全管理制度 编辑:程序博客网 时间:2024/06/11 04:02

本来是比较简单的东西,但是我的web项目是打包发布的,所以,得不到发布项目的物理路径,只好把生成的 excel生成下载到客户机本地   采用struts结构,在action中取得数据,放入一个vector 中,将此vector置入session里,,jsp代码中生成excel,jsp代码如下:

 

 

<%@ page language="java" contentType="text/html;charset=GB2312"%>
<%@ page import="java.io.*"%>
<%@ page import="jxl.Workbook"%>
<%@ page import="jxl.write.*"%>
<%@ page import="jxl.write.WritableWorkbook.*"%>
<%@ page import="jxl.*"%>
<%@ page import="com.agenda.tdyhweb.util.ExcelTools"%>
<%@ page import="java.util.Vector"%>
<%@ page import="java.util.Vector"%>
<%@ page import="com.agenda.tdyhweb.bean.UserInfo"%>

 <%  response.reset();
  response.setHeader("Content-disposition",  "attachment;inline;  filename=file.xls");//设定输出文件头
  response.setContentType("application/vnd.ms-excel");//定义输出类型
  try
  {
          OutputStream  os  =  response.getOutputStream();       
        
           
         WritableFont  wf  =  new  WritableFont( WritableFont.TIMES,12,WritableFont.NO_BOLD,false); 
         WritableCellFormat  wcfF  =  new  WritableCellFormat(wf); 
         WritableWorkbook  wwb  =  Workbook.createWorkbook(os);
         WritableSheet  ws  =  wwb.createSheet("Test  Sheet  1",  0);
   String usertype="";
         //Label  labelC  =  new  Label(0,0,"姓名",wcfF);
         //ws.addCell(labelC);
          Vector uv=new Vector();
            ExcelTools tool=new ExcelTools();
          tool.addCells(ws, 0, 0, "姓名");
         tool.addCells(ws, 1, 0, "证件号码");
      tool.addCells(ws, 2, 0, "性别");
   tool.addCells(ws, 3, 0, "生日");
   tool.addCells(ws, 4, 0, "电子邮件");
   tool.addCells(ws, 5, 0, "家庭电话");
   tool.addCells(ws, 6, 0, "公司电话");
   tool.addCells(ws, 7, 0, "手机号码");
   tool.addCells(ws, 8, 0, "联系地址");
   tool.addCells(ws, 9, 0, "邮政编码");
   tool.addCells(ws, 10, 0, "职业");
   tool.addCells(ws, 11, 0, "泰达用户类型");
   tool.addCells(ws, 12, 0, "投资金额");
   tool.addCells(ws, 13, 0, "从何处得知");
   tool.addCells(ws, 14, 0, "其他信息");

   tool.addCells(ws, 15, 0, "是否免费索取理财专刊");
   tool.addCells(ws, 16, 0, "是否派专人联系");
   tool.addCells(ws, 17, 0, "用户类型");
   uv=(Vector)session.getAttribute("outputgame");
   for (int i = 0; i < uv.size(); i++) {
    UserInfo fbean = (UserInfo) uv.get(i);
    if (fbean.getUsertype() == 0)
     usertype = "网上交易客户";
    if (fbean.getUsertype() == 1)
     usertype = "非网上交易客户";
    if (fbean.getUsertype() == 2)
     usertype = "不是泰达荷银客户";
    tool.addCells(ws, 0, i + 1, fbean.getUsername());
    tool.addCells(ws, 1, i + 1, fbean.getIdentycode());
    tool.addCells(ws, 2, i + 1, fbean.getSex() == 0 ? "男" : "女");
    tool.addCells(ws, 3, i + 1, fbean.getBirthdy());
    tool.addCells(ws, 4, i + 1, fbean.getEmail());
    tool.addCells(ws, 5, i + 1, fbean.getHometel());
    tool.addCells(ws, 6, i + 1, fbean.getCompanytel());
    tool.addCells(ws, 7, i + 1, fbean.getMobile());
    tool.addCells(ws, 8, i + 1, fbean.getAddress());
    tool.addCells(ws, 9, i + 1, fbean.getPostcode());
    tool.addCells(ws, 10, i + 1, fbean.getJob());
    tool.addCells(ws, 11, i + 1, usertype);
    tool.addCells(ws, 12, i + 1, fbean.getMoney());
    tool.addCells(ws, 13, i + 1, fbean.getComefrom());
    tool.addCells(ws, 14, i + 1, fbean.getNote());
    tool.addCells(ws, 15, i + 1, fbean.getFreeget() == 1 ? "希望得到免费专刊"
      : "不需要免费专刊");
    tool.addCells(ws, 16, i + 1, fbean.getContact() == 1 ? "请专人联系"
      : "不需要专人联系");
    tool.addCells(ws, 17, i + 1, "游戏用户");


   }
    wwb.write();
          wwb.close();
          os.flush();
          os.close();
         
  }
  catch(Exception  e){
          e.printStackTrace();
        }
       
       
      
 
 %>

其中addcells是调用

package com.agenda.tdyhweb.util;

public class  ExcelTools{

public  static void addCells(jxl.write.WritableSheet ws, int cloum, int row,
   String str) {
  try {
   jxl.write.Label labelC = new jxl.write.Label(cloum, row, str);
   ws.addCell(labelC);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}中的方法,使用的时候注意导入jxl包