easyexcel指定列导出的实现

Springboot继承easyexcel实现指定列导出功能:

前端传递需要导出的字段值,后端根据前端的传值导出对应的字段的值到Excel

-----核心代码-----

所需依赖:

		
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.6version>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.3version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plusartifactId>
            <version>3.3.0version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>easyexcelartifactId>
            <version>2.1.6version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
       
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>2.0.6version>
        dependency>

controller:

	@GetMapping("/exportExcel")
    public void exportExcel(Demo demo) {
        try {
            demoService.exportExcel(demo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

domain

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

@Data
public class Demo  {
    @ExcelIgnore
    private int id;

    @ExcelProperty("字段0")
    private String o;

    @ExcelProperty("字段1")
    private String yi;

    @ExcelProperty("字段2")
    private String er;
}

service:

	void exportExcel(Demo demo) throws JsonProcessingException;

impl:

		public void exportExcel(Demo demo) throws JsonProcessingException {
		List<Demo> demos = demoMapper.demoList();

		ObjectMapper mapper = new ObjectMapper();
		//JAVA类转化为String类型键值对
		String json = mapper.writeValueAsString(demo);
		//String转化为json
		JSONObject jsonObject= JSONObject.parseObject(json);
		//获得json所有键
		Set<String> keys = jsonObject.keySet();
		//遍历获取所有值(用户传入的字段),并存入集合
		Set<String> column = new HashSet<String>();
		for (String key : keys) {
			if (jsonObject.get(key) != null){
				column.add(jsonObject.get(key).toString());
			}
		}
		//这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
		EasyExcel.write("D:\\excel"+System.currentTimeMillis()+".xlsx", Demo.class)
				.includeColumnFiledNames(column).sheet("模板")
				.doWrite(demos);
	}

mapper:

	List<Demo> demoList();

mapper.xml:

	<select id="demoList" resultType="com.excel.domain.Demo">
        select * from sys_demo
    </select>

-----测试-----

此时传入的值中,yi,er的值为对应的字段名
easyexcel指定列导出的实现_第1张图片
点击导出,导出成功
easyexcel指定列导出的实现_第2张图片
去导出的路径查看文件
easyexcel指定列导出的实现_第3张图片
打开即可看到,是我们想要的样子
easyexcel指定列导出的实现_第4张图片

你可能感兴趣的:(Java,java,mybatis,spring,boot)