蓝缘基础框架项目-账号导出

导出报表也是比较常见的功能,今天就聊聊导出Excel报表功能的实现吧。

		$("#exportExcel").click("click", function() {//绑定导出按扭
			var f = $('#fenye');
			f.attr('target','_blank');
			f.attr('action','${pageContext.request.contextPath}/background/account/export.html');
			f.submit();
		});

说明:
1、页面导出按钮绑定事件。
2、事件内容是提交表单,表单action请求的是后端导出报表接口。

	@RequestMapping("export")
	public void exportExcel(HttpServletResponse response,Account account) {
		 List<Account> acs = accountService.queryAll(account);
		 POIUtils.exportToExcel(response, "账号报表", acs, Account.class, "账号", acs.size());
	}

导出报表后端接口主要调用poi工具类,同时将response传入过去,直接返回输出文件到浏览器。

/**
     * 采用注解形式导出Excel
     */
    public static void exportToExcel(HttpServletResponse response, String fileName, List objs, Class clazz,
                                     String sheetName, int pageSize) {
        OutputStream out = null;
        try {
            String tempName = new String(fileName.getBytes(), "ISO8859-1");
            response.setHeader("content-disposition", "attachment;filename=" + tempName + ".xls");
            response.setContentType("application/vnd.ms-excel");
            HSSFWorkbook workbook = handleDataToExcel(objs, clazz, sheetName, pageSize);
            out = response.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            logger.error("导出报表Excel异常", e);
        } finally {
            try {
                if (out != null) {
                    out.flush();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.error("关闭流异常", e);
            }
        }
    }

说明:
1、设置返回浏览器格式为文件流格式。
2、将生成的HSSFWorkbook文件格式转换成文件流输出到页面。
3、handleDataToExcel 通过注解方式获得数据,输出excel对象文件。

/**
     *组装数据,生成excel文件     *
     */
    public static HSSFWorkbook handleDataToExcel(List list, Class clazz, String sheetName, int pageSize) throws Exception {

        HSSFWorkbook workbook = null;
        workbook = new HSSFWorkbook();
        // 获取Excel标题
        List<ExcelHeader> headers = getHeaderList(clazz);
        Collections.sort(headers);
        //
        if (null != list && list.size() > 0) {
            int sheetCount = list.size() % pageSize == 0 ? list.size() / pageSize : list.size() / pageSize + 1;
            for (int i = 1; i <= sheetCount; i++) {
                HSSFSheet sheet = null;
                if (!StringUtils.isEmpty(sheetName)) {
                    sheet = workbook.createSheet(sheetName + i);
                } else {
                    sheet = workbook.createSheet();
                }

                HSSFRow row = sheet.createRow(0);
                // 写标题
                CellStyle titleStyle = setCellStyle(workbook, position_title);
                for (int j = 0; j < headers.size(); j++) {
                    ExcelHeader excelHeader = headers.get(j);
                    HSSFCell cell = row.createCell(j);
                    cell.setCellStyle(titleStyle);
                    cell.setCellValue(excelHeader.getTitle());
                    sheet.setColumnWidth(j, excelHeader.getWidth() * 256);
                }

                // 写内容
                Object obj = null;
                CellStyle bodyStyle = setCellStyle(workbook, position_body);
                int begin = (i - 1) * pageSize;
                int end = (begin + pageSize) > list.size() ? list.size() : (begin + pageSize);
                int rowCount = 1;
                for (int n = begin; n < end; n++) {
                    row = sheet.createRow(rowCount);
                    rowCount++;
                    obj = list.get(n);
                    for (int x = 0; x < headers.size(); x++) {
                        Cell cell = row.createCell(x);
                        cell.setCellStyle(bodyStyle);
                        Method method = clazz.getDeclaredMethod(headers.get(x).getMethodName());
                        Object value = method.invoke(obj);
                        if (value instanceof Date) {
                            JsonDateSerializer jsonDateSerializer = new JsonDateSerializer();
                            String formattedDate = jsonDateSerializer.dateFormat.format(new Date());
                            cell.setCellValue(formattedDate);
                        } else if (value instanceof Double) {
                            cell.setCellValue((Double) value);
                        } else if (value instanceof String) {
                            cell.setCellValue((String) value);
                        } else if (value instanceof Integer) {
                            cell.setCellValue((Integer) value);
                        }
                    }
                }
            }
        }
        return workbook;
    }

说明:
1、首先创建HSSFWorkbook对象,并通过类名获取该类的注解信息,拼接标题的内容集合。
2、根据标题内容集合,写入HSSFRow标题行。
3、遍历传入的数据集合,并通过反射调用类函数,获取内容写入每行。
4、将生成的excel对象返回输出到页面。

在这里插入图片描述
软件定制及其他业务
请加微信号:13128600812

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张先生程序猿

谢谢您的打赏,我会持续创作下去

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值