• easyExcel根据ExcelDTO生成excel并导出自定义样式


    @Override
        public String exportExcelModel(HttpServletResponse response) {
            OutputStream outputStream = ExcelUtils.getResponseOutputStream(response, "导入模板");
            ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(outputStream).registerWriteHandler(new CustomCellWriteHandler()).excelType(ExcelTypeEnum.XLSX);
            ExcelWriter excelWriter = excelWriterBuilder.build();
    
            ExcelUtils.writeOnly(excelWriter, importAssignExcelDemo(), OneExcelDTO.class, 1, "模板数据");
            ExcelUtils.writeOnly(excelWriter, describeDemo(), TwoExcelDTO.class, 2, "详细说明");
            try {
                ExcelUtils.finishWriter(outputStream, excelWriter);
            } catch (Exception e) {
                System.out.println("导出异常!!!");
                e.printStackTrace();
            }
            return "导出指定模板结束";
        }
    /**
    * 生成excel文件指定sheet页
    *
    * @param excelWriter
    * @param data
    * @param clazz
    * @param sheetNo
    * @param sheetName
    * @param <T>
    */

    public static <T> void writeOnly(ExcelWriter excelWriter, List<T> data, Class clazz,Integer sheetNo,String sheetName){
            ExcelWriterSheetBuilder excelWriterSheetBuilder;
            WriteSheet writeSheet = new WriteSheet();
            excelWriterSheetBuilder = new ExcelWriterSheetBuilder(excelWriter);
            excelWriterSheetBuilder.sheetNo(sheetNo);
            excelWriterSheetBuilder.sheetName(sheetName);
            writeSheet.setSheetNo(sheetNo);
            writeSheet.setSheetName(sheetName);
            writeSheet.setClazz(clazz);
            excelWriter.write(data,writeSheet);
        }
    public static void finishWriter(OutputStream outputStream,ExcelWriter excelWriter) throws IOException { outputStream.flush(); excelWriter.finish(); outputStream.close(); System.out.println("结束!!!"); }

      

    自定义样式监听类:
    继承抽象策略

    public class CustomCellWriteHandler extends AbstractCellStyleStrategy {

    private WriteCellStyle headWriteCellStyle;
    private List<WriteCellStyle> contentWriteCellStyleList;
    private CellStyle headCellStyle;
    private List<CellStyle> contentCellStyleList;

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

    public CustomCellWriteHandler(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
    this.headWriteCellStyle = headWriteCellStyle;
    this.contentWriteCellStyleList = new ArrayList();
    this.contentWriteCellStyleList.add(contentWriteCellStyle);
    }
    @Override
    protected void initCellStyle(Workbook workbook) {
    if (this.headWriteCellStyle != null) {
    this.headCellStyle = StyleUtil.buildHeadCellStyle(workbook, this.headWriteCellStyle);
    }

    if (this.contentWriteCellStyleList != null && !this.contentWriteCellStyleList.isEmpty()) {
    this.contentCellStyleList = new ArrayList();
    Iterator var2 = this.contentWriteCellStyleList.iterator();

    while(var2.hasNext()) {
    WriteCellStyle writeCellStyle = (WriteCellStyle)var2.next();
    this.contentCellStyleList.add(StyleUtil.buildContentCellStyle(workbook, writeCellStyle));
    }
    }

    }
    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
    if (this.headCellStyle != null) {
    cell.setCellStyle(this.headCellStyle);
    }
    }
    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
    if (this.contentCellStyleList != null && !this.contentCellStyleList.isEmpty()) {

    // 这里可以对cell进行任何操作
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle headStyle = getContentStyle(workbook, cell);
    cell.setCellStyle(headStyle);

    // cell.setCellStyle((CellStyle)this.contentCellStyleList.get(relativeRowIndex % this.contentCellStyleList.size()));
    }
    }



    /*
    * 表内容样式
    */
    public static CellStyle getContentStyle(Workbook workbook, Cell cell) {
    // 设置字体
    Font font = workbook.createFont();
    //设置字体大小
    font.setFontHeightInPoints((short) 11);
    //字体加粗
    font.setBold(false);
    //斜体
    font.setItalic(true);
    //设置字体名字
    font.setFontName("宋体");
    //设置样式;
    CellStyle style = workbook.createCellStyle();
    if(cell.getRowIndex() == 1){
    //第一行示例数据 红色 宋体 斜体
    font.setColor(IndexedColors.RED.getIndex());//字体颜色=红色
    }
    //在样式用应用设置的字体;
    style.setFont(font);
    //设置自动换行;
    style.setWrapText(true);
    //设置水平对齐的样式为居中对齐;
    style.setAlignment(HorizontalAlignment.CENTER);
    //数据规范信息描述,加粗、靠左,通过内容指定内容格式
    if(cell.getColumnIndex()==0 && cell.getStringCellValue().contains("导入数据规范说明")){
    font.setBold(true);
    style.setAlignment(HorizontalAlignment.LEFT);
    }
    //设置垂直对齐的样式为居中对齐;
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    return style;
    }

      

    参考链接地址:

    https://blog.csdn.net/m0_46109609/article/details/109774365
    https://www.cnblogs.com/Hizy/p/11825886.html

  • 相关阅读:
    [转]大型网站架构设计的体系演变
    [转]木桶理论已死,长板理论告诉你:优势才是王道!
    UHF RFID编码之TPP编码
    Git使用笔记
    使用Open Live Writer写博客
    频谱分析仪
    相位噪声
    峰值因子,峰均比,Reference Level
    SeeSharpTools.JXI.DSP.Spectrum 使用
    dyld: Library not loaded: /usr/lib/libstdc++.6.dylib
  • 原文地址:https://www.cnblogs.com/mangwusuozhi/p/15649977.html
Copyright © 2020-2023  润新知