java web导出excel并下载

java web生成excel并下载(jxl)  

 

在Action中:

public ActionForward export(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

request.setCharacterEncoding("UTF-8");

String currentDay = DateUtil.convertDateToString(new Date(),

"yyyy-MM-dd HH:mm:ss");

String fname = "统计表"+currentDay+".xls";//Excel文件名字

String sql = “xxxx”;

List list = getService().findList(sql);

 

try{

OutputStream os = response.getOutputStream();

response.reset();

response.setHeader("Content-disposition",

"attachment;filename="+new String(fname.getBytes("GBK"),"ISO8859-1"));

response.setContentType("application/msexcel;charset=UTF-8");

 

exportExcel(os,list);

}catch(IOException e){

e.printStackTrace();

}catch(Exception o){

o.printStackTrace();

}finally{

 

}

return null;

}

 

实际生成Excel的方法:

public void exportExcel(OutputStream os, List list) throws Exception {


 

int row = 2;// 从第三行开始写

int col = 0;// 从第一列开始写


 

WritableWorkbook wwb = Workbook.createWorkbook(os);//创建Excel文件

WritableSheet ws = wwb.createSheet("流程统计", 0);//创建sheet

//设置表头

Label label = new Label(0,0,"");

ws.addCell(label);

label = new Label(0,0,"流程类别");

ws.addCell(label);

label = new Label(1,0,"流程名称");

ws.addCell(label);

label = new Label(2,0,"使用次数");

ws.addCell(label);

label = new Label(3,0,"平均处理天数");

ws.addCell(label);

label = new Label(4,0,"流程负责人");

ws.addCell(label);

 

if (list != null && list.size() > 0) {

for (int i = 0; i < list.size(); i++) {

ReviewUsageVo ruv = (ReviewUsageVo) list.get(i);


 

ws.addCell(new Label(col++, row, ruv.getFdCategoryName()));// 

ws.addCell(new Label(col++, row, ruv.getFdTemplateName()));// 

ws.addCell(new Label(col++, row, ruv.getCount()));// 

ws.addCell(new Label(col++, row, ruv.getAverageTime()));// 

ws.addCell(new Label(col++, row, ruv.getReviewOwner()));// 

row++;

col = 0;

}

}

//excel表格中一些字符格式转换

1 添加带有字型的formatting对象
    WritableFont wf = new
    WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
    WritableCellFormat wcf = new WritableCellFormat(wf);
    Label labelCF = new Label(1,0,"This is a Label Cell",wcf);
    ws.addCell(labelCF);
2 添加带有字体颜色的formatting对象
    WritableFont wfc = new
    WritableFont  (WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
    WritableCellFormat wcfF = new WritableCellFormat(wfc);
    Label labelCFC = new Label(1,0,"This is a Label Cell",wcfF);
    ws.addCell(labelCFC);
    //2 添加number对象
    jxl.write.Number num = new jxl.write.Number(0,1,3.1415926);
    ws.addCell(num);
3 添加带有formatting的Number对象
    NumberFormat nf = new NumberFormat("#.##");
    WritableCellFormat wcfN = new WritableCellFormat(nf);
    jxl.write.Number labelNF = new
    jxl.write.Number(1,1,3.1415926,wcfN);
    ws.addCell(labelNF);
4 添加boolean对象
    Boolean labelB = new Boolean(0,2,false);
    ws.addCell(labelB);
5 添加DateTime对象
    DateTime labelDT = new DateTime(0,3,new java.util.Date());
    ws.addCell(labelDT);
6 添加带有formatting的dateformat对象
    DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
    WritableCellFormat wcfDF = new WritableCellFormat(df);
    DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
    ws.addCell(labelDTF);


 

wwb.write();

wwb.close();

os.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值