目录
1:引入maven
2:代码实现
3.导出通讯录信息到Excel文件
4.生成并下载Excel文件部分解释
1:引入maven
- 添加依赖:首先,在你的项目中添加EasyExcel库的依赖。你可以在项目的构建文件(如Maven的pom.xml)中添加以下依赖项:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.4.3</version>
</dependency>
2:代码实现
- 写入Excel文件:使用EasyExcel写入Excel文件非常简单。下面的示例演示如何将数据写入Excel文件:
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 com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
public class ExcelWriterExample {
public static void process(HttpServletResponse response,String fileName) throws IOException {
PageHelper.startPage("1", "10");
PageInfo<User> userListInfo =getUserList();
// 定义数据列表
List<User> userList = userListInfo.getList();
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置背景颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//设置头字体
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 13);
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);
//设置头居中
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置 水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
EasyExcel.write(response.getOutputStream(), User.class)
.registerWriteHandler(horizontalCellStyleStrategy )
.excelType(ExcelTypeEnum.XLS)
.autoCloseStream(Boolean.TRUE)
.sheet("报表")
.doWrite(userList);
}
@Data
private static class User {
private String name;
private int age;
}
// 示例方法,返回用户数据列表
private static List<User> getUserList() {
List<User> userList = new ArrayList<>();
userList.add(new User("John", 25));
userList.add(new User("Alice", 30));
userList.add(new User("Bob", 35));
return userList;
}
}
3.导出通讯录信息到Excel文件
PageHelper.startPage("1", "10");
PageInfo<User> userListInfo =getUserList();
// 定义数据列表
List<User> userList = userListInfo.getList();
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置背景颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//设置头字体
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 13);
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);
//设置头居中
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置 水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
:创建一个WriteCellStyle
对象,用于设置Excel表头样式。headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
:设置表头的背景颜色为白色。WriteFont headWriteFont = new WriteFont();
:创建一个WriteFont
对象,用于设置表头字体。headWriteFont.setFontHeightInPoints((short) 13);
:设置表头字体的大小为13号。headWriteFont.setBold(true);
:设置表头字体为粗体。headWriteCellStyle.setWriteFont(headWriteFont);
:将表头字体设置到表头样式中。headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
:设置表头内容居中对齐。WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
:创建一个WriteCellStyle
对象,用于设置Excel内容样式。contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
:设置内容的水平居中对齐。HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
:创建一个HorizontalCellStyleStrategy
对象,将表头样式和内容样式组合成一个策略对象。
4.生成并下载Excel文件部分解释
触发浏览器下载的关键,response.
要触发浏览器下载文件的关键是设置正确的响应头信息response和将文件内容写入响应流中response.getOutputStream()。在Java Servlet中,可以使用HttpServletResponse
对象来实现这一功能。
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename="
+ URLEncoder.encode(fileName, "utf-8"));
EasyExcel.write(response.getOutputStream(), User.class)
.registerWriteHandler(horizontalCellStyleStrategy )
.excelType(ExcelTypeEnum.XLS)
.autoCloseStream(Boolean.TRUE)
.sheet("报表")
.doWrite(userList);
response.setCharacterEncoding("UTF-8");
:设置响应的字符编码为UTF-8,确保支持中文字符。response.setContentType("application/vnd.ms-excel");
:设置响应的内容类型为Excel文件。response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
:设置响应头,指定文件名,并对文件名进行URL编码,确保中文文件名的正确性。EasyExcel.write(response.getOutputStream(), User.class)
:使用EasyExcel库,创建一个写入Excel的Builder对象,指定输出流和数据对象的类。.registerWriteHandler(horizontalCellStyleStrategy)
:注册写入处理器(WriteHandler),可以用于自定义Excel的样式、格式等。.excelType(ExcelTypeEnum.XLS)
:指定Excel文件的类型为XLS格式。.autoCloseStream(Boolean.TRUE)
:设置自动关闭输出流,确保资源的正确释放。.sheet("报表")
:设置Excel的工作表名称为"用户报表"。.doWrite(userList)
:执行写入操作,将数据对象(target)写入Excel文件。