博客
关于我
导出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/

    你可能感兴趣的文章
    NIO笔记---上
    查看>>
    NIO蔚来 面试——IP地址你了解多少?
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    NISP国家信息安全水平考试,收藏这一篇就够了
    查看>>
    NIS服务器的配置过程
    查看>>
    NIS认证管理域中的用户
    查看>>
    Nitrux 3.8 发布!性能全面提升,带来非凡体验
    查看>>
    NiuShop开源商城系统 SQL注入漏洞复现
    查看>>
    NI笔试——大数加法
    查看>>
    NLog 自定义字段 写入 oracle
    查看>>
    NLog类库使用探索——详解配置
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    NLP 模型中的偏差和公平性检测
    查看>>
    Vue3.0 性能提升主要是通过哪几方面体现的?
    查看>>
    NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
    查看>>
    NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
    查看>>
    NLP、CV 很难入门?IBM 数据科学家带你梳理
    查看>>
    NLP三大特征抽取器:CNN、RNN与Transformer全面解析
    查看>>
    NLP入门(六)pyltp的介绍与使用
    查看>>
    NLP学习笔记:使用 Python 进行NLTK
    查看>>