PS:不知道EasyPoi 的可以看快速上手文档
<dependency>
<groupId>cn.afterturngroupId>
<artifactId>easypoi-spring-boot-starterartifactId>
<version>4.1.3version>
dependency>
package ev;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* 文件名称: ev.EasyPoiTest.java
* 初始作者: AirOrangeWorkSpace
* 创建日期: 2020/7/15 9:22
* 功能说明: 测试EasyPoi
* =================================================
* 修改记录:
* 修改作者 日期 修改内容
* ================================================
* Copyright (c) 2020-2021 .All rights reserved.
*/
@Slf4j
public class EasyPoiColorTest {
@Data
static class ClientData implements Serializable {
private static final long serialVersionUID = 1L;
// 编号
private String code;
// 客户名
private String clientName;
// 联系电话
private String phone;
// 联系电话
private String color;
}
public static void main(String[] args) throws IOException{
String data = "[\n" +
" {\n" +
" \"code\":\"123\",\n" +
" \"clientName\":\"阿里是蓝色\",\n" +
" \"color\":\"blue\",\n" +
" \"phone\":\"400\"\n" +
" },\n" +
" {\n" +
" \"code\":\"321\",\n" +
" \"clientName\":\"腾讯是黄色\",\n" +
" \"color\":\"yellow\",\n" +
" \"phone\":\"410\"\n" +
" },\n" +
" {\n" +
" \"code\":\"213\",\n" +
" \"clientName\":\"百度是红色\",\n" +
" \"color\":\"red\",\n" +
" \"phone\":\"420\"\n" +
" }\n" +
"]";
// 获取业务数据
List<ClientData> clientData = JSON.parseArray(data, ClientData.class);
// 动态导出
downLoadExcel(clientData);
}
private static void downLoadExcel(List<ClientData> data) throws IOException {
TemplateExportParams params = new TemplateExportParams("D:/客户信息模板.xls");
Map<String, Object> map = Maps.newHashMap();
map.put("list", data);
Workbook book = ExcelExportUtil.exportExcel(params, map);
// 创建导出文件
FileOutputStream fo = new FileOutputStream("D:/客户信息.xls");
book.write(fo);
fo.close();
}
}
编号 | 客户名 | 联系电话 | ||
---|---|---|---|---|
{ {fe: list t.code | t.clientName | t.phone | t.color}} |
Excel条件表达式(不懂的可以去查一查Excel表达式如何书写)
=$D1="red" // D1行往下若行内值为red
=$D1="blue" // D1行往下若行内值为blue
=$D1="yellow" // D1行往下若行内值为yellow
=$A:$C // 使用范围为A列至C列
在springMVC中使用
@ResponseBody
@GetMapping(value = "/apis/exportExcel/down")
public void exportExcel(
HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
// 查询出的数据
String data = "[\n" +
" {\n" +
" \"code\":\"123\",\n" +
" \"clientName\":\"阿里是蓝色\",\n" +
" \"color\":\"blue\",\n" +
" \"phone\":\"400\"\n" +
" },\n" +
" {\n" +
" \"code\":\"321\",\n" +
" \"clientName\":\"腾讯是黄色\",\n" +
" \"color\":\"yellow\",\n" +
" \"phone\":\"410\"\n" +
" },\n" +
" {\n" +
" \"code\":\"213\",\n" +
" \"clientName\":\"百度是红色\",\n" +
" \"color\":\"red\",\n" +
" \"phone\":\"420\"\n" +
" }\n" +
"]";
// 获取业务数据
List<ClientData> list = JSON.parseArray(data, ClientData.class);
ClassPathResource classPathResource = new ClassPathResource("D:/客户信息模板.xls");
Map<String,Object> map = Maps.newHashMap();
map.put("list", list);
TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
modelMap.put(TemplateExcelConstants.FILE_NAME, "客户信息");
modelMap.put(TemplateExcelConstants.PARAMS, result);
modelMap.put(TemplateExcelConstants.MAP_DATA, map);
PoiBaseView.render(modelMap, request, response,
TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
}
esult);
modelMap.put(TemplateExcelConstants.MAP_DATA, map);
PoiBaseView.render(modelMap, request, response,
TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
}