分享

(原创)用jxl输出excel的源代码(送给以前和我一样对excel输出迷茫的同志们)

 魔戒 2007-01-06
(原创)用jxl输出excel的源代码(送给以前和我一样对excel输出迷茫的同志们)

提交时间: Aug 16, 2005 4:03:54 PM
加5分   引用 回复   发消息

以前第一次要求输出excel报表的时候,我上网查了N多资料,用了N久的时间才理清头绪,所以我把我写的这个excel输出的组件发上来给大家,给新手以帮助,也请高手指点指点。

下面是源代码:请大家指教:
/*
* Created on 2005-8-16
* 如需引用,请保留作者名
*/
package com.paic.pacc.common.util;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
* @author lzr
* 根据提供的各个参数进行excel输出的操作
*/
public class ExcelBill {
   
   
    /**
     * 根据excel文件名,字段名,查询结果和排版大小进行excel输出
     * @param filename  文件名 title 标题 characterName 字段中文名 characterId 数据库字段名 查询结果 res
     * @note 注意,本类中结果集是用ibatis生成 每一行记录为一个DTO(javabean)所有记录(DTO)用一个List封装
     */
    public static void excelBill(String fileName,String title,String characterName,String characterId,List res,HttpServletResponse httpResponse){
      try{
        OutputStream os=httpResponse.getOutputStream();
        httpResponse.setContentType("application/vnd.ms-excel");
        httpResponse.setHeader("Content-disposition", "attachment; filename=" + fileName);
        //开始写入excel
        //加标题
    //标题字体
    jxl.write.WritableFont wfc = new jxl.write.WritableFont(WritableFont.COURIER, 18, WritableFont.BOLD,true);
    jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(wfc);
    wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
    wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   
       //字段字体
    jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(WritableFont.COURIER,10, WritableFont.BOLD,true);
    jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(wfc1);
    wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
    wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
   
    //结果字体
    jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
    wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
    wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); 
   
    WritableWorkbook wbook = Workbook.createWorkbook(os);
    //写sheet名称
    WritableSheet wsheet = wbook.createSheet("sheet1", 0);
        Date aaa=new Date();
SimpleDateFormat aSimpleDateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String today = aSimpleDateFormat.format(aaa);

String[] splistCharacterName=characterName.split(",");
String[] splistCharacterId=characterId.split(",");
int characterSize=splistCharacterName.length;
int resSize=res.size();
int i;

for(i=0;i<characterSize;i++){
    //加入字段名
    wsheet.addCell(new jxl.write.Label(i,3,splistCharacterName,wcfFC1));
    System.out.println(splistCharacterName);
   
}
//加入标题
wsheet.mergeCells(0,0,i-1,0);
wsheet.addCell(new Label(0,0,title,wcfFC));
//加入打印时间
wsheet.addCell(new Label(i-2,1,"打印日期:"));
wsheet.addCell(new Label(i-1,1,today)); 
   
//下面开始输出结果
for(int j=0;j<resSize;j++){
    Object obj=res.get(j);
    Class cla=obj.getClass();
    Method methods[]=cla.getDeclaredMethods();
    for(int m=0;m<methods.length;m++){
        for(int k=0;k<splistCharacterId.length;k++) {
        if(methods[m].getName().startsWith("get"+splistCharacterId[k])){
            wsheet.addCell(new Label(k,j+4,String.valueOf(methods[m].invoke(obj,null)),wcfFC2)); 
        }
      }
    }    
}
//写入流中
  wbook.write();
  wbook.close();
  os.close();    
      }catch(IOException e){
          e.printStackTrace(); 
      }catch(Exception ex){
          ex.printStackTrace();
      }
      
    }

}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约