SpringBoot导出到Excel


一、依赖

pom.xml


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>

二、相关bean

SpringBoot导出到Excel 
package com.exiport.demo.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import org.springframework.stereotype.Component;

@Component
public class Student {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("学号")//指定字段名字
// @ExcelProperty(index = 0)//指定导出列的顺序
// @ExcelIgnore//忽略该字段
private String id;

public Student(String name, String id) {
this.name = name;
this.id = id;
}

public Student() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
return "Studen{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
'}';
}
}

ExcelUtil
package com.exiport.demo.util;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.exiport.demo.domain.Student;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExcelUtil {
public static void writExcel(HttpServletResponse response, List list) throws IOException {
//将输出流封装到excelWriter
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//定义工作表对象
WriteSheet sheet = EasyExcel.writerSheet(0,"sheet").head(Student.class).build();
//往excel写入
excelWriter.write(list,sheet);
//关闭输出流
excelWriter.finish();
}
}

三、Controller(实现)

package com.exiport.demo.controller;

import com.exiport.demo.domain.Student;
import com.exiport.demo.util.ExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {
@GetMapping("/test")
@ResponseBody
public void test(HttpServletResponse response) throws IOException {
List<Student> list = new ArrayList<>();
list.add(new Student("刘华强","001"));
list.add(new Student("卖瓜老板","002"));
//返回输出流。格式Excel
response.setContentType("application/vnd.ms-excel");
//设置表名称
String name = "学生信息表";
try {
//设置编码格式
name = URLEncoder.encode(name, StandardCharsets.UTF_8.toString());
// 设置输出名字 attachment附件
response.setHeader("Content-Disposition","attachment;filename="+name+".xlsx");
ExcelUtil.writExcel(response,list);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

}

}

结果:

SpringBoot导出到Excel

 

 

                                                                                                                  SpringBoot导出到Excel

 

上一篇:AX2009导出EXCEL2016之后版本报错


下一篇:用java读取Excel并依据模板图生成对应的图片