zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    打印杨辉三角
    插值排序
    各种冒泡排序法
    Linux系统命令符01
    2.1博客系统 |基于form组件和Ajax实现注册登录
    python面试笔试题,你都会了吗?快来复习
    1.2博客系统 |登录页| 验证码
    1.1博客系统| 表结构
    第五章:5.2面向对象-绑定方法和非绑定方法| 内置方法 |元类
    11.Django|中间件
  • 原文地址:https://www.cnblogs.com/unique1319/p/6813703.html
Copyright © 2011-2022 走看看