首页

基于alibaba的EasyExcel处理工具类 - 简化excel导出处理

标签:excel生成     发布时间:2024-09-04   
package com.tools.common.excel;@b@@b@import com.alibaba.excel.EasyExcel;@b@import com.alibaba.excel.support.ExcelTypeEnum;@b@import com.alibaba.excel.write.metadata.style.WriteCellStyle;@b@import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;@b@import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;@b@import org.apache.poi.ss.usermodel.HorizontalAlignment;@b@@b@import javax.servlet.http.HttpServletResponse;@b@import java.io.IOException;@b@import java.io.OutputStream;@b@import java.net.URLEncoder;@b@import java.util.List;@b@@b@/**@b@ * Excel 工具类@b@ * @author 小柒2012@b@ */@b@public class ExcelUtil {@b@    /**@b@     * 导出 Excel :一个 sheet,带表头.@b@     * @param response  HttpServletResponse@b@     * @param data      数据 list,每个元素为一个 BaseRowModel@b@     * @param fileName  导出的文件名@b@     * @param sheetName 导入文件的 sheet 名@b@     * @param model     映射实体类,Excel 模型@b@     * @throws Exception 异常@b@     */@b@    public static void writeExcel(HttpServletResponse response, List<? extends Object> data,@b@                                  String fileName, String sheetName, Class model) throws Exception {@b@@b@        /**@b@         * 头的策略 设置表头居中对齐@b@         */@b@        WriteCellStyle headWriteCellStyle = new WriteCellStyle();@b@        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);@b@        /**@b@         * 内容的策略 设置内容靠左对齐@b@         */@b@        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();@b@        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);@b@        HorizontalCellStyleStrategy horizontalCellStyleStrategy =@b@                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);@b@        EasyExcel.write(getOutputStream(fileName, response), model)@b@                .excelType(ExcelTypeEnum.XLSX)@b@                .sheet(sheetName)@b@                .registerWriteHandler(horizontalCellStyleStrategy)@b@                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())@b@                .doWrite(data);@b@@b@    }@b@@b@    /**@b@     * 导出文件时为Writer生成OutputStream.@b@     * @param fileName 文件名@b@     * @param response response@b@     * @return ""@b@     */@b@    private static OutputStream getOutputStream(String fileName,@b@                                                HttpServletResponse response) throws Exception {@b@        try {@b@            fileName = URLEncoder.encode(fileName, "UTF-8");@b@            response.setContentType("application/vnd.ms-excel");@b@            response.setCharacterEncoding("utf8");@b@            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");@b@            response.setHeader("Pragma", "public");@b@            response.setHeader("Cache-Control", "no-store");@b@            response.addHeader("Cache-Control", "max-age=0");@b@            return response.getOutputStream();@b@        } catch (IOException e) {@b@            throw new Exception("导出excel表格失败!", e);@b@        }@b@    }@b@}


  • <<相关内容>>
<<热门下载>>