note16:大批量数据导出为excel

1、查找符合条件的数据 BReportServiceImpl

     /**
     * 根据条件查询数据,结果是list格式
     * @param queryParams  BInfoForm查询条件实体类
     * @param page
     * @return
     */
    @Override
    public List<Object> selectListForExcelExport(Object queryParams, int page) {
        BInfoForm bInfoForm = (BInfoForm) queryParams;
        // 设置起始行 
        bInfoForm.setRowStart(ExcelUtils.getStartRow(page));
        // 设置结束行
        bInfoForm.setRowEnd(ExcelUtils.getEndRow(page));
        // 查询数据 比正常的筛选查询多了rowNo行号
        List<BInfoBean> bInfo = bReportMapper.getExcelList(bInfoForm);

        bInfo.forEach(bInfoBean -> {
            // 进行sql语句不方便处理的替换,一般省略
        });
        return bInfo.stream().map((item) -> {
            Object obj = item;
            return obj;
        }).collect(Collectors.toList());
    }
*?*
list.stream().map().collect()
stream() 把源数据转换成流
map() 对流中的每个元素执行操作 
      注意:map()只是起到映射作用,不会对原来的list中的元素做出任何改变;forEach()会改变原来list中的元素
collect() 将流转化为集合等
getExcelList:比展示界面的列名多了行号
BInfoBean:展示的实体类 加注释@Excel 
          可以设置列名name 导出类型type 列宽width 导入时间格式importFarmat 导出时间格式exportFormat……

2、导出excel BReportServiceImpl

    /**
     * 导出Excel
     * @param response
     * @param bInfoForm
     * @return
     */
    @Override
    public String excelExport(HttpServletResponse response,BInfoForm bInfoForm) {
        String tokenId = Token.getInstance().getTokenIdFM();
        String title = "统计报表";
        String sheetName = "sheet";
        logger.info("tokenId: " + tokenId + " 开始下载Excel文件");
        try {
            String fileName = "交互统计报表_" + DateUtil.getDateStrNO();
            IExcelExportServer server = SpringHelp.getBean(BReportServiceImpl.class);
            ExcelUtils.excelDownload(title, sheetName, BInfoBean.class, server, bInfoForm, fileName, response);
            logger.info("tokenId: " + tokenId + " 下载Excel文件结束");
            return ReturnFormat.retData("ok");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("下载Excel文件出现异常,报错信息为:" + e.getMessage());
            return ReturnFormat.retParam("500", e.getMessage());
        }
    }

其中,IExcelExportServer

public interface IExcelExportServer {
    List<Object> selectListForExcelExport(Object var1, int var2);
}
*???*
BReportServiceImpl implement BReportService和IExcelExportServer
IExcelExportServer server = SpringHelp.getBean(BReportServiceImpl.class);

3、excelUtils工具类

public class ExcelUtils {
    public static final int queryCount = 5000;
        /**
     * 下载Excel 超过两万条数据使用(大数据量导出)
     *
     * @param title       大标题
     * @param sheetName   页标签
     * @param object      导出实体类
     * @param server      查询接口
     * @param queryParams 查询条件
     * @param fileName    下载文件名
     * @param response    响应
     * @throws Exception
     */
    public static void excelDownload(String title, String sheetName, Class<?> object, IExcelExportServer server,Object queryParams, String fileName, HttpServletResponse response) throws Exception {
        Workbook workbook = null;
        OutputStream outputStream = null;
        try {
            // 导出参数
            ExportParams exportParams = new ExportParams(title, sheetName);
            // 设置导出样式,可以不设置使用默认样式,这里用的是自定义样式
            exportParams.setStyle(ExcelStyle.class);
            // 设置每个sheet装载条数
            int maxNum = 200000;
            exportParams.setMaxNum(maxNum);
            workbook = ExcelExportUtil.exportBigExcel(exportParams, object, server, queryParams);
            // 获取输出流
            outputStream = new BufferedOutputStream(response.getOutputStream());
            // 设置header
            responseSetProperties(fileName, response);
            // 输出表格
            workbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            throw new Exception(e.getMessage(), e);
        } finally {
            if (workbook != null) {
                workbook.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
 /**
     * 设置起始行
     *
     * @param pageNum
     * @return
     */
    public static String getStartRow(int pageNum) {
        return String.valueOf((pageNum - 1) * queryCount);
    }

    /**
     * 设置结束行
     *
     * @param pageNum
     * @return
     */
    public static String getEndRow(int pageNum) {
        return String.valueOf(pageNum * queryCount + 1);
    }
Workbook:工作簿的高级表现形式,是sheet的上级对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值