博客
关于我
导出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 的 SSL 模块安装
    查看>>
    Nginx 的优化思路,并解析网站防盗链
    查看>>
    Nginx 的配置文件中的 keepalive 介绍
    查看>>
    nginx 禁止以ip形式访问服务器
    查看>>
    Nginx 结合 consul 实现动态负载均衡
    查看>>
    Nginx 负载均衡与权重配置解析
    查看>>
    Nginx 负载均衡详解
    查看>>
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置服务器文件上传与下载
    查看>>
    Nginx 配置清单(一篇够用)
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    Nginx 集成Zipkin服务链路追踪
    查看>>
    nginx 集群配置方式 静态文件处理
    查看>>
    nginx+php的搭建
    查看>>
    nginx+tomcat+memcached
    查看>>
    Nginx+Tomcat实现动静分离
    查看>>
    nginx+Tomcat性能监控
    查看>>
    nginx+uwsgi+django
    查看>>