导出excel方式2


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
   EasyExcelUtil.exportAsXLSX(exportList, LabelImportChk.class, "机构名单校验结果", req, resp, new CellColorSheetWriteHandler());



package com.gf.ecrm.accountnew.model;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Date;

@Getter
@Setter
@ToString
public class LabelImportChk {
/**
* 校验错误字段:中文分号隔开
*/
@ExcelProperty(value = "校验结果(导入时需删除此列,否则无法识别导入信息)")
private String chkFields;
@ExcelProperty(value = "客户全称")
private String accName;//客户全称
@ExcelIgnore
private String labelImportId;
}
public static <T> void exportAsXLSX(List<T> excelRows, Class<T> head, String fileName, HttpServletRequest req,HttpServletResponse resp, WriteHandler writeHandler) {
        try (final OutputStream outputStream = resp.getOutputStream()) {
            String userAgent = req.getHeader("User-Agent").toLowerCase();
            if (userAgent.contains("msie") || userAgent.contains("trident")) {
                setHeaders(resp, fileName, ExcelTypeEnum.XLSX);
            } else {
                // 非IE内核浏览器的处理:
                setHeadersNoIe(resp, fileName, ExcelTypeEnum.XLSX);
            }
            EasyExcel.write(outputStream, head)
                    .registerWriteHandler(defaultCellStyle())
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(20))
                    .registerWriteHandler(writeHandler)
                    .sheet()
                    .doWrite(excelRows);
        } catch (IOException e) {
            throw new GException("导出失败,请重试");
        }
    }
 public static void setHeaders(HttpServletResponse resp, String fileName, ExcelTypeEnum excelType)
            throws UnsupportedEncodingException {
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("UTF-8");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        resp.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName + excelType.getValue());
    }


public static void setHeadersNoIe(HttpServletResponse resp, String fileName, ExcelTypeEnum excelType)
throws UnsupportedEncodingException {
resp.setContentType("application/vnd.ms-excel");
resp.setCharacterEncoding("UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
resp.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName + excelType.getValue());
}
 

 

   public static HorizontalCellStyleStrategy defaultCellStyle() {
        //表头样式
        WriteCellStyle headWriteCellStyle = defaultHeadCellStyle();
        //内容样式
        WriteCellStyle contentWriteCellStyle = defaultContentCellStyle();
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


    public static WriteCellStyle defaultHeadCellStyle() {
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.GREY_50_PERCENT.getIndex());
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        headWriteCellStyle.setBorderRight(BorderStyle.THIN);
        headWriteCellStyle.setBorderTop(BorderStyle.THIN);
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteFont.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        headWriteCellStyle.setWriteFont(headWriteFont);
        return headWriteCellStyle;
    }



    public static WriteCellStyle defaultContentCellStyle() {
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setBold(false);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        return contentWriteCellStyle;
    }

  

public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy {

    private WriteCellStyle headWriteCellStyle;
    private List<WriteCellStyle> contentWriteCellStyleList;

    private CellStyle headCellStyle;
    private List<CellStyle> contentCellStyleList;

    public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle,
        List<WriteCellStyle> contentWriteCellStyleList) {
        this.headWriteCellStyle = headWriteCellStyle;
        this.contentWriteCellStyleList = contentWriteCellStyleList;
    }

    public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
        this.headWriteCellStyle = headWriteCellStyle;
        contentWriteCellStyleList = new ArrayList<WriteCellStyle>();
        contentWriteCellStyleList.add(contentWriteCellStyle);
    }

    @Override
    protected void initCellStyle(Workbook workbook) {
        if (headWriteCellStyle != null) {
            headCellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
        }
        if (contentWriteCellStyleList != null && !contentWriteCellStyleList.isEmpty()) {
            contentCellStyleList = new ArrayList<CellStyle>();
            for (WriteCellStyle writeCellStyle : contentWriteCellStyleList) {
                contentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, writeCellStyle));
            }
        }
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if (headCellStyle == null) {
            return;
        }
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if (contentCellStyleList == null || contentCellStyleList.isEmpty()) {
            return;
        }
        cell.setCellStyle(contentCellStyleList.get(relativeRowIndex % contentCellStyleList.size()));
    }

}
public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
    private Integer columnWidth;

    /**
     *
     * @param columnWidth
     */
    public SimpleColumnWidthStyleStrategy(Integer columnWidth) {
        this.columnWidth = columnWidth;
    }

    @Override
    protected Integer columnWidth(Head head, Integer columnIndex) {
        return columnWidth;
    }
}

  

posted @ 2022-06-14 19:22  老王的日常  阅读(88)  评论(0)    收藏  举报