7
版本
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta1</version>
</dependency>
异常代码
实体对象package org.shiloh.easyexcel.test.write.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.util.Date;
/**
* @author lxlei
* @description 基础数据,测试用注解定义excel写出的数据的样式
* @date 2020/3/31 11:33
*/
@Data
/**
* 设置标题头样式:
* fillPatternType:设置填充前景类型为直线{@link FillPatternType#SOLID_FOREGROUND}
* fillBackgroundColor:填充背景色为红色{@link org.apache.poi.ss.usermodel.IndexedColors#RED}
*/
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillBackgroundColor = 10)
// 设置标题头字体高度为20
@HeadFontStyle(fontHeightInPoints = 20)
/**
* 设置内容样式:
* fillPatternType:设置填充前景类型为直线{@link FillPatternType#SOLID_FOREGROUND}
* fillBackgroundColor:填充背景色为绿色{@link org.apache.poi.ss.usermodel.IndexedColors#GREEN}
*/
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillBackgroundColor = 17)
// 设置内容字体高度为20
@ContentFontStyle(fontHeightInPoints = 20)
public class DemoStyleData {
// 单独设置一下字符串标题的样式
/**
* 字符串标题头样式:
* fillPatternType:填充图案前景类型为直线{@link FillPatternType#SOLID_FOREGROUND}
* fillBackgroundColor:标题头的背景色单独设置为粉红色{@link org.apache.poi.ss.usermodel.IndexedColors#PINK}
*/
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillBackgroundColor = 14)
// 字符串标题头的字体高度设置为30
@HeadFontStyle(fontHeightInPoints = 30)
/**
* 字符串内容样式:
* fillPatternType:填充图案前景类型为直线{@link FillPatternType#SOLID_FOREGROUND}
* fillBackgroundColor:标题头的背景色单独设置为天蓝色{@link org.apache.poi.ss.usermodel.IndexedColors#SKY_BLUE}
*/
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillBackgroundColor = 40)
// 字符串内容的字体高度设置为30
@ContentFontStyle(fontHeightInPoints = 30)
@ExcelProperty("字符串标题")
private String str;
@ExcelProperty("日期标题")
private Date date;
@ExcelProperty("数字标题")
private Double doubleData;
}
测试代码
/**
* 通过注解自定义写入样式来写入数据
* 1.创建excel对应的实体对象,参照{@link DemoStyleData}
* 2.直接写入
*/
@Test
public void writeExcelWithAnnotationStyle() {
String fileName = "D:\\shiloh\\style_test_demo.xlsx";
EasyExcel.write(fileName, DemoStyleData.class)
.sheet("写入测试")
.doWrite(getData());
}
理想的写入效果
实际写入效果