依赖
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>3.1.0version>
dependency>
工具类
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
public class EsayExcelUtil {
public static Boolean writeExcel(List list, Class clazz, HttpServletResponse response, String fileName, String sheetName) throws IOException {
String file = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + file);
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = response.getOutputStream();
HorizontalCellStyleStrategy cellStyleStrategy = createStyle();
try {
EasyExcel.write(outputStream, clazz)
.registerWriteHandler(cellStyleStrategy)
.sheet(sheetName)
.doWrite(list);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
outputStream.flush();
outputStream.close();
}
}
public static Boolean writeExcel(List list, Class clazz, HttpServletResponse response, String fileName) throws Exception {
String file = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + file);
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = response.getOutputStream();
try {
EasyExcel.write(outputStream, clazz).sheet().doWrite(list);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
outputStream.flush();
outputStream.close();
}
}
public static Boolean writeExcel(List list, Class clazz, HttpServletResponse response) throws Exception {
String fileName = System.currentTimeMillis() + "";
return writeExcel(list, clazz, response, fileName);
}
public static Boolean writeExcel(List list, List<List<String>> head, HttpServletResponse response, String fileName) throws IOException {
String file = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + file);
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = response.getOutputStream();
try {
EasyExcel.write(outputStream)
.head(head)
.sheet()
.doWrite(list);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
outputStream.flush();
outputStream.close();
}
}
private static HorizontalCellStyleStrategy createStyle(){
WriteCellStyle titleStyle = new WriteCellStyle();
WriteCellStyle dataStyle = new WriteCellStyle();
titleStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setWrapped(false);
titleStyle.setFillForegroundColor(IndexedColors.WHITE.index);
WriteFont font = new WriteFont();
font.setFontName("楷体");
font.setFontHeightInPoints((short) 15);
font.setColor(IndexedColors.RED.index);
font.setBold(true);
font.setItalic(false);
font.setStrikeout(false);
titleStyle.setWriteFont(font);
return new HorizontalCellStyleStrategy(titleStyle, dataStyle);
}
private static void setBorderStyle(WriteCellStyle style){
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLUE.index);
style.setBottomBorderColor(IndexedColors.BLUE.index);
style.setLeftBorderColor(IndexedColors.BLUE.index);
style.setRightBorderColor(IndexedColors.BLUE.index);
}
}
测试一下
第一步:创建导出的对象
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(15)
@ContentRowHeight(20)
@ColumnWidth(10)
public class Student {
@ExcelProperty(value = "姓名", index = 0)
private String name;
@ExcelProperty(value = "年龄", index = 2)
private Integer age;
@ExcelProperty(value = "地址", index = 4)
private String address;
}
第二步:写测试接口
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/excel")
@Controller
public class ExcelController {
@GetMapping("/test")
public void test(HttpServletResponse response) throws Exception {
List<Student> data = getData();
EsayExcelUtil.writeExcel(data, Student.class, response);
}
private List<Student> getData(){
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 23, "北京"));
list.add(new Student("李四", 24, "上海"));
list.add(new Student("王五", 25, "杭州"));
return list;
}
第三步:启动项目,访问接口
如图:
测试动态表头
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@RequestMapping("/excel")
@Controller
public class ExcelController {
@GetMapping("/test")
public void test(HttpServletResponse response) throws Exception {
String fileName = "测试Excel导出";
List<Student> data = getData();
List<List<String>> head = getHead();
EsayExcelUtil.writeExcel(data, head, response, fileName);
}
public static List<List<String>> getHead(){
List<List<String>> list = new ArrayList<>();
List<String> head1 = new ArrayList<>();
head1.add("我是姓名");
List<String> head2 = new ArrayList<>();
head2.add("我是年龄");
List<String> head3 = new ArrayList<>();
head3.add("我是地址");
list.add(head1);
list.add(head2);
list.add(head3);
return list;
}
private List<Student> getData(){
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 23, "北京"));
list.add(new Student("李四", 24, "上海"));
list.add(new Student("王五", 25, "杭州"));
return list;
}
结果如图
就这样,再会!