ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

easyexcel总结

2022-07-25 21:01:44  阅读:209  来源: 互联网

标签:总结 20 String easyexcel ExcelProperty private 标题 字符串


简介

github地址:https://github.com/alibaba/easyexcel

使用文档:https://www.yuque.com/easyexcel/doc/easyexcel

 

支持的功能

读excel

写excel:

分多个sheet写入,指定单元格样式,格式化显示等

实战

引入依赖

● 2+ 版本支持 Java7和Java6

● 3+ 版本至少 Java8

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>easyexcel</artifactId>

            <version>3.0.5</version>

        </dependency>

 

写Excel

实体类

@Data

public class DemoData {

    @ExcelProperty("字符串标题")

    private String string;

    @ExcelProperty("日期标题")

    private Date date;

    @ExcelProperty("数字标题")

    private Double doubleData;

    /**

     * 忽略这个字段

     */

    @ExcelIgnore

    private String ignore;

}

   

 

 

@Data

public class IndexData {

    @ExcelProperty(value = "字符串标题", index = 0)

    private String string;

    @ExcelProperty(value = "日期标题", index = 1)

    private Date date;

    /**

     * 这里设置3 会导致第二列空的

     */

    @ExcelProperty(value = "数字标题", index = 3)

    private Double doubleData;

}

          

 

 

@Data

public class ConverterData {

    /**

     * 我想所有的 字符串起前面加上"自定义:"三个字

     */

    @ExcelProperty(value = "字符串标题", converter = CustomStringStringConverter.class)

    private String string;

    /**

     * 我想写到excel 用年月日的格式

     */

    @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")

    @ExcelProperty("日期标题")

    private Date date;

    /**

     * 我想写到excel 用百分比表示

     */

    @NumberFormat("#.##%")

    @ExcelProperty(value = "数字标题")

    private Double doubleData;

}

     

 

 

@ContentRowHeight(10)

@HeadRowHeight(20)

@ColumnWidth(25)

public class WidthAndHeightData {

    @ExcelProperty("字符串标题")

    private String string;

    @ExcelProperty("日期标题")

    private Date date;

    /**

     * 宽度为50

     */

    @ColumnWidth(50)

    @ExcelProperty("数字标题")

    private Double doubleData;

}

 

// 头背景设置成红色 IndexedColors.RED.getIndex()

@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10)

// 头字体设置成20

@HeadFontStyle(fontHeightInPoints = 20)

// 内容的背景设置成绿色 IndexedColors.GREEN.getIndex()

@ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17)

// 内容字体设置成20

@ContentFontStyle(fontHeightInPoints = 20)

public class DemoStyleData {

    // 字符串的头背景设置成粉红 IndexedColors.PINK.getIndex()

    @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 14)

    // 字符串的头字体设置成20

    @HeadFontStyle(fontHeightInPoints = 30)

    // 字符串的内容的背景设置成天蓝 IndexedColors.SKY_BLUE.getIndex()

    @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)

    // 字符串的内容字体设置成20

    @ContentFontStyle(fontHeightInPoints = 30)

    @ExcelProperty("字符串标题")

    private String string;

    @ExcelProperty("日期标题")

    private Date date;

    @ExcelProperty("数字标题")

    private Double doubleData;

}/**

 * 样式的数据类

 *

 * @author Jiaju Zhuang

 **/

@Data

// 头背景设置成红色 IndexedColors.RED.getIndex()

@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10)

// 头字体设置成20

@HeadFontStyle(fontHeightInPoints = 20)

// 内容的背景设置成绿色 IndexedColors.GREEN.getIndex()

@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17)

// 内容字体设置成20

@ContentFontStyle(fontHeightInPoints = 20)

public class DemoStyleData {

    // 字符串的头背景设置成粉红 IndexedColors.PINK.getIndex()

    @HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 14)

    // 字符串的头字体设置成20

    @HeadFontStyle(fontHeightInPoints = 30)

    // 字符串的内容的背景设置成天蓝 IndexedColors.SKY_BLUE.getIndex()

    @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40)

    // 字符串的内容字体设置成20

    @ContentFontStyle(fontHeightInPoints = 30)

    @ExcelProperty("字符串标题")

    private String string;

    @ExcelProperty("日期标题")

    private Date date;

    @ExcelProperty("数字标题")

    private Double doubleData;

}

 

 

示例:

@PostMapping(vale = "/export_year")

    public void exportYear(HttpServletResponse response,

                            @Valid @RequestBody StatementExportAO ao){

List<StatementYearExcel> excelList = new ArrayList<>();

 StatementYearExcel excel = new StatementYearExcel();

 ……填充数据

 excelList.add(excel);

           

String file = "year_" + DateTimeUtils.now2String("yyyyMMddHHmmss") + ".xlsx";

String fileName = new String(file.getBytes(), StandardCharsets.ISO_8859_1);

response.setContentType("application/vnd.ms-excel;charset=utf-8");

response.setCharacterEncoding("utf-8");

response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

   try {

     ServletOutputStream out = response.getOutputStream();

     ExcelWriter excelWriter = EasyExcel.write(out).build();

     WriteSheet sheet = EasyExcel.writerSheet(0, "年结算单")

         .head(StatementYearExcel.class).build();

      excelWriter.write(excelList, sheet);

      excelWriter.finish();

    } catch (Exception e) {

         log.error("exportYear export error.", e);

   }

}

标签:总结,20,String,easyexcel,ExcelProperty,private,标题,字符串
来源: https://www.cnblogs.com/xgss/p/16518817.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有