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

    你可能感兴趣的文章
    netlink2.6.32内核实现源码
    查看>>
    Netpas:不一样的SD-WAN+ 保障网络通讯品质
    查看>>
    NetScaler的常用配置
    查看>>
    netsh advfirewall
    查看>>
    NETSH WINSOCK RESET这条命令的含义和作用?
    查看>>
    Netstat端口占用情况
    查看>>
    Netty WebSocket客户端
    查看>>
    netty 主要组件+黏包半包+rpc框架+源码透析
    查看>>
    Netty 异步任务调度与异步线程池
    查看>>
    Netty中集成Protobuf实现Java对象数据传递
    查看>>
    netty之 定长数据流处理数据粘包问题
    查看>>
    Netty事件注册机制深入解析
    查看>>
    Netty原理分析及实战(四)-客户端与服务端双向通信
    查看>>
    Netty和Tomcat的区别已经性能对比
    查看>>
    Netty学习总结(5)——Netty之TCP粘包/拆包问题的解决之道
    查看>>
    Netty客户端断线重连实现及问题思考
    查看>>
    Netty工作笔记0006---NIO的Buffer说明
    查看>>
    Netty工作笔记0007---NIO的三大核心组件关系
    查看>>
    Netty工作笔记0011---Channel应用案例2
    查看>>
    Netty工作笔记0013---Channel应用案例4Copy图片
    查看>>