博客
关于我
导出Excel功能
阅读量:387 次
发布时间:2019-03-05

本文共 6516 字,大约阅读时间需要 21 分钟。

导出Excel功能实现

Controller逻辑分析

在实际工作中,常需要将数据库中的数据导出为Excel文件。以下是实现导出功能的Controller逻辑说明:

@RequestMapping(value = {"/ledger_fdi_reinvest_rpt_load"}, method = RequestMethod.POST)@ResponseBodypublic void downLoadExcel(Page page, HttpServletRequest request, HttpServletResponse response) throws Exception {    page.setPageSize(Integer.MAX_VALUE);    page.setPageNo(1);    QueryFilter queryFilter = new QueryFilter();    //级联查询目前只支持EQFilter 一级级联    String innerCorpName = request.getParameter("innerCorpName");    if (StringUtils.isNotEmpty(innerCorpName)) {        queryFilter.setLikeFilter("innerCorpName", innerCorpName);    }    String outCorpName = request.getParameter("outCorpName");    if (StringUtils.isNotEmpty(outCorpName)) {        queryFilter.setLikeFilter("outCorpName", outCorpName);    }    String outCnyCde = request.getParameter("outCnyCde");    if (StringUtils.isNotEmpty(outCnyCde)) {        queryFilter.setEQFilter("cnyCde.cnyCde", outCnyCde);    }    String vocCatCde = request.getParameter("vocCatCde");    if (StringUtils.isNotEmpty(vocCatCde)) {        queryFilter.setEQFilter("vocCde.vocCatCde.vocCde", vocCatCde);    }    String outVocCde = request.getParameter("outVocCde");    if (StringUtils.isNotEmpty(outVocCde)) {        queryFilter.setEQFilter("vocCde.vocCde", outVocCde);    }    String rptYear = request.getParameter("rptYear");    if (StringUtils.isNotEmpty(rptYear)) {        queryFilter.setEQFilter("rptYear", rptYear);    }    page.setOrder("desc");    page.setOrderBy("rptYear");    Page
pages = this.commonService.findSupportCascade(page, queryFilter.getPropertyFilters(), LedgerFdiReinvestRptTab.class); getdownLoadDatas(pages.getResult(), request, response);}

Excel生成工具类细节

在项目中,为了实现Excel导出功能,我们使用了ExcelUtil工具类。以下是工具类的主要实现逻辑:

public class ExcelUtil {    private ExcelUtil() {}        public static void exportExcel(String sheetName, String title, int fontHeight, String[] headName, List
> list, int listSize, String[] headCode, String fileName, HttpServletRequest request, HttpServletResponse response) { try { HSSFWorkbook wb = new HSSFWorkbook(); HSSFCellStyle styleForTitle = wb.createCellStyle(); styleForTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); styleForTitle.setFillForegroundColor(HSSFColor.WHITE.index); styleForTitle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont fontForTitle = wb.createFont(); fontForTitle.setColor(HSSFColor.BLACK.index); fontForTitle.setFontHeightInPoints((short) 16); fontForTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); styleForTitle.setFont(fontForTitle); HSSFCellStyle styleForHead = wb.createCellStyle(); styleForHead.setAlignment(HSSFCellStyle.ALIGN_CENTER); styleForHead.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); styleForHead.setFillForegroundColor(HSSFColor.WHITE.index); styleForHead.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleForHead.setBorderBottom(HSSFCellStyle.BORDER_THIN); styleForHead.setBorderLeft(HSSFCellStyle.BORDER_THIN); styleForHead.setBorderRight(HSSFCellStyle.BORDER_THIN); styleForHead.setBorderTop(HSSFCellStyle.BORDER_THIN); HSSFFont fontForHead = wb.createFont(); fontForHead.setColor(HSSFColor.BLACK.index); fontForHead.setFontHeightInPoints((short) 12); fontForHead.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); styleForHead.setFont(fontForHead); HSSFCellStyle styleForContent = wb.createCellStyle(); styleForContent.setAlignment(HSSFCellStyle.ALIGN_LEFT); styleForContent.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); styleForContent.setFillForegroundColor(HSSFColor.WHITE.index); styleForContent.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); styleForContent.setWrapText(true); HSSFFont fontForContent = wb.createFont(); fontForContent.setFontHeightInPoints((short) 11); fontForContent.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); styleForContent.setFont(fontForContent); Row row = sheet.createRow(0); for (int i = 0; i < headName.length; i++) { row.createCell(i); } sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headName.length - 1)); Cell cellMerged = row.getCell(0); cellMerged.setCellStyle(styleForTitle); cellMerged.setCellType(HSSFCell.CELL_TYPE_STRING); cellMerged.setCellValue(title); row = sheet.createRow(1); for (int k = 0; k < headName.length; k++) { cell = row.createCell(k); cell.setCellValue(headName[k]); cell.setCellStyle(styleForHead); } for (int i = 0; i < listSize; i++) { if (list.get(i) != null) { Row row = sheet.createRow(i + 2); Map
hmTemp = list.get(i); String data = ""; for (int j = 0; j < headName.length; j++) { data = hmTemp.get(headName[j]) == null ? "" : hmTemp.get(headName[j]).toString(); Cell ce = row.createCell(j + 1); ce.setCellValue(data); ce.setCellStyle(styleForContent); } } } response.reset(); response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); response.setContentType("application/vnd.ms-excel;charset=utf-8"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); wb.write(toClient); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } }}

功能解释

  • Controller逻辑

    • 通过@RequestMapping定义路由,接收POST请求
    • 设置页面大小为最大值,默认显示第一页
    • 创建QueryFilter对象,用于数据库查询过滤
    • 根据请求参数构建级联查询过滤器
    • 调用服务层方法获取数据
    • 调用getdownLoadDatas方法处理数据并导出
  • Excel工具类

    • 创建新的Excel文件HSSFWorkbook
    • 定义多种样式HSSFCellStyle,用于标题、表头和内容区域
    • 设置默认列宽和行高
    • 创建并添加表头行,使用合并单元格处理
    • 根据数据列表生成Excel行,设置单元格值和样式
    • 处理不同浏览器的文件名编码问题
    • 输出Excel文件到响应流
  • 注意事项

    • IE浏览器对文件名编码的支持有限,需要特殊处理
    • 文件名中避免使用特殊字符,确保兼容性
    • 数据导出前需确保数据库查询结果正确
  • 以上是完整的导出Excel功能实现逻辑,适用于实际项目中的数据报表导出需求。

    转载地址:http://oedzz.baihongyu.com/

    你可能感兴趣的文章
    nginx反向代理、文件批量改名及统计ip访问量等精髓总结
    查看>>
    Nginx反向代理与正向代理配置
    查看>>
    Nginx反向代理及负载均衡实现过程部署
    查看>>
    Nginx反向代理和负载均衡部署指南
    查看>>
    Nginx反向代理是什么意思?如何配置Nginx反向代理?
    查看>>
    nginx反向代理解决跨域问题,使本地调试更方便
    查看>>
    nginx反向代理转发、正则、重写、负摘均衡配置案例
    查看>>
    Nginx反向代理配置
    查看>>
    Nginx启动SSL功能,并进行功能优化,你看这个就足够了
    查看>>
    nginx启动脚本
    查看>>
    Nginx在Windows上和Linux上(Docker启动)分别配置基本身份认证示例
    查看>>
    Nginx在Windows下载安装启动与配置前后端请求代理
    查看>>
    Nginx多域名,多证书,多服务配置,实用版
    查看>>
    nginx如何实现图片防盗链
    查看>>
    Nginx学习总结(12)——Nginx各项配置总结
    查看>>
    Nginx学习总结(13)——Nginx 重要知识点回顾
    查看>>
    Nginx学习总结(14)——Nginx配置参数详细说明与整理
    查看>>
    Nginx学习总结(15)—— 提升 Web 应用性能的十个步骤
    查看>>
    Nginx学习总结(8)——Nginx服务器详解
    查看>>
    nginx学习笔记002---Nginx代理配置_案例1_实现了对前端代码的方向代理_并且配置了后端api接口的访问地址
    查看>>