EasyPoi导出Excel实现标记颜色

EasyPoi导出Excel实现标记颜色

PS:不知道EasyPoi 的可以看快速上手文档

       <dependency>
            <groupId>cn.afterturngroupId>
            <artifactId>easypoi-spring-boot-starterartifactId>
            <version>4.1.3version>
        dependency>
实现思路:通过设置模板的Excel丰富的样式(利用条件表达式)来将数据标记颜色

java 部分(只是测试Demo类,与springMVC 结合使用点击这里)

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

Excel 模板部分

编号 客户名 联系电话
{ {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列
先设置EasyPoi表达式

EasyPoi导出Excel实现标记颜色_第1张图片

选择条件格式,选择最后一项管理规则,然后新建立Excel表达式规则

EasyPoi导出Excel实现标记颜色_第2张图片

在此输入表达式,设置想要的格式,点击确定,依次设置好自己想要标记的数据

EasyPoi导出Excel实现标记颜色_第3张图片

设置完成三个表达式(再将D列隐藏)

EasyPoi导出Excel实现标记颜色_第4张图片

运行java代码,得出新文件效果

EasyPoi导出Excel实现标记颜色_第5张图片

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

你可能感兴趣的:(poi,java)