`
ghost_fly
  • 浏览: 108738 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

java Excel 导出

    博客分类:
  • JAVA
阅读更多
项目中新加的
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[i], 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>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics