1.代码实现
写入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;
}
}
2.导出通讯录信息到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);
1.WriteCellStyle headWriteCellStyle = new WriteCellStyle();:创建一个WriteCellStyle对象,用于设置Excel表头样式。
2.headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());:设置表头的背景颜色为白色。
3.WriteFont headWriteFont = new WriteFont();:创建一个WriteFont对象,用于设置表头字体。
4.headWriteFont.setFontHeightInPoints((short) 13);:设置表头字体的大小为13号。
5.headWriteFont.setBold(true);:设置表头字体为粗体。
6.headWriteCellStyle.setWriteFont(headWriteFont);:将表头字体设置到表头样式中。
7.headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);:设置表头内容居中对齐。
8.WriteCellStyle contentWriteCellStyle = new WriteCellStyle();:创建一个WriteCellStyle对象,用于设置Excel内容样式。
9.contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);:设置内容的水平居中对齐。
10.HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);:创建一个HorizontalCellStyleStrategy对象,将表头样式和内容样式组合成一个策略对象。
3.生成并下载Excel文件部分解释
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文件。