当前位置: 首页 > news >正文

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)
}

请求的代码如下

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,})

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);}}

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;@Overridepublic 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();}

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

相关文章:

  • html2canvas 将DOM节点转成图片
  • sql-labs第46关(order by盲注脚本)
  • golang 函数式编程库samber/mo使用: IO
  • C++利用汇编挖掘编程语言的本质..
  • C++:String类的使用
  • Attention 中的 Q, K, V
  • k8s中容器的调度与创建:CRI,cgroup
  • React富文本编辑器开发(五)
  • Day08:基础入门-算法分析传输加密数据格式密文存储代码混淆逆向保护
  • C++笔记(六)--- 静态成员变量/函数(static)
  • Python实现链表:从基础到应用
  • 力扣:120. 三角形最小路径和
  • ROS 2基础概念#1:计算图(Compute Graph)| ROS 2学习笔记
  • 智能生活:嵌入式技术改变我们的日常体验
  • iOS-设置指定边圆角(左上、左下等)
  • fetch 从初识到应用
  • Github访问慢解决办法
  • JavaScript创建对象的四种方式
  • MySQL几个简单SQL的优化
  • Nacos系列:Nacos的Java SDK使用
  • Object.assign方法不能实现深复制
  • PaddlePaddle-GitHub的正确打开姿势
  • React-flux杂记
  • Swift 中的尾递归和蹦床
  • Vim Clutch | 面向脚踏板编程……
  • vue-cli在webpack的配置文件探究
  • 盘点那些不知名却常用的 Git 操作
  • 前端性能优化——回流与重绘
  • 世界上最简单的无等待算法(getAndIncrement)
  • 我建了一个叫Hello World的项目
  • 小李飞刀:SQL题目刷起来!
  • 【干货分享】dos命令大全
  • Prometheus VS InfluxDB
  • #QT(智能家居界面-界面切换)
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • $.ajax,axios,fetch三种ajax请求的区别
  • (3)Dubbo启动时qos-server can not bind localhost22222错误解决
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (C语言)二分查找 超详细
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (pytorch进阶之路)扩散概率模型
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (转载)从 Java 代码到 Java 堆
  • .“空心村”成因分析及解决对策122344
  • .net Stream篇(六)
  • .NET 发展历程
  • .NET 中使用 TaskCompletionSource 作为线程同步互斥或异步操作的事件
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NET轻量级ORM组件Dapper葵花宝典
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • .pyc文件是什么?
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码
  • [AIGC] Spring Interceptor 拦截器详解