其他分享
首页 > 其他分享> > easyexcel总结

easyexcel总结

作者:互联网

简介

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