easy-poi横向纵向模板导出Excel
easy-poi横向纵向模板导出Excel
本文记录的是easy-poi使用模板,对Excel进行横向和纵向的动态导出。
模板语法:
- #fe: 横向遍历
- v_fe:横向值遍历
- fe: 纵向遍历
一、引入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
二、模板说明
导出的是公司的股票代码每天的一个涨跌幅。
纵向是公司、横向的是titles每天日期,都需动态循环。
导出的效果大概如下:
三、测试代码
主要的的就是测试的数据结构
#fe:
和v_fe:
是最先执行的,然后执行fe:
根据测试数据,先执行横向语法后,模板会变成下边这个样子:
剩余这就是正常的纵向模板的语法了
完整测试代码如下:
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
/**
* excel导出演示
*
* @author 傲寒
* @date 2024/05/14
*/
public class ExcelExportDemo {
public static void main(String[] args) throws IOException {
List<Map<String, Object>> titles = new ArrayList<>();
titles.add(createExcelTitle("2024-05-14"));
titles.add(createExcelTitle("2024-05-13"));
final Collection<Map<String, Object>> exportData = new ArrayList<>();
final Map<String, Object> itemMap1 = createItemMap("A公司", "000000", "A公司");
final Map<String, Object> dateData1 = new HashMap<>();
dateData1.put("riseFallRange", "1.0");
dateData1.put("trend", "上涨");
itemMap1.put("2024-05-14", dateData1);
final Map<String, Object> dateData2 = new HashMap<>();
dateData2.put("riseFallRange", "5.0");
dateData2.put("trend", "上涨");
itemMap1.put("2024-05-13", dateData2);
exportData.add(itemMap1);
final Map<String, Object> itemMap2 = createItemMap("B公司", "000002", "B公司");
final Map<String, Object> dateData3 = new HashMap<>();
dateData3.put("riseFallRange", "-1.0");
dateData3.put("trend", "下跌");
itemMap2.put("2024-05-14", dateData3);
final Map<String, Object> dateData4 = new HashMap<>();
dateData4.put("riseFallRange", "-5.0");
dateData4.put("trend", "下跌");
itemMap2.put("2024-05-13", dateData4);
exportData.add(itemMap2);
TemplateExportParams templateExportParams = new TemplateExportParams("C:\\Users\\17731\\Desktop\\公司股市情况统计.xlsx");
//开启横向遍历
templateExportParams.setColForEach(true);
Map<String, Object> map = new HashMap<>();
map.put("titles", titles);
map.put("data", exportData);
final Workbook sheets = ExcelExportUtil.exportExcel(templateExportParams, map);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
sheets.write(bos);
IoUtil.close(sheets);
Files.write(Paths.get("C:\\Users\\17731\\Desktop\\公司股市情况统计Export.xlsx"), bos.toByteArray());
}
/**
* 创建excel标题
* <p>
* dateStr: 2024-04-14
* riseFallRangeTitle: 涨跌幅
* trendTitle: 趋势
* t.2024-05-14.riseFallRange: 涨跌幅值
* t.2024-05-14.trend: 趋势值
*
* @param dateStr 日期str
* @return {@link Map}<{@link String}, {@link Object}>
*/
private static Map<String, Object> createExcelTitle(String dateStr) {
Map<String, Object> title = new HashMap<>();
title.put("dateStr", dateStr);
title.put("riseFallRangeTitle", "涨跌幅");
title.put("trendTitle", "趋势");
title.put("riseFallRangeVal", "t." + dateStr + ".riseFallRange");
title.put("trendVal", "t." + dateStr + ".trend");
return title;
}
private static Map<String, Object> createItemMap(String companyName, String stockCode, String stockAbbr) {
final Map<String, Object> item = new HashMap<>();
item.put("companyName", companyName);
item.put("stockCode", stockCode);
item.put("stockAbbr", stockAbbr);
return item;
}
}
转载自:https://juejin.cn/post/7368413106677497871