easyexcel 列宽、行高、样式


easyexcel 列宽、行高、样式

 

 

**********************

相关注解

 

HeadRowHeight:标注在类上

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HeadRowHeight {

    short value() default -1;
}

 

ContentRowHeight:标注在类上

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentRowHeight {

    short value() default -1;
}

 

ColumnWidth:标注在类、字段上,列宽

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ColumnWidth {

    int value() default -1;
}

 

HeadStyle:标注在类、字段上

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HeadStyle {

    short dataFormat() default -1;
    short rotation() default -1;
    short indent() default -1;


    boolean hidden() default false;
    boolean locked() default true;
    boolean quotePrefix() default false;
    boolean wrapped() default true;
    boolean shrinkToFit() default false;


    BorderStyle borderLeft() default BorderStyle.THIN;
    BorderStyle borderRight() default BorderStyle.THIN;
    BorderStyle borderTop() default BorderStyle.THIN;
    BorderStyle borderBottom() default BorderStyle.THIN;

    short leftBorderColor() default -1;
    short rightBorderColor() default -1;
    short topBorderColor() default -1;
    short bottomBorderColor() default -1;

    short fillBackgroundColor() default -1;
    short fillForegroundColor() default -1;


    HorizontalAlignment horizontalAlignment() default HorizontalAlignment.CENTER;
    VerticalAlignment verticalAlignment() default VerticalAlignment.CENTER;
    FillPatternType fillPatternType() default FillPatternType.SOLID_FOREGROUND;

}

 

HeadFontStyle:标注在类、字段上

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HeadFontStyle {

    byte underline() default -1;

    short color() default -1;
    short typeOffset() default -1;
    short fontHeightInPoints() default 14;

    int charset() default -1;

    String fontName() default "宋体";

    boolean italic() default false;
    boolean strikeout() default false;
    boolean bold() default true;
}

 

ContentStyle

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentStyle {

    short dataFormat() default -1;
    short rotation() default -1;
    short indent() default -1;
    short fillBackgroundColor() default -1;
    short fillForegroundColor() default -1;


    boolean hidden() default false;
    boolean locked() default false;
    boolean quotePrefix() default false;
    boolean wrapped() default false;
    boolean shrinkToFit() default false;


    BorderStyle borderLeft() default BorderStyle.NONE;
    BorderStyle borderRight() default BorderStyle.NONE;
    BorderStyle borderTop() default BorderStyle.NONE;
    BorderStyle borderBottom() default BorderStyle.NONE;

    short leftBorderColor() default -1;
    short rightBorderColor() default -1;
    short topBorderColor() default -1;
    short bottomBorderColor() default -1;

    FillPatternType fillPatternType() default FillPatternType.NO_FILL;
    VerticalAlignment verticalAlignment() default VerticalAlignment.CENTER;
    HorizontalAlignment horizontalAlignment() default HorizontalAlignment.GENERAL;

}

 

ContentFontStyle

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentFontStyle {

    byte underline() default -1;

    short color() default -1;
    short typeOffset() default -1;
    short fontHeightInPoints() default -1;

    int charset() default -1;

    String fontName() default "";

    boolean italic() default false;
    boolean strikeout() default false;
    boolean bold() default false;
}

 

 

**********************

示例

 

*****************

head 类

 

Fruit

@Data
@HeadRowHeight(40)
@ContentRowHeight(20)
public class Fruit {

    @ColumnWidth(10)
    private Integer id;

    @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND ,fillForegroundColor = 6)
    private String name;

    @HeadFontStyle(color = 20)
    private Double price;
}

 

*****************

测试类

 

Test

public class Test5 {

    private static final String file_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write_test9.xlsx";

    public static void main(String[] args){
        List<Fruit> list=new ArrayList<>();

        Fruit fruit=new Fruit();
        fruit.setId(1);
        fruit.setName("apple");
        fruit.setPrice(10d);

        list.add(fruit);

        EasyExcel.write(file_path,Fruit.class).sheet().doWrite(list);
    }
}

 

 

**********************

使用测试

 

                   

 

 

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
EasyExcel 支持导出合并行和列的表头的 Excel,可以通过设置 `@HeadRowHeight`、`@HeadColumnWidth`、`@ContentRowHeight` 和 `@ContentColumnWidth` 注解来控制表头和内容的行高列宽。同时,可以通过设置 `@ExcelProperty` 注解的 `colspan` 和 `rowspan` 属性来合并行和列。 以下是一个例子,导出一个合并了表头的 Excel: ```java @ExcelIgnoreUnannotated public class ExportData { @ExcelProperty(index = 0, value = "姓名", rowspan = 2, colspan = 2) @HeadRowHeight(30) @HeadColumnWidth(15) private String name; @ExcelProperty(index = 2, value = "性别", rowspan = 2) @HeadRowHeight(30) @HeadColumnWidth(15) private String gender; @ExcelProperty(index = 3, value = "联系方式", colspan = 2) @HeadRowHeight(30) @HeadColumnWidth(20) private String contact; @ExcelProperty(index = 4, value = "电话") @HeadRowHeight(30) @HeadColumnWidth(15) private String phone; @ExcelProperty(index = 5, value = "邮箱") @HeadRowHeight(30) @HeadColumnWidth(20) private String email; // 其他属性... // getter/setter 方法... } ``` 在这个例子中,我们使用 `@HeadRowHeight` 和 `@HeadColumnWidth` 注解设置表头的行高列宽,使用 `@ContentRowHeight` 和 `@ContentColumnWidth` 注解设置内容的行高列宽。同时,我们使用 `@ExcelProperty` 注解的 `colspan` 和 `rowspan` 属性来合并行和列。注意,需要将 `@ExcelIgnoreUnannotated` 注解添加到类上,以忽略未注解的属性。 然后,我们可以使用 EasyExcel 的 `ExcelWriter` 和 `Sheet` 类来写入数据。以下是一个示例代码: ```java public class ExcelExportUtil { public static void exportExcel(List<ExportData> dataList, String fileName, HttpServletResponse response) throws IOException { // 设置响应头 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); // 创建 ExcelWriter ServletOutputStream outputStream = response.getOutputStream(); ExcelWriter excelWriter = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX); // 创建 Sheet Sheet sheet = new Sheet(1, 0, ExportData.class); sheet.setTableStyle(createTableStyle()); // 写入数据 excelWriter.write(dataList, sheet); // 关闭 ExcelWriter excelWriter.finish(); } private static TableStyle createTableStyle() { TableStyle tableStyle = new TableStyle(); tableStyle.setTableContentBackGroundColor(IndexedColors.WHITE); tableStyle.setTableContentFontName("宋体"); tableStyle.setTableContentFontSize(12); tableStyle.setTableHeadBackGroundColor(IndexedColors.GREY_25_PERCENT); tableStyle.setTableHeadFontName("宋体"); tableStyle.setTableHeadFontSize(14); tableStyle.setTableHeadFontBold(true); tableStyle.setTableHeadFontColor(IndexedColors.WHITE); return tableStyle; } } ``` 在这个例子中,我们使用 `ExcelWriter` 和 `Sheet` 类来写入数据,使用 `createTableStyle()` 方法创建表格样式。注意,需要设置表格样式,否则导出的 Excel 可能会出现样式问题。 最后,在需要导出 Excel 的方法中调用 `ExcelExportUtil.exportExcel()` 方法即可。例如: ```java public class ExportController { @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { List<ExportData> dataList = // 查询数据... ExcelExportUtil.exportExcel(dataList, "export", response); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值