jxl导出

public static void export(Connection conn, ServletOutputStream outputStream){
        
        Statement stmt = null;
        ResultSet rs = null;
        StringBuffer sqlTestCaseList = new StringBuffer()
            .append("  SQL语句   ");
        
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sqlTestCaseList.toString());
        
        /**填写Excel**/
        WritableWorkbook wb = Workbook.createWorkbook(outputStream);
        WritableSheet ws = wb.createSheet("需求报表", 0);
        
        try{
            WritableFont title = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
            WritableCellFormat title_style = new WritableCellFormat(title);
            title_style.setAlignment(jxl.format.Alignment.CENTRE);
            title_style.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            
            WritableFont body = new jxl.write.WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.NO_BOLD);
            WritableCellFormat body_style = new WritableCellFormat(body);
            body_style.setAlignment(jxl.format.Alignment.CENTRE);
            body_style.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            body_style.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            
            int startRowNum = 0;// 起始行
            int startColNum = 0;// 起始列
            int maxColSize = 10;// 最大列数
            // 设置列宽
            ws.setColumnView(0, 10);
            ws.setColumnView(1, 10);
            ws.setColumnView(2, 15);
            ws.setColumnView(3, 60);
            ws.setColumnView(4, 10);
            ws.setColumnView(5, 10);
            ws.setColumnView(6, 15);
            ws.setColumnView(7, 15);
            ws.setColumnView(8, 10);
            ws.setColumnView(9, 30);
            ws.setColumnView(10, 30);
            
            generateCells(ws, startRowNum++, startColNum, 1, 11);  //增加一个空行
            ws.addCell(new Label(startColNum, startRowNum, ""));
            ws.mergeCells(startColNum, startRowNum, startColNum + maxColSize- 1, startRowNum);
            startColNum = 0;
            startRowNum++;
            generateCells(ws, startRowNum++, startColNum, 1, 11);
            //标题
            ws.addCell(new Label(startColNum, startRowNum, "单号",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "问需单类型",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "项目名称",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "问需单名称",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "开发人员",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "状态",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "目标版本号",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "创建人员",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "需求优先级",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            ws.addCell(new Label(startColNum, startRowNum, "创建时间",title_style));
            ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
            startColNum = startColNum + 1;
            
            /**插入数据**/
            for (;rs.next();){
                
                startRowNum++;
                startColNum = 0;
                for(int c = 1;c<11;c++){
                    if(c==10){
                        ws.addCell(new Label(startColNum, startRowNum, rs.getString(c).substring(0,19),body_style));
                        ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
                        continue;
                    }
                    ws.addCell(new Label(startColNum, startRowNum, rs.getString(c),body_style));
                    ws.mergeCells(startColNum, startRowNum, startColNum, startRowNum);
                    startColNum = startColNum + 1;
                }
            }
            wb.write();
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            if(wb!=null) wb.close();
            if(rs!=null) rs.close();
            if(stmt!=null) stmt.close();
        }
    }
原文地址:https://www.cnblogs.com/unique1319/p/6813703.html