songguoqiang
7 months ago
7 changed files with 128 additions and 1 deletions
@ -0,0 +1,88 @@ |
|||
package com.win.framework.excel.core.handler; |
|||
|
|||
import com.alibaba.excel.metadata.Head; |
|||
import com.alibaba.excel.metadata.data.WriteCellData; |
|||
import com.alibaba.excel.write.handler.CellWriteHandler; |
|||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
|||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
|||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
|||
import com.alibaba.excel.write.metadata.style.WriteFont; |
|||
import com.win.framework.excel.core.annotations.Colour; |
|||
import com.win.framework.excel.core.annotations.ExcelValid; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.poi.ss.usermodel.*; |
|||
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
|||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; |
|||
|
|||
import java.lang.reflect.Field; |
|||
import java.util.List; |
|||
|
|||
@Slf4j |
|||
public class CustomCellWriteHandler implements CellWriteHandler { |
|||
|
|||
@Override |
|||
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
|||
if(isHead){ |
|||
WriteCellData writeCellData = cellDataList.get(0); |
|||
WriteCellStyle headWriteCellStyle = writeCellData.getOrCreateStyle(); |
|||
// 头的策略 样式调整
|
|||
Field field = head.getField(); |
|||
if (field.isAnnotationPresent(Colour.class)) { |
|||
// 如果是头部 主表数据
|
|||
headWriteCellStyle.setFillForegroundColor(field.getAnnotation(Colour.class).value().getIndex()); |
|||
}else { |
|||
//头背景 浅绿 子表数据
|
|||
headWriteCellStyle.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); |
|||
} |
|||
WriteFont headWriteFont = new WriteFont(); |
|||
// 头字号
|
|||
headWriteFont.setFontHeightInPoints((short) 14); |
|||
// 字体样式
|
|||
headWriteFont.setFontName("宋体"); |
|||
headWriteCellStyle.setWriteFont(headWriteFont); |
|||
// 自动换行
|
|||
headWriteCellStyle.setWrapped(true); |
|||
// 设置细边框
|
|||
headWriteCellStyle.setBorderBottom(BorderStyle.THIN); |
|||
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); |
|||
headWriteCellStyle.setBorderRight(BorderStyle.THIN); |
|||
headWriteCellStyle.setBorderTop(BorderStyle.THIN); |
|||
|
|||
// 设置必填项备注消息
|
|||
if(field.isAnnotationPresent(ExcelValid.class)){ |
|||
Sheet sheet = writeSheetHolder.getSheet(); |
|||
// 备注
|
|||
Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch(); |
|||
Comment comment = drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), 0, (short) cell.getColumnIndex()+1, 1)); |
|||
comment.setString(new XSSFRichTextString(field.getAnnotation(ExcelValid.class).message())); |
|||
// 将批注添加到单元格对象中
|
|||
sheet.getRow(cell.getRowIndex()).getCell(cell.getColumnIndex()).setCellComment(comment); |
|||
} |
|||
// 设置边框颜色 25灰度
|
|||
headWriteCellStyle.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
|||
headWriteCellStyle.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
|||
headWriteCellStyle.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
|||
headWriteCellStyle.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); |
|||
// 水平对齐方式
|
|||
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
|||
// 垂直对齐方式
|
|||
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|||
}else { |
|||
for(int i=1;i<cellDataList.size();i++){ |
|||
WriteCellData writeCellData = cellDataList.get(i); |
|||
// 内容的策略 宋体
|
|||
WriteCellStyle contentStyle = writeCellData.getOrCreateStyle(); |
|||
// 设置垂直居中
|
|||
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
|||
// 设置 水平居中
|
|||
contentStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
|||
WriteFont contentWriteFont = new WriteFont(); |
|||
// 内容字号
|
|||
contentWriteFont.setFontHeightInPoints((short) 12); |
|||
// 字体样式
|
|||
contentWriteFont.setFontName("宋体"); |
|||
contentStyle.setWriteFont(contentWriteFont); |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue