ghost_fly 发表于 2013-1-28 19:45:46

java Excel 导出

项目中新加的public void createExcel(OutputStream os,List<TbReceiptDocument> docList)throws Exception{         WritableWorkbook wwb = Workbook.createWorkbook(os);         WritableSheet ws = wwb.createSheet("收文登记表", 0);      ws.mergeCells(0, 0, 5, 0);      WritableFont font1 = new WritableFont(WritableFont.TIMES, 16,                                              WritableFont.BOLD);      try{      WritableCellFormat format1 = new WritableCellFormat(font1);      format1.setAlignment(jxl.format.Alignment.CENTRE);      WritableCellFormat cellFormat = new WritableCellFormat();      cellFormat.setAlignment(jxl.format.Alignment.CENTRE);      cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);      cellFormat.setWrap(true);//      文件字号 主办部室 文件标题 附件 接收列表 发文时间      //first row         Label l = new Label(0, 0, "收文登记表", format1);      ws.addCell(l);//      two row    标题         String[] titels={"收文序号", "收文日期", "文件字号", "来文单位", "文件标题", "公司领导指示" };         for(int i=0;i<titels.length;i++) {               l=new Label(i, 1, titels, cellFormat);               ws.addCell(l);         }      ws.setColumnView(0, 15);      ws.setColumnView(1, 20);      ws.setColumnView(2, 20);      ws.setColumnView(3, 20);      ws.setColumnView(4, 20);      ws.setColumnView(5, 20);         int j=0;      for(TbReceiptDocument docBean:docList){         Label labelA = null;            if(docBean.getSequenceNo()!=null){            labelA = new Label(0, j + 2,docBean.getSequenceNo().toString());            }            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);             labelA = new Label(1, j + 2,docBean.getReceiptDtAsStr());            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);            labelA = new Label(2, j + 2,docBean.getFileNo());            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);            labelA = new Label(3, j + 2,docBean.getReceiptDeptIdStr());            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);            labelA = new Label(4, j + 2,docBean.getFileTitle());            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);            List<TbReceiptApproval> fileList=docBean.getComLeaderComments();            StringBuffer fileTile=new StringBuffer();            for(TbReceiptApproval ra:fileList){                fileTile.append(ra.getApprovalComment());            }            labelA = new Label(5, j + 2,fileTile.toString());            labelA.setCellFormat(cellFormat);            ws.addCell(labelA);            j++;      }          // 写入工作表      wwb.write();      wwb.close();      }catch(Exception e){            e.printStackTrace();      }   } 
采用jsp调用:
<%@ page import="java.util.List" %><%@ page import="java.util.ArrayList" %><%@ page import="java.io.OutputStream" %><%@ page import="java.net.URLEncoder" %><%@ page import="com.timeson.oa.receiptdocument.web.ReceiptDocAction"%><html><body><%    response.reset();    response.setContentType("application/msexcel");//    response.setHeader("content-disposition", "attachment;filename=untitled.xls");//(1)    response.setHeader("Content-Disposition", "inline;filename=收文登记表.XLS");//      response.setHeader("Content-disposition","inline;filename=untitled.xls");//定义文件名(2)    OutputStream os = response.getOutputStream();    ReceiptDocAction doc = new ReceiptDocAction();    try    {      List list = (List) request.getAttribute("docList");      doc.createExcel(os, list);//把列表信息导入Excel    } catch(Exception e)    {      e.printStackTrace();    }    os.flush();    os.close();    out.clear();//(3)    out = pageContext.pushBody();//(4)%></body></html> 
页: [1]
查看完整版本: java Excel 导出