java使用easyexcel导出文件,自定义样式

2023-12-21 17:08:08

实体类

package com.control.domain.vo;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.FillPatternTypeEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.util.Date;

/**
 * 描述 : TODO<br/>
 * 作者 : <br/>
 * 时间 : 2023-12-21 9:00:41<br/>
 */
@Data
@EqualsAndHashCode
@HeadStyle
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, borderTop = BorderStyleEnum.THIN,
		borderBottom = BorderStyleEnum.THIN, borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN)
@ContentFontStyle(fontHeightInPoints = 12)
@ContentRowHeight(30)
@HeadRowHeight(30)
public class OutAluminumTaskExcel {

	@ColumnWidth(value = 7)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "报告时间", "序号" })
	private Integer id;

	@ColumnWidth(value = 20)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "报告时间", "单位" })
	private String pApartment;

	@ColumnWidth(value = 15)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "槽号" })
	private String slotNo;

	/**
	 * 实际出铝量
	 */
	@ColumnWidth(value = 15)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "任务量(kg)" })
	private String realOutAluminumWeight;

	/**
	 * 计划出铝量
	 */
	@ColumnWidth(value = 15)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "实出量(kg)" })
	private String planOutAluminumWeight;

	@ColumnWidth(value = 20)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "出铝日期", "出铝偏差(kg)" })
	private String diffOutAluminumWeight;

	@ColumnWidth(value = 15)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "偏差等级" })
	private String taskLevel;

	@ColumnWidth(value = 15)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "任务类型" })
	private String taskName;

	@ColumnWidth(value = 10)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "责任人" })
	private String taskSendPerson;

	@ColumnWidth(value = 22)
	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "任务发出时间" })
	private Date createTime;

	@ExcelProperty({ "偏差统计及任务列表", "偏差统计及任务列表", "", "建议" })
	@ColumnWidth(value = 150)
	@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT)
	private String opinionNote;

}

测试类中的测试方法

@Disabled
	@DisplayName("excel绘制")
	@Test
	public void exportExcel() throws IOException {
		// pOutAluminumTasksController.exportExcel(null);
		String path = "C:\\Users\\xxx\\Desktop\\";
		String fileName = path + "偏差统计及任务列表" + ".xlsx";
		List<OutAluminumTaskExcel> outAluminumTask = pTaskMapper.getOutAluminumTask(DateUtil.beginOfDay(new Date()));
		// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
		EasyExcel.write(fileName, OutAluminumTaskExcel.class).registerWriteHandler(new CellWriteHandler() {
			@Override
			public void afterCellDispose(CellWriteHandlerContext context) {
				if (context.getRowIndex() == 0) {
					if (context.getColumnIndex() == 0) {
						Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
						Font font = workbook.createFont();
						font.setFontHeightInPoints((short) 32);
						font.setColor(Font.COLOR_RED);
						font.setFontName("宋体");
						CellStyle cellStyle = workbook.createCellStyle();
						cellStyle.setFillBackgroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
						// cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
						cellStyle.setFont(font);
						cellStyle.setAlignment(HorizontalAlignment.CENTER);
						cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
						context.getCell().setCellStyle(cellStyle);
						context.getFirstCellData().setWriteCellStyle(null);
					}
				}
				else if (context.getRowIndex() == 2) {
					if (context.getColumnIndex() == 2) {
						context.getCell().setCellValue(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
					}
					else if (context.getColumnIndex() == 6) {
						context.getCell().setCellValue(DateUtil.format(new Date(), "yyyy年MM月dd日"));
					}
				}

			}
		}).sheet("偏差统计及任务列表").doWrite(outAluminumTask);
	}

效果图
在这里插入图片描述

文章来源:https://blog.csdn.net/weixin_54089340/article/details/135130356
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。