easyexcel加粗边框
package org.zt.task.report.listener;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
/**
* @author liuhuapeng
* @date 2024/2/29
*/
public class CustomCellWriteHandler extends AbstractVerticalCellStyleStrategy {
@Override
protected WriteCellStyle headCellStyle(Head head) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
writeCellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
WriteFont writeFont = new WriteFont();
writeFont.setBold(false);
writeFont.setFontHeightInPoints(Short.valueOf((short)15));
writeCellStyle.setWriteFont(writeFont);
return writeCellStyle;
}
// 重写定义内容部分样式的方法
@Override
protected WriteCellStyle contentCellStyle(Head head) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeCellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
writeCellStyle.setBorderTop(BorderStyle.THIN);
writeCellStyle.setBorderBottom(BorderStyle.THIN);
writeCellStyle.setBorderLeft(BorderStyle.THIN);
writeCellStyle.setBorderRight(BorderStyle.THIN);
return writeCellStyle;
}
}
测试
// 指定输出的Excel文件路径
String excelFilePath = "example-write.xlsx";
//设置要输出的数据内容
List<Person> personList = new ArrayList<>();
personList.add(new Person("a01", "张三", "男", 10));
personList.add(new Person("a02", "李四", "男", 20));
personList.add(new Person("a03", "王五", "女", 30));
CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler();
EasyExcel.write(excelFilePath, Person.class)
.registerWriteHandler(customCellWriteHandler)
.sheet("testSheet")
.doWrite(personList);
person.class
package org.zt.task.report.listener;
import lombok.Data;
/**
* @author liuhuapeng
* @date 2024/2/29
*/
@Data
public class Person {
private String name;
private String sex;
private String tax;
private int age;
public Person(String name, String sex, String tax, int age) {
this.name = name;
this.sex = sex;
this.tax = tax;
this.age = age;
}
}