问题描述:
在使用 excel-converter-starter 时,注入ExExporter或者ExcelExporter导出数据时,有时导出的excel中,数据列的顺序不是按照配置文件中的顺序。例如有以下配置:
excel: converter: props-map: default: sheet-index: 1 name-row-index: 0 comment-row-index: 1 first-data-row-index: 2 last-data-row-index: -1 data-row-num: -1 start-column-index: 0 name-header-map: id: ID name: 名称 address: 地址 date: 日期 show-name-row: true
数据列分别为id,name,address,date,但是最终导出excel中的列的顺序乱了。
分析:
由于 name-header-map 是LinkedHashMap类型的,以保证最终导出是有序的,所以在推测应该是在读取配置文件时就已经乱序了。
再测试几次发现:使用application.properties配置文件就会使 nameHeaderMap 乱序,所以,应该是 spring boot 读取 .properties 和 .yml 配置文件的方式不同,导致读取进来的 LinkedHashMap 的顺序错乱。
解决方法:
1 采用 application.yml配置文件即可
2 如果项目原本就采用application.properties配置文件,可以新建一个application-excel.yml 文件,再在 application.properties配置文件 中导入即可,导入 application-excel.yml 配置文件只需要加入以下配置:
spring.profiles.include=excel