为什么要使用原生的POI
项目中需要用到导出Excel的功能,一开始用的是阿里的easyExcel,据说是性能比较好,原本本地测试没啥问题,一到了线上的环境后,就出现了几个报错,百度了一下大概是说缺少了某种字体啥的,可参考的解决方案不多,再说我不能保证到时现场的技术人员懂得装这啥啥字体,最后还是自己封装一下原生的POI,争取使用起来跟easyExcel差不多的简单快捷,对于小项目来说是够用的。
快速使用
1.在pom.xml中导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
2.新建一个ExcelUtil工具类,复制以下的代码
package com.rui.hellospringboot.Utils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
public class ExportExcelUtil {
/**
* 导出excel表
* @param response
* @param name
* @throws IOException
*/
public static <T>void exportExel(HttpServletResponse response,List<T> list,String[] title, String name) {
try {
HSSFWorkbook wb = generateExelFile(list, title,name);
//到这里,excel就已经生成了,然后就需要通过流来写出去
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//将excel写入流
wb.write(byteArrayOutputStream);
//设置文件标题
String outFile = name + ".xls";
//设置返回的文件类型
response.setContentType("application/vnd.ms-excel;charset=utf-8");
//对文件编码
outFile = response.encodeURL(new String(outFile.getBytes("gb2312"), "iso8859-1"));
//使用Servlet实现文件下载的时候,避免浏览器自动打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + outFile);
//设置文件大小
response.setContentLength(byteArrayOutputStream.size());
//创建Cookie并添加到response中
Cookie cookie = new Cookie("fileDownload", "true");
cookie.setPath("/");
response.addCookie(cookie);
//将流写进response输出流中
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 生成Excel文件
* @param list
* @return
*/
public static <T> HSSFWorkbook generateExelFile(List<T> list,String[] title,String sheetName) throws IllegalAccessException {
//生成exel文件
//创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet = wb.createSheet();
sheet.createFreezePane(0, 1, 0, 1);
wb.setSheetName(0, sheetName);
/*//默认宽高
sheet.setDefaultColumnWidth((short) 20);
sheet.setDefaultRowHeight((short) 2000);*/
//创建样式和字体
HSSFCellStyle curStyle = wb.createCellStyle();
HSSFFont curFont = wb.createFont();
Font font = wb.getFontAt((short) 0);
CellStyle style = wb.createCellStyle();
font.setCharSet(HSSFFont.DEFAULT_CHARSET);
//更改默认字体大小
font.setFontHeightInPoints((short) 12);
font.setFontName("宋体");
style.setFont(font);
//创建行列
HSSFRow nRow = sheet.createRow(0);
HSSFCell nCell = nRow.createCell(0);
//设置列的样式(具体实现在后面......)
nCell.setCellStyle(mainTitleStyle(curStyle, curFont));
//控制行号列号
int rowNo = 0;
//设置标题到第一行的列中
nRow = sheet.createRow(rowNo++);
for (int i = 0; i < title.length; i++) {
nCell = nRow.createCell(i);
nCell.setCellValue(title[i]);
nCell.setCellStyle(textStyle(curStyle, curFont));
}
//创建样式和字体
curStyle = wb.createCellStyle();
curFont = wb.createFont();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//封装exel 表体
for (int i = 0; i < list.size(); i++) {
T dto = list.get(i);
Class<?> aClass = dto.getClass();
nRow = sheet.createRow(rowNo++);
//HSSFCell nCell;
int colNo = 0;//控制列号
//第一列
Field[] fields = aClass.getDeclaredFields();
for (int j=0;j<title.length;j++){
Field field=fields[j];
field.setAccessible(true);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(field.get(dto).toString());
nCell.setCellStyle(textStyle(curStyle, curFont));
}
}
return wb;
}
/**
* 表格内容样式
*
* @param curStyle
* @param curFont
* @return
*/
public static HSSFCellStyle textStyle(HSSFCellStyle curStyle, HSSFFont curFont) {
curStyle.setAlignment(CellStyle.ALIGN_CENTER); //水平居中
curStyle.setWrapText(true); // 自动换行
curFont.setFontName("宋体");//字体
curFont.setFontHeightInPoints((short) 10);//字体大小
curStyle.setFont(curFont); // 绑定关系
return curStyle;
}
/**
* 表格标题样式
*
* @param curStyle
* @param curFont
* @return
*/
public static HSSFCellStyle mainTitleStyle(HSSFCellStyle curStyle, HSSFFont curFont) {
curStyle.setAlignment(CellStyle.ALIGN_GENERAL); //水平居中
curFont.setFontHeightInPoints((short) 16);//字体大小
curStyle.setFont(curFont); // 绑定关系
return curStyle;
}
}
3.工具类的使用
访问 /exportExcel 路径就能下载关于Person表的Excel了。
@GetMapping(value = "/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
Person p1 = new Person(01, "zhangsan", 20,"廣州", 5000);
Person p2 = new Person(02, "lisi", 20,"杭州", 8000);
Person p3 = new Person(03, "wangwu", 20,"上海", 15000);
List<Person> personList = Arrays.asList(p1, p2, p3);
String[] title={"序号","姓名","年龄","地址","工资"};
ExportExcelUtil.exportExel(response,personList,title,"用户统计表");
}
4.效果
注意事项:
如果上述的Person类中有些字段不需要导出到Excel的话,应该把不需要导出的字段放在最后面,然后标题titile里面只写需要导出的字段即可(一定要按顺序)。
针对这些问题,后续加上了注解方式,使用起来更加简便: