• 导出Excel表格


    /**
         * 导出验证用户的操作日志的表格
         *
         * @throws Exception
         */
        public static void exportWeChatUserLogs(String searchCriteria) {
            // 根据需求生成excel表格
            HSSFWorkbook wb = createHSSFWorkbook(searchCriteria);
            // 第六步,将文件存到指定位置

            // 设置对话框的风格
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 选择目录
            JFileChooser chooser = new JFileChooser();

            Date dt = new Date();// 如果不需要格式,可直接用dt,dt就是当前系统时间
            DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssS");// 设置显示格式
            String nowTime = "";
            nowTime = df.format(dt);
            // 设置默认的保存文件的文件名:当前时间.xls格式
            chooser.setSelectedFile(new File(nowTime + ".xls"));
            // 设置保存对话框的各种样式属性
            chooser.setDialogTitle(Messages.get("save_file"));
            chooser.setApproveButtonText(Messages.get("save"));
            chooser.setApproveButtonToolTipText(Messages.get("save"));
            chooser.setFocusable(true);
            chooser.setToolTipText(Messages.get("save_file"));
            chooser.setDragEnabled(true);// 设置 dragEnabled 属性,要在此组件上启用自动拖动处理(drag 和
                                            // drop 的第一部分),此属性必须为 true
            chooser.setFileFilter(new FileNameExtensionFilter("Excel", new String[] { ".xls"}));

            int result = chooser.showOpenDialog(null);
            switch (result) {
            case JFileChooser.APPROVE_OPTION:
                // 这一种方法是把用户输入的作为保存的文件名
                String filePath = chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName() + ".xls";
                try {
                    // 输出文件流
                    FileOutputStream fout = new FileOutputStream(filePath);
                    wb.write(fout);
                    fout.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 关闭对话框,跳转到微信用户操作日志展示的页面
                weChatUserStatisticalLogs(0, searchCriteria);
                break;
            case JFileChooser.CANCEL_OPTION:
                // 选择取消按钮,直接跳到微信用户操作日志展示的页面
                weChatUserStatisticalLogs(0, searchCriteria);
            case JFileChooser.ERROR_OPTION:
                // 出错时,直接跳到微信用户操作日志展示的页面
                weChatUserStatisticalLogs(0, searchCriteria);
                break;
            default:
                break;
            }

        }

        /**
         * 返回生成的excel表格HSSFWorkbook对象
         *
         * @param searchCriteria
         * @return
         */
        public static HSSFWorkbook createHSSFWorkbook(String searchCriteria) {
            // 第一步,创建一个webbook,对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(Messages.get("wechatuser_log_statistical_table"));
            // 创建单元格样式
            HSSFCellStyle cellStyle = wb.createCellStyle();

            // 指定单元格居中对齐
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            // 指定单元格垂直居中对齐
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

            // 指定当单元格内容显示不下时自动换行
            cellStyle.setWrapText(true);

            // 第四步,创建单元格,并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

            // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short:所有用户的日志的合计
            HSSFRow rowAllCount = sheet.createRow((int) 0);
            // 第一行的第一列
            HSSFCell cell = rowAllCount.createCell(0);
            cell.setCellValue(new HSSFRichTextString(Messages.get("total_statistical_num")));
            cell.setCellStyle(style);
            // 第一行的第二列
            cell = rowAllCount.createCell(1);
            cell.setCellValue(new HSSFRichTextString(Messages.get("addtask_num_all")));
            cell.setCellStyle(style);
            // 第一行的第三列
            cell = rowAllCount.createCell(2);
            cell.setCellValue(new HSSFRichTextString(Messages.get("transtask_num_all")));
            cell.setCellStyle(style);
            // 第一行的第四列
            cell = rowAllCount.createCell(3);
            cell.setCellValue(new HSSFRichTextString(Messages.get("submit_num_all")));
            cell.setCellStyle(style);
            // 第一行的第五列
            cell = rowAllCount.createCell(4);
            cell.setCellValue(new HSSFRichTextString(Messages.get("reject_num_all")));
            cell.setCellStyle(style);

            // 第二行:各类型的所有统计数
            HSSFRow rowAllCountNum = sheet.createRow((int) 1);
            Map<String, Long> countMap = WeChatUserStatisticalBean.countSatisticNum();
            rowAllCountNum.createCell(0).setCellValue(new HSSFRichTextString(Messages.get("statistical_string")));// 第二行第一列
            rowAllCountNum.createCell(1).setCellValue(countMap.get("addTaskAllNum"));// 第二行第一列
            rowAllCountNum.createCell(2).setCellValue(countMap.get("transTaskAllNum"));// 第二行第二列
            rowAllCountNum.createCell(3).setCellValue(countMap.get("submitAllNum"));// 第二行第三列
            rowAllCountNum.createCell(4).setCellValue(countMap.get("rejectAllNum"));// 第二行第三列

            // 第三行:各类型合计的统计数
            HSSFRow rowType = sheet.createRow((int) 2);
            cell = rowType.createCell(0);

            // 判断导出的是哪种类查询条件的统计数据
            if (!"".equals(searchCriteria) && searchCriteria != null && "approvalType".equals(searchCriteria)) {
                // 导出的是根据审批类型进行查询的统计数据
                cell.setCellValue(new HSSFRichTextString(Messages.get("all_approval_type")));// 第三行第一列
            } else {
                // 导出的是根据用户名查询的统计数据
                cell.setCellValue(new HSSFRichTextString(Messages.get("username")));// 第三行第一列
            }

            cell = rowType.createCell(1);

            cell.setCellValue(new HSSFRichTextString(Messages.get("addtask_num")));// 第三行第二列
            cell.setCellStyle(style);
            cell = rowType.createCell(2);
            cell.setCellValue(new HSSFRichTextString(Messages.get("transtask_num")));// 第三行第三列
            cell.setCellStyle(style);
            cell = rowType.createCell(3);
            cell.setCellValue(new HSSFRichTextString(Messages.get("submit_num")));// 第三行第四列
            cell.setCellStyle(style);
            cell = rowType.createCell(4);
            cell.setCellValue(new HSSFRichTextString(Messages.get("reject_num")));// 第三行第五列
            cell.setCellStyle(style);

            // 第五步,写入实体数据 实际应用中这些数据从数据库得到,

            // 根据条件查询的所有WeChatUserStatisticalBean对象的集合
            List<WeChatUserStatisticalBean> weChatUserStatisticalBeans = new ArrayList<WeChatUserStatisticalBean>();
            // 判断是哪种搜索条件下的excel表格的生成
            if (!"".equals(searchCriteria) && searchCriteria != null && "approvalType".equals(searchCriteria)) {
                // 导出的是根据审批类型进行查询的统计数据
                weChatUserStatisticalBeans = WeChatUserStatisticalBean.getWeChatUserApprovalTypeStatistic(searchCriteria);
            } else {
                // 导出的是根据用户名查询的统计数据
                // 查询所有的微信操作日志的不同名的操作者
                String sqlStr = Const.SQL_LOG_OPERATORS;
                Query query = Log.em().createNativeQuery(sqlStr);
                List<String> operators = query.getResultList();
                for (String operator : operators) {
                    // 根据操作者查询日志统计对象WeChatUserStatisticalBean
                    WeChatUserStatisticalBean weChatUserStatisticalBean = WeChatUserStatisticalBean.initWeChatUserStatisticalBean(operator);
                    // 将对象存入集合中
                    weChatUserStatisticalBeans.add(weChatUserStatisticalBean);
                }
            }
            for (int i = 0; i < weChatUserStatisticalBeans.size(); i++) {
                // 第四步,创建单元格,并设置值
                HSSFRow rowNum = sheet.createRow((int) 2 + i + 1);
                rowNum.createCell(0).setCellValue(weChatUserStatisticalBeans.get(i).realname);
                rowNum.createCell(1).setCellValue(weChatUserStatisticalBeans.get(i).addTaskNum);
                rowNum.createCell(2).setCellValue(weChatUserStatisticalBeans.get(i).transTaskNum);
                rowNum.createCell(3).setCellValue(weChatUserStatisticalBeans.get(i).submitNum);
                rowNum.createCell(4).setCellValue(weChatUserStatisticalBeans.get(i).rejectNum);
            }
            return wb;
        }

  • 相关阅读:
    ArchLinux安装
    数据库优化空间换时间优化
    SQL server 系统优化通过执行计划优化索引(2)
    SQL 语句技巧递归查询机构和下属机构的用户数
    SQL语句技巧复杂逻辑的SQL简单实现(2)
    用最少的成本获得最大收益――论DBA在企业可持续发展中的价值
    sql server性能分析定时收集系统运行情况
    SQL server 数据库自动备份
    Sql Server 2005 实现Oracle 10g的rangelist/range等组合分区功能
    关于Oracle学习以及DBA工作机会(转)
  • 原文地址:https://www.cnblogs.com/xunfang123/p/4196174.html
Copyright © 2020-2023  润新知