easyExcel设置单个单元格(颜⾊)样式相关代码如下(注释⽐较多,我就不细讲了,有问题留⾔~):
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta5</version>
</dependency>
import l.annotation.ExcelProperty;
import l.metadata.BaseRowModel;
/**
* blog.csdn/abc20090208/article/details/89054599  阿⾥开源Easy-Excel单元格样式调整
*
* @ExcelProperty注解是⽤来标记字段在Excel中的表头,value值⽀持多级表头,⽤⼀级表头⼀致框架
* ⾃动会对表头进⾏合并index是⽤来标记在Excel中的顺序(不是Excel中的位置),因为项⽬实际需要中有⼏个字段是可选
* 导出的,所以index没有设置成连续的,其次导出的字段中总量要求有千分位分隔符,两个满⾜率字段要求有百分号,因此
* 需要单独设置单元格的样式。
**/
public class BasePurchaseExecutionResponse extends BaseRowModel {
/**
* 序号
*/
@ExcelProperty(value = {"", "", "序号"}, index = 0)
private String num;
/**
* 供应商类型
*/
@ExcelProperty(value = {"", "", "供应商类型"}, index = 1)
private String supplierType;
/**
* 品牌
*/
@ExcelProperty(value = {"", "", "品牌"}, index = 2)
private String brandNameListString;
/**
* 年份
*/
@ExcelProperty(value = {"", "", "年份"}, index = 3)
private String productYear;
/**
* 产品季节
*/
@ExcelProperty(value = {"", "", "产品季节"}, index = 4)
private String productSeason;
/
**
* 总量
*/
@ExcelProperty(value = {"", "", "总量"}, index = 9)
private int totalShipment;
/**
* 计划交期满⾜率
* 计划交期满⾜率
*/
@ExcelProperty(value = {"", "", "计划交期满⾜率"}, index = 10)
private String planDeliverRate;
/
**
* 确认交期满⾜率
*/
@ExcelProperty(value = {"", "", "确认交期满⾜率"}, index = 11)
private String confirmDeliverRate;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSupplierType() {
return supplierType;
}
public void setSupplierType(String supplierType) {
this.supplierType = supplierType;
}
public String getBrandNameListString() {
return brandNameListString;
}
public void setBrandNameListString(String brandNameListString) {        this.brandNameListString = brandNameListString;
}
public String getProductYear() {
return productYear;
}
public void setProductYear(String productYear) {
this.productYear = productYear;
}
public String getProductSeason() {
return productSeason;
}
public void setProductSeason(String productSeason) {
this.productSeason = productSeason;
}
public int getTotalShipment() {
return totalShipment;
}
public void setTotalShipment(int totalShipment) {
}
public String getPlanDeliverRate() {
return planDeliverRate;
}
public void setPlanDeliverRate(String planDeliverRate) {
this.planDeliverRate = planDeliverRate;
}
public String getConfirmDeliverRate() {
return confirmDeliverRate;
}
public void setConfirmDeliverRate(String confirmDeliverRate) {
}
}
import l.event.WriteHandler;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import java.math.BigDecimal;
/**
* @author Eric on 2019/4/5.
* @version 1.0
*/
public class StyleExcelHandler implements WriteHandler {
private static int time=0;
@Override
public void sheet(int i, Sheet sheet) {
}
@Override
public void row(int i, Row row) {
}
@Override
public void cell(int i, Cell cell) {
// 从第⼆⾏开始设置格式,第⼀⾏是表头
//这⾥可以获得Workbook是因为Sheet类有这个接⼝,但是其他地⽅没有对应的Sheet,所以要获得的话,需要⽤到反射了
Workbook workbook = Sheet().getWorkbook();
CellStyle cellStyle = createStyle(workbook);
if (RowIndex() > 2) {
if (i == 5) {
DataFormat dataFormat = ateDataFormat();
// 设置千位分隔符
cellStyle.Format("#,##0"));
time++;
if(time==2){
//这个终于可以设置颜⾊了  blog.csdn/z1074907546/article/details/50544178 这个链接有颜⾊说明。
//setFillPattern是设置单元格填充样式,SOLID_FOREGROUND纯⾊使⽤前景颜⾊填充,接着设置前景颜⾊
// (setFillForegroundColor)就可以给单元格着⾊了。setFillForegroundColor()⽅法的参数是⼀个short
类型,
// easyExcel使⽤索引来代表颜⾊,默认已经有⼀些颜⾊了。可以⾃定义颜⾊,可以⾃定义颜⾊,可以⾃定义颜⾊。
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//设置前景填充样式
cellStyle.setFillForegroundColor(HSSFColor.DARK_RED.index);//前景填充⾊
//cellStyle.setFillForegroundColor(Index());
}
}
if (i == 7 || i == 6) {
String stringCellValue = StringCellValue();
cell.setCellValue(new placeAll("%", "")).divide(new BigDecimal(100), 8, BigDecimal.ROUND_HALF_UP).setScale(4, BigDe
cell.setCellValue(new placeAll("%", "")).divide(new BigDecimal(100), 8, BigDecimal.ROUND_HALF_UP).setScale(4                // 设置百分⽐
cellStyle.BuiltinFormat("0.00%"));
}
if (i == 0 || i == 3) {
cell.setCellValue(Long.StringCellValue()));
}
}
}
/**
* 实际中如果直接获取原单元格的样式进⾏修改, 最后发现是改了整⾏的样式, 因此这⾥是新建⼀个样* 式
*/
private CellStyle createStyle(Workbook workbook) {
CellStyle cellStyle = ateCellStyle();
// 下边框
cellStyle.setBorderBottom(BorderStyle.THIN);
// 左边框
cellStyle.setBorderLeft(BorderStyle.THIN);
// 上边框
cellStyle.setBorderTop(BorderStyle.THIN);
// 右边框
cellStyle.setBorderRight(BorderStyle.THIN);
// ⽔平对齐⽅式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//cellStyle.setFillForegroundColor(Index());
// 垂直对齐⽅式
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
}
import l.ExcelWriter;
import l.metadata.BaseRowModel;
import l.metadata.Sheet;
import l.support.ExcelTypeEnum;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/
**
* easyExcel不⾜之处:
*    样式的设置可维护性太差,通过在样式类中硬编码各个列号对应的样式确实不太好,问题点在于,单元格样式的创建需要
*    workBook对象实例才可以,框架本⾝并没有提供获取wirkBook的⽅法,因此,如果想要在其他地⽅设置样式的话,可以
*    通过反射的⽅式来获取workbook对象
*/
public class test3 {
public static void main(String[] args) throws IOException {
StyleExcelHandler handler = new StyleExcelHandler();
OutputStream outputStream = new FileOutputStream("D://2007.xlsx");
OutputStream outputStream = new FileOutputStream("D://2007.xlsx");
// 这⾥要把上⾯创建的样式类通过构造函数传⼊
ExcelWriter writer = new ExcelWriter(null, outputStream, ExcelTypeEnum.XLSX, true, handler);htmlborder
Sheet sheet1 = new Sheet(1, 1, BasePurchaseExecutionResponse.class, "含供应商和地区", null);
//设置列宽设置每列的宽度
Map columnWidth = new HashMap();
columnWidth.put(0,10000);columnWidth.put(1,10000);columnWidth.put(2,10000);columnWidth.put(3,10000);        sheet1.setColumnWidthMap(columnWidth);
//或⾃适应宽度
//sheet1.setAutoWidth(true);
writer.write(createResponseList(), sheet1);
writer.finish();
outputStream.close();
}
/**
* 创建数据集合
*
* @return
*/
private static List<? extends BaseRowModel> createResponseList() {
List<BasePurchaseExecutionResponse> responses = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
BasePurchaseExecutionResponse response = new BasePurchaseExecutionResponse();
response.setTotalShipment(i * 1000000);
response.setConfirmDeliverRate( i+ "%");
response.setNum(String.valueOf(i));
response.setProductSeason("冬收到甲⽅");
response.setProductYear("19");
response.setSupplierType("本⼚");
response.setBrandNameListString("耐特");
response.setPlanDeliverRate(i * 2 + "%");
responses.add(response);
}
return responses;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。