一、使用exsyExcel实现
includeColumnFieldNames 方法
导出只包含参数中传入的字段
/**
* 将列表以 Excel 响应给前端(支持勾选列导出)
*
* @param response 响应
* @param filename 文件名
* @param sheetName Excel sheet 名
* @param head Excel head 头
* @param data 数据列表哦
* @param title 需要导出的列
* @param <T> 泛型,保证 head 和 data 类型的一致性
* @throws IOException 写入失败的情况
*/
public static <T> void write(HttpServletResponse response, String filename, String sheetName,
Class<T> head, List<T> data,List<String> title) throws IOException {
// 输出 Excel
EasyExcel.write(response.getOutputStream(), head)
.includeColumnFieldNames(title) //只要导出对象中字段的数据
.autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) // 基于 column 长度,自动适配。最大 255 宽度


3255

被折叠的 条评论
为什么被折叠?



