• excel导出标准化


    虽然标题叫标准化,只不过是我自己的习惯,当一件事情变得流程标准化之后,开发程序就会飞快,开发评估工作总是 搞个1~2天,实则前端后端一起开发,1个小时就可以搞定。
    1 前端

    const exportXls = async () => {
        var now = moment(new Date()).format('YYYYMMDDHHMMSS')
    	let name = '商品收发明细表.xls'
        const res = await proxy.$api.invOrder.goodsRdDetail.export({...condForm.value})
        let data = res.data;
    	let url = window.URL.createObjectURL(new Blob([data], ))
    	let link = document.createElement('a')
    	link.style.display = 'none'
    	link.href = url;
    	console.log(link);
    	link.setAttribute('download', now + name)
    	document.body.appendChild(link)
    	link.click()
    	document.body.removeChild(link)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    请求的代码如下

    public postOnlyFile = (url: string, data = {} , config: AxiosRequestConfig<any> = {}): Promise<any> =>
    	axios({
    		...this.baseConfig,
    		headers:{
    			...this.baseConfig.headers,
    			'Content-Type': "application/json"
    		},
    		responseType:'blob',
    		url,
    		method: 'post',
    		data,
    		...config,
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2 后端
    controller层基本就是复制粘贴,传参数给到service层而已。

        @PostMapping("export")
        @ApiOperation("导出商品收发明细表")
        public void export(@RequestBody PsiInvOrderReportCondDto condDto, HttpServletResponse response){
            //设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            //设置防止文件名中文乱码
            try {
                //设置防止文件名中文乱码
                String fileName = URLEncoder.encode("商品收发明细表", "utf-8");
                response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
                //
                if (!CheckEmptyUtil.isEmpty(condDto.getBillDateRange())){
                    condDto.setStartBillDate(condDto.getBillDateRange().get(0));
                    condDto.setEndBillDate(condDto.getBillDateRange().get(1));
                }
                goodsRdDetailService.export(response.getOutputStream(),"xls/GoodsRdDetail.xls", condDto);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    service层主要通过easyexcel填充数据

    import com.alibaba.excel.EasyExcel;
    import com.alibaba.excel.ExcelWriter;
    import com.alibaba.excel.write.metadata.WriteSheet;
    import com.alibaba.excel.write.metadata.fill.FillConfig;
        @Override
        public void export(OutputStream outputStream, String pathName, PsiInvOrderReportCondDto condDto) {
            List<GoodsRdDetailListDto> goodsRdDetailListDtos = select(condDto);
            int line = 1;
            for (GoodsRdDetailListDto goodsRdDetailListDto:goodsRdDetailListDtos){
                goodsRdDetailListDto.setLine(String.valueOf(line++));
                goodsRdDetailListDto.setBillDateStr(DateUtil.formatDate(goodsRdDetailListDto.getBillDate()));
                BusinessTypeEnum businessTypeEnum = BusinessTypeEnum.getInvBusinessTypeEnum(goodsRdDetailListDto.getBusiType());
                goodsRdDetailListDto.setBusiType(businessTypeEnum.display());
            }
            org.springframework.core.io.Resource resource = new ClassPathResource(pathName);
            InputStream inputStream = null;
            String fileName = DateUtil.getDateRandom() + ".xls";
            File file = new File(TmpDic.url + File.separator + fileName);
            try {
                inputStream = resource.getInputStream();
                FileUtils.copyInputStreamToFile(inputStream, file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(file).build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            //
            WriteSheet sheet0 = EasyExcel.writerSheet(0,"单据明细(不合并表头)")
                    //单元格
    //                .registerWriteHandler(new CustomCellWriteHandler())
                    .build();
            excelWriter.fill(goodsRdDetailListDtos, fillConfig, sheet0);
            //
            PsiAccountSet psiAccountSet = psiAccountSetService.getById(condDto.getAsId());
            GoodsRdDetailExcelHeaderDto excelHeaderDto = new GoodsRdDetailExcelHeaderDto();
            excelHeaderDto.setCompanyName(psiAccountSet.getName());
            excelHeaderDto.setStartBillDate(DateUtil.formatDate(condDto.getStartBillDate()));
            excelHeaderDto.setEndBillDate(DateUtil.formatDate(condDto.getEndBillDate()));
            excelWriter.fill(excelHeaderDto,sheet0);
            //
            excelWriter.finish();
            file.delete();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3 excel模板
    定义excel模板,就是上面的xls/GoodsRdDetail.xls
    1

  • 相关阅读:
    【无标题】
    C#中+=的用法
    第14章 - 垃圾回收概述
    电脑案件冲突问题
    echarts快速实现迁徙地图
    UE必学系列(基础篇完结)
    贪心算法----几个基本例题
    分布式医疗大数据存储方案研究综述
    外包干了2个月,技术退步明显...
    Node-怎么连接MySQL
  • 原文地址:https://blog.csdn.net/warrah/article/details/136362407