SpringCloud把xml报文导出Excel(csv格式)文档

    导出excel报表之类,相信有过1~2年开发经验的至少都做过了。但是大多应该都是传统的SSH或SSM架构,相对于在最近流行的SpringCloud分布式架构上做类似导出,可能经历不是那么多。

  鄙人做过的导出excel报表,有2种方案:

  1.  Poi原生的,jar类库
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.9</version>
    		</dependency>
    HSSFWorkbook、HSSFSheet、HSSFCellStyle等一系列,poi包中类库。优势:可以根据业务需要最大限度的格式化excel报表(合并单元格/居中/字体颜色等);此种方案见另一篇资料:
    poi格式导出excelpoi方法参考另一篇博文:poi方法导出excel文档
  2. apache系列的commons-csv类库:CSVPrinter
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-csv</artifactId>
                <version>1.4</version>
            </dependency>

    由于本人项目中,之前统一了导出方案由:CSVPrinter格式,也妥协于api-zuul网关的输入输出流格式的限制,最终选用此种。优势:可与查询接口合并,查询数据供前端页面调用,导出直接输出excel文档。

   今天跟大家分享一下,在SpringCloud架构下,导出excel报表的经验。同时又错误或遗漏之处欢迎指正建议,感激。

xml报文内容格式:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <SttlCntNb>2</SttlCntNb>
    <DebitCntAmt>CNY0.00</DebitCntAmt>
    <CreditCntAmt>CNY4700.00</CreditCntAmt>
    <SttlList>
        <SttlInf>
            <SttlReptFlg>2018052500170139</SttlReptFlg>
            <SttlDCFlg>2</SttlDCFlg>
            <SttlAmt>CNY100.00</SttlAmt>
            <BatchList>
                <BatchInf>
                    <BatchId>B201805230015</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY100.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0113|C3228644000018|04|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0120|C3228640000020|05|CNY50.00|1|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0114|C3228640000029|07|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0115|C3228640000019|06|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0111|C3228640000016|03|CNY0.00|0|CNY10.00|1|</SubItemInf>
                        <SubItemInf>0110|C3228644000016|01|CNY0.00|0|CNY110.00|1|</SubItemInf>
                        <SubItemInf>0112|C3228644000017|99|CNY0.00|0|CNY5.00|1|</SubItemInf>
                        <SubItemInf>0110|C3228644000016|02|CNY0.00|0|CNY10.00|1|</SubItemInf>
                    </SubItemList>
                </BatchInf>
            </BatchList>
        </SttlInf>
        <SttlInf>
            <SttlReptFlg>2018052500170138</SttlReptFlg>
            <SttlDCFlg>2</SttlDCFlg>
            <SttlAmt>CNY4600.00</SttlAmt>
            <BatchList>
                <BatchInf>
                    <BatchId>B201805240001</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0126|C1010511003703|00|CNY0.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0125|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0126|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0124|C1010211000012|01|CNY0.00|8|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0123|C1010211000012|01|CNY0.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0110|C1010211000012|01|CNY0.00|0|CNY400.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240002</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0125|C1010211000012|00|CNY400.00|4|CNY0.00|0|</SubItemInf>
                        <SubItemInf>0110|C1010211000012|00|CNY0.00|0|CNY800.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240003</BatchId>
                    <BatchDCFlg>1</BatchDCFlg>
                    <BatchNetAmt>CNY0.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0120|C1010211000012|00|CNY0.00|4|CNY0.00|0|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240004</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0112|C1010211000012|01|CNY0.00|0|CNY1200.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240005</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0113|C1010211000012|00|CNY0.00|0|CNY400.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240006</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0114|C1010211000012|00|CNY0.00|0|CNY400.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240007</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0115|C1010211000012|01|CNY0.00|0|CNY200.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240008</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY600.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0115|C1010211000012|01|CNY0.00|0|CNY600.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240009</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY400.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0110|C1010511003703|00|CNY0.00|0|CNY400.00|2|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240010</BatchId>
                    <BatchDCFlg>2</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0111|C1010211000012|01|CNY0.00|0|CNY1200.00|4|</SubItemInf>
                    </SubItemList>
                </BatchInf>
                <BatchInf>
                    <BatchId>B201805240012</BatchId>
                    <BatchDCFlg>1</BatchDCFlg>
                    <BatchNetAmt>CNY1200.00</BatchNetAmt>
                    <SubItemList>
                        <SubItemInf>0116|C1010511003703|00|CNY1200.00|4|CNY0.00|0|</SubItemInf>
                    </SubItemList>
                </BatchInf>
            </BatchList>
        </SttlInf>
    </SttlList>
</Root>

可以看到嵌套了好几层:SttlList—>SttlInf—>BatchList—>BatchInf—>SubItemList—>SubItemInf

其中,要先把xml报文解析成obj对象,根据xml报文拆分后封装的obj对象,对于xml报文解析可查看另一篇:

Dom4j解析xml报文

核心类库是:

1、org.apache.commons.csv 类库中的CSVPrinter进行打印输出。

2、借助java.io的输入输出流等

  • HttpServletRequest
  • HttpServletResponse
  • OutputStream/OutputStreamWriter

3、org.apache.commons.io关闭CSVPrinter打印输出流。

     上代码:接口Controller控制入口类:

@RequestMapping(value = "/export")
    @Logable(businessTag = "accountInfoExport")
    @Exceptionable
    @Validatable
    @ApiOperation(value = "对账文件导出", notes = "对账文件导出", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountDate", value = "对账日期(yyyy-MM-dd)", required = true, dataType = "String", length = 100, paramType = "query"),
            @ApiImplicitParam(name = "download", value = "是否下载:true/false", required = true, dataType = "boolean", length = 100, paramType = "query"),
            @ApiImplicitParam(name = "fileName", value = "例:AccountInfo", required = true, dataType = "String", length = 100, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "格式:csv", required = true, dataType = "String", length = 100, paramType = "query")})
    public PageResult<SettInfoExportDto> exportAccountInfo(@RequestParam String accountDate,
                                                           @RequestParam(value = "download", required = true) boolean download,
                                                           @RequestParam(value = "fileName", required = true) String fileName,
                                                           @RequestParam(value = "type", required = true) String type,
                                                           HttpServletRequest request, HttpServletResponse response) {

        PageResult<SettInfoExportDto> pageResult = new PageResult<SettInfoExportDto>();
        AccountInfoEntityResp accountInfoEntityResp = accountInfoService.getLocalAccountInf(accountDate);
        //1.结算场次列表
        List<String> settListTitles = new ArrayList<>();
        settListTitles.add("报文标识号");
        settListTitles.add("场次借贷标识");
        settListTitles.add("场次金额");
        //2.批次列表
        List<String> batchInfListTitles = new ArrayList<>();
        batchInfListTitles.add("报文标识号");
        batchInfListTitles.add("批次号");
        batchInfListTitles.add("批次借贷标识");
        batchInfListTitles.add("批次金额");
        //3.分项列表
        List<String> sttlInfListTitles = new ArrayList<>();
        sttlInfListTitles.add("批次号");
        sttlInfListTitles.add("业务类型");
        sttlInfListTitles.add("银行金额机构标识");
        sttlInfListTitles.add("账户类型");
        sttlInfListTitles.add("分项借方发生额");
        sttlInfListTitles.add("分项借方发生笔数");
        sttlInfListTitles.add("分项贷方发生额");
        sttlInfListTitles.add("分项贷方发生笔数");

        //4.账务日期...
        Map<String, String> statisticsData = new HashMap<>();
        String settCount = accountInfoEntityResp.getSttlCntNb().toString();
        String debitAmount = accountInfoEntityResp.getDebitCntAmt().toString();
        String creditAmount = accountInfoEntityResp.getCreditCntAmt().toString();
        statisticsData.put("财务日期:", accountDate.replace("//-","///"));
        statisticsData.put("结算总笔数", settCount);
        statisticsData.put("借方金额", debitAmount);
        statisticsData.put("贷方金额", creditAmount);
        //5.结算场次列表数据list
        List<SettInfoExportDto> settInfoExportDtoList = new ArrayList<SettInfoExportDto>();
        //6.批次列表数据list
        List<BatchInfExportDto> batchInfExportDtoList = new ArrayList<BatchInfExportDto>();
        //7.分项列表数据list
        List<SubItemInfoExportDto> subItemInfoExportDtoList = new ArrayList<SubItemInfoExportDto>();
        if(accountInfoEntityResp.getSttlList() != null){
            //结算场次列表数据
            for(int i=0; i<accountInfoEntityResp.getSttlList().size(); i++){
                SettInfoExportDto settInfoExportDto = new SettInfoExportDto();
                settInfoExportDto.setSttlAmt(accountInfoEntityResp.getSttlList().get(i).getSttlAmt());
                settInfoExportDto.setSttlDCFlg(accountInfoServiceImpl.DCFlag(accountInfoEntityResp.getSttlList().get(i).getSttlDCFlg()));
                settInfoExportDto.setSttlReptFlg(accountInfoEntityResp.getSttlList().get(i).getSttlReptFlg());
                settInfoExportDtoList.add(settInfoExportDto);
                //批次列表数据
                if(accountInfoEntityResp.getSttlList().get(i).getBatchList() != null){
                    for(int j= 0; j<accountInfoEntityResp.getSttlList().get(i).getBatchList().size(); j++) {
                        BatchInfExportDto batchInfExportDto = new BatchInfExportDto();
                        batchInfExportDto.setSttlReptFlg(accountInfoEntityResp.getSttlList().get(i).getSttlReptFlg());
                        batchInfExportDto.setBatchDCFlg(accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getBatchDCFlg());
                        batchInfExportDto.setBatchId(accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getBatchId());
                        batchInfExportDto.setBatchNetAmt(accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getBatchNetAmt());
                        batchInfExportDtoList.add(batchInfExportDto);
                        //分项列表数据
                        if(accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getSubItemList() != null){
                            for(int f=0; f< accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getSubItemList().size(); f++){
                                SubItemInfoExportDto subItemInfoExportDto = new SubItemInfoExportDto();
                                subItemInfoExportDto.setBatchId(accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getBatchId());
                                String subStr = accountInfoEntityResp.getSttlList().get(i).getBatchList().get(j).getSubItemList().get(f).getSubItemInf();
                                subStr = subStr.replace("CNY","");
                                String[] subArray = {};
                                subArray = subStr.split("\\|");
                                subItemInfoExportDto.setBusinessType(accountInfoServiceImpl.busiType(subArray[0]));
                                subItemInfoExportDto.setBankOrgFlag(subArray[1]);
                                subItemInfoExportDto.setAccountType(accountInfoServiceImpl.accountType(subArray[2]));
                                subItemInfoExportDto.setDebitSplitAmount(subArray[3]);
                                subItemInfoExportDto.setDebitSplitCount(Integer.valueOf(subArray[4]));
                                subItemInfoExportDto.setCreditSplitAmount(subArray[5]);
                                subItemInfoExportDto.setCreditSplitCount(Integer.valueOf(subArray[6]));
                                subItemInfoExportDtoList.add(subItemInfoExportDto);
                            }
                        }
                    }
                }
            }
        }
        settListTitles.addAll(batchInfListTitles);
        settListTitles.addAll(sttlInfListTitles);
        CSVPrinter csvPrinter = accountInfoService.downloadCsv(request, response, settListTitles);
        pageResult.setTitles(settListTitles);
        pageResult.setResult(Constants.detailReturnCode.RETURN_SUCCESS.code);
        pageResult.setRows(settInfoExportDtoList);
        accountInfoService.printlnCsv(csvPrinter,statisticsData,settInfoExportDtoList,batchInfExportDtoList,
                subItemInfoExportDtoList,settListTitles,batchInfListTitles,sttlInfListTitles);
        IOUtils.closeQuietly(csvPrinter);
        return null;
    }

输入输出流,封装CSVPrinter对象:

public CSVPrinter downloadCsv(HttpServletRequest request, HttpServletResponse response, List<String> titles) {
        try {
            // 获取数据
            String fileName = request.getParameter("fileName");
            // 设置文件名, 用于下载
            response.setContentType("application/csv;charset=UTF-8");
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf-8"));
            // 添加UTF-8的BOM头,避免Excel打开文件时使用系统默认编码产生乱码
            byte[] byteBOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(byteBOM);
            OutputStreamWriter outputStreamWriter =  new OutputStreamWriter(outputStream, "utf-8");
            CSVPrinter printer = CSVFormat.DEFAULT.withHeader(titles.toArray(new String[titles.size()])).print(outputStreamWriter);
            return printer;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

csv导出记录:

public void printlnCsv(CSVPrinter printer, Map<String, String> statisticsData,List<SettInfoExportDto> settInfoExportDtoList,
                           List<BatchInfExportDto> batchInfExportDtoList,List<SubItemInfoExportDto> subItemInfoExportDtoList,
                           List<String> settListTitles,List<String> batchInfListTitles,List<String> sttlInfListTitles){
        if(subItemInfoExportDtoList.size() > 0){
            try{
                //报文标识号
                String sttlReptFlg = null;
                //结算列表数据
                for(SettInfoExportDto row:settInfoExportDtoList){
                    Field[] declaredFields = row.getClass().getDeclaredFields();
                    for (Field declaredField : declaredFields) {
                        declaredField.setAccessible(true);
                        FieldAnnotation annotation = declaredField.getAnnotation(FieldAnnotation.class);
                        if (annotation == null) {
                            // 只有注解了 FieldAnnotation 的字段,才会输出
                            continue;
                        }
                        Object value = declaredField.get(row);
                        if (StringUtils.equals(annotation.fieldName(), "报文标识号")) {
                            sttlReptFlg = (String) value;
                        }
                        printer.print(value + "\t");
                    }
                    // 换行
                    printer.println();
                    //打印批次列表数据
                    printBacthRecord(printer,sttlReptFlg, batchInfExportDtoList, subItemInfoExportDtoList, settListTitles
                            ,batchInfListTitles, sttlInfListTitles);
                }
                // 换行
                printer.println();
                //手续费行
                if (statisticsData != null && !statisticsData.isEmpty()) {
                    Iterator<String> iterator = statisticsData.keySet().iterator();
                    while (iterator.hasNext()) {
                        String key = (String) iterator.next();
                        printer.print(key);
                        printer.print(statisticsData.get(key));
                        // 换行
                        printer.println();
                    }
                    byte[] nulls = new byte[] { (byte) 00, (byte) 00, (byte) 00 };
                    printer.print(new String(nulls));
                }
                printer.flush();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 打印批次列表数据
     */
    public void printBacthRecord(CSVPrinter printer,String sttlReptFlg,List<BatchInfExportDto> batchInfExportDtoList,
                                 List<SubItemInfoExportDto> subItemInfoExportDtoList,List<String> settListTitles ,
                                 List<String> batchInfListTitles,List<String> sttlInfListTitles) throws IOException {
        int i = settListTitles.size() - batchInfListTitles.size() - sttlInfListTitles.size();
        //取出该报文标识号的批次列表
        List<BatchInfExportDto> printBatchInfoList = new ArrayList<BatchInfExportDto>();
        if(batchInfExportDtoList != null && batchInfExportDtoList.size()>0){
            for(BatchInfExportDto record:batchInfExportDtoList){
                if(record.getSttlReptFlg().equals(sttlReptFlg)){
                    printBatchInfoList.add(record);
                }
            }
        }
        if (printBatchInfoList != null && !printBatchInfoList.isEmpty()) {
            String batchId = null;
            for (BatchInfExportDto bacthInfo : printBatchInfoList) {
                byte[] nulls = new byte[i];
                for (byte b : nulls) {
                    printer.print(new String(nulls));
                }
                printer.print(sttlReptFlg);
                printer.print(bacthInfo.getBatchId());
                printer.print(bacthInfo.getBatchDCFlg());
                printer.print(bacthInfo.getBatchNetAmt());
                batchId = bacthInfo.getBatchId();
                // 换行
                printer.println();
                //打印分项列表数据
                printSubItemRecord(printer,batchId,subItemInfoExportDtoList,settListTitles,sttlInfListTitles);
            }
        }
    }

    /**
     * 打印分项列表数据
     */
    public void printSubItemRecord(CSVPrinter printer,String batchId,List<SubItemInfoExportDto> subItemInfoExportDtoList,
                                   List<String> settListTitles ,List<String> sttlInfListTitles ) throws IOException {
        int i = settListTitles.size() - sttlInfListTitles.size();
        //取出该批次号的分项列表
        List<SubItemInfoExportDto> subItemPrintRecordList = new ArrayList<SubItemInfoExportDto>();
        if(subItemInfoExportDtoList != null && subItemInfoExportDtoList.size()>0){
            for(SubItemInfoExportDto record : subItemInfoExportDtoList){
                if(record.getBatchId().equals(batchId)){
                    subItemPrintRecordList.add(record);
                }
            }
        }
        if (subItemPrintRecordList != null && !subItemPrintRecordList.isEmpty()) {
            for (SubItemInfoExportDto subItemInfo : subItemPrintRecordList) {
                byte[] nulls = new byte[i];
                for (byte b : nulls) {
                    printer.print(new String(nulls));
                }
                printer.print(batchId);
                printer.print(subItemInfo.getBusinessType());
                printer.print(subItemInfo.getBankOrgFlag());
                printer.print(subItemInfo.getAccountType());
                printer.print(subItemInfo.getDebitSplitAmount());
                printer.print(subItemInfo.getDebitSplitCount());
                printer.print(subItemInfo.getCreditSplitAmount());
                printer.print(subItemInfo.getDebitSplitCount());
                // 换行
                printer.println();
            }
        }
    }

主要核心代码以上。

最终导出效果如下:

 

同名原创公众号: 程序大视界

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序大视界

原创不易,请给点支持和鼓励吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值