java
复制代码
package com.yunjin.matyl.domain.dto.pbms;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.yunjin.matyl.domain.vo.pbms.MatYlQcNewVo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jetbrains.annotations.NotNull;
import java.text.SimpleDateFormat;
/**.
* 处理单元格类
*
* @author ck
* @since 2025-10-11
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MatYlQcNewSheetWriteHandler implements SheetWriteHandler, WorkbookWriteHandler {
/**.
* 不固定内容的单元格,定义变量
*/
private MatYlQcNewVo mainVo;
/**.
* 不固定内容的单元格,定义变量
*/
private MatYlQcNewVo matYlQcNewVo;
/**
* .
* 设置表头样式
*
* @param workbook 工作薄
* @return 样式对象
*/
private static @NotNull CellStyle getTitleCellStyle(Workbook workbook) {
CellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleCellStyle.setBorderLeft(BorderStyle.THIN);
titleCellStyle.setBorderTop(BorderStyle.THIN);
titleCellStyle.setBorderRight(BorderStyle.THIN);
titleCellStyle.setBorderBottom(BorderStyle.THIN);
titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
Font titleFont = workbook.createFont(); // 创建字体样式
titleFont.setBold(true);
titleFont.setFontHeightInPoints((short) 10); // 使用磅值(points)设置,常规大小
titleCellStyle.setFont(titleFont); // 设置单元格样式
return titleCellStyle;
}
// 在创建工作簿之前,在创建工作表之前执行
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
// 在创建工作簿之后,在创建工作表之前执行
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
// 创建SHEET页
Sheet sheet = workbook.getSheetAt(0);
setSheetColWeight(sheet); //设置每列列宽
CellStyle cellStyle = workbook.createCellStyle();
buildCellStyle(cellStyle);
Font font = workbook.createFont(); // 创建字体样式
font.setBold(true);
font.setFontHeightInPoints((short) 10); // 使用磅值(points)设置,常规大小
cellStyle.setFont(font); // 设置单元格样式
CellStyle cellStyle2 = workbook.createCellStyle();
buildCellStyle2(cellStyle2);
Font font2 = workbook.createFont(); // 创建字体样式
font2.setBold(false);
font.setFontHeightInPoints((short) 10); // 使用磅值(points)设置,常规大小
cellStyle2.setFont(font2); // 设置单元格样式
buildOneToTherrCol(sheet, workbook, cellStyle, cellStyle2); //创建 1-3列
buildFourToSixCol(sheet, cellStyle, cellStyle2); //创建 4-6列
}
/**.
* 创建 1-3列
* @param sheet sheet
* @param cellStyle cellStyle
* @param cellStyle2 cellStyle2
*/
private void buildFourToSixCol(Sheet sheet, CellStyle cellStyle, CellStyle cellStyle2) {
// --- 【第四行】 ---
Row row4 = sheet.createRow(3);
// 1-2列
Cell cell41 = row4.createCell(0);
cell41.setCellValue("库位");
cell41.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 1));
setBorderForMergedRegion(sheet, new CellRangeAddress(3, 3, 0, 1), BorderStyle.THIN);
// 3列
Cell cell43 = row4.createCell(2);
cell43.setCellValue(mainVo.getWareHouseName());
cell43.setCellStyle(cellStyle2);
// 4列
Cell cell44 = row4.createCell(3);
cell44.setCellValue("皮重(kg)");
cell44.setCellStyle(cellStyle);
// 5列
Cell cell45 = row4.createCell(4);
cell45.setCellValue(mainVo.getAvgweight().doubleValue()-200);
cell45.setCellStyle(cellStyle2);
// 6列
Cell cell46 = row4.createCell(5);
cell46.setCellValue("尾箱");
cell46.setCellStyle(cellStyle);
// 7列
Cell cell47 = row4.createCell(6);
cell47.setCellValue(mainVo.getTrunk()==1?"有":"无");
cell47.setCellStyle(cellStyle2);
// 8列
Cell cell48 = row4.createCell(7);
cell48.setCellValue("烟叶结构");
cell48.setCellStyle(cellStyle);
// 9列
Cell cell49 = row4.createCell(8);
cell49.setCellValue(mainVo.getLeafNotch());
cell49.setCellStyle(cellStyle2);
// --- 【第五行】 ---
Row row5 = sheet.createRow(4);
Cell cell51 = row5.createCell(0);
cell51.setCellValue("来料类型");
cell51.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 1));
setBorderForMergedRegion(sheet, new CellRangeAddress(4, 4, 0, 1), BorderStyle.THIN);
Cell cell53 = row5.createCell(2);
cell53.setCellValue(mainVo.getYlarrivalbusitypeName());
cell53.setCellStyle(cellStyle2);
sheet.addMergedRegion(new CellRangeAddress(4, 4, 2, 3));
setBorderForMergedRegion(sheet, new CellRangeAddress(4, 4, 2, 3), BorderStyle.THIN);
Cell cell54 = row5.createCell(4);
cell54.setCellValue("加工单位");
cell54.setCellStyle(cellStyle);
Cell cell55 = row5.createCell(5);
cell55.setCellValue(mainVo.getSuppliername());
cell55.setCellStyle(cellStyle2);
sheet.addMergedRegion(new CellRangeAddress(4, 4, 5, 6));
setBorderForMergedRegion(sheet, new CellRangeAddress(4, 4, 5, 6), BorderStyle.THIN);
// 4列
Cell cell57 = row5.createCell(7);
cell57.setCellValue("内衬膜");
cell57.setCellStyle(cellStyle);
// 5列
Cell cell58 = row5.createCell(8);
cell58.setCellValue(mainVo.getInsidebag() == 1 ? "有" : "无");
cell58.setCellStyle(cellStyle2);
// --- 【第六行】 ---
Row row6 = sheet.createRow(5);
Cell cell61 = row6.createCell(0);
cell61.setCellValue("到货时间");
cell61.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(5, 5, 0, 1));
setBorderForMergedRegion(sheet, new CellRangeAddress(5, 5, 0, 1), BorderStyle.THIN);
Cell cell63 = row6.createCell(2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = mainVo.getArrivalstartdate() != null
?
sdf.format(mainVo.getArrivalstartdate()) : "";
cell63.setCellValue(formattedDate);
cell63.setCellStyle(cellStyle2);
sheet.addMergedRegion(new CellRangeAddress(5, 5, 2, 3));
setBorderForMergedRegion(sheet, new CellRangeAddress(5, 5, 2, 3), BorderStyle.THIN);
Cell cell64 = row6.createCell(4);
cell64.setCellValue("检验时间");
cell64.setCellStyle(cellStyle);
Cell cell65 = row6.createCell(5);
String formattedDate2 = mainVo.getCheckdate() != null
?
sdf.format(mainVo.getCheckdate()) : "";
cell65.setCellValue(formattedDate2);
cell65.setCellStyle(cellStyle2);
sheet.addMergedRegion(new CellRangeAddress(5, 5, 5, 6));
setBorderForMergedRegion(sheet, new CellRangeAddress(5, 5, 5, 6), BorderStyle.THIN);
// 4列
Cell cell67 = row6.createCell(7);
cell67.setCellValue("到货数量");
cell67.setCellStyle(cellStyle);
// 5列
Cell cell68 = row6.createCell(8);
cell68.setCellValue(mainVo.getArrivalcount().doubleValue());
cell68.setCellStyle(cellStyle2);
}
/**.
* 创建 4-6列
* @param sheet sheet
* @param cellStyle cellStyle
* @param workbook workbook
* @param cellStyle2 cellStyle2
*/
private void buildOneToTherrCol(Sheet sheet, Workbook workbook, CellStyle cellStyle, CellStyle cellStyle2) {
// --- 【第一行】 ---
Row row1 = sheet.createRow(0); //创建第一行-空行
row1.setHeight((short) 500); //设置行高为500
Cell cell = row1.createCell(0);
cell.setCellValue(""); //设置单元格内容
// --- 【第二行】 ---
Row row2 = sheet.createRow(1); // 创建第二行
row2.setHeight((short) 500); //设置行高
Cell cell1 = row2.createCell(0); // 创建单元格
cell1.setCellValue("烟叶入库质量抽检单"); // 设置标题内容
CellStyle titleCellStyle = getTitleCellStyle(workbook);
cell1.setCellStyle(titleCellStyle);
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 8));
setBorderForMergedRegion(sheet, new CellRangeAddress(1, 1, 0, 8), BorderStyle.THIN);
// --- 【第三行】 ---
Row row3 = sheet.createRow(2); // 创建第三行
// 1-2列
Cell cell3 = row3.createCell(0); // 在第3行的第1列创建单元格并设置内容
cell3.setCellValue("产地");
cell3.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));
setBorderForMergedRegion(sheet, new CellRangeAddress(2, 2, 0, 1), BorderStyle.THIN);
// 3列
Cell cell4 = row3.createCell(2);
cell4.setCellValue(mainVo.getTobacOrig());
cell4.setCellStyle(cellStyle2);
// 4列
Cell cell5 = row3.createCell(3);
cell5.setCellValue("等级");
cell5.setCellStyle(cellStyle);
// 5列
Cell cell6 = row3.createCell(4);
cell6.setCellValue(mainVo.getTobacLevel());
cell6.setCellStyle(cellStyle2);
// 6列
Cell cell7 = row3.createCell(5);
cell7.setCellValue("年份");
cell7.setCellStyle(cellStyle);
// 7列
Cell cell8 = row3.createCell(6);
cell8.setCellValue(mainVo.getTobacYear());
cell8.setCellStyle(cellStyle2);
// 8列
Cell cell9 = row3.createCell(7);
cell9.setCellValue("烟叶类型");
cell9.setCellStyle(cellStyle);
// 9列
Cell cell10 = row3.createCell(8);
cell10.setCellValue(mainVo.getTobacType());
cell10.setCellStyle(cellStyle2);
}
/**.
* 创建样式2
* @param cellStyle2 cellStyle2
*/
private static void buildCellStyle2(CellStyle cellStyle2) {
cellStyle2.setBorderLeft(BorderStyle.THIN);
cellStyle2.setBorderTop(BorderStyle.THIN);
cellStyle2.setBorderRight(BorderStyle.THIN);
cellStyle2.setBorderBottom(BorderStyle.THIN);
cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle2.setAlignment(HorizontalAlignment.CENTER);
}
/**.
* 创建样式1
* @param cellStyle cellStyle1
*/
private static void buildCellStyle(CellStyle cellStyle) {
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
}
/**.
* 设置列宽
* @param sheet sheet
*/
private static void setSheetColWeight(Sheet sheet) {
sheet.setColumnWidth(0, 5 * 256); // 第1列:15个字符宽度
sheet.setColumnWidth(1, 5 * 256); // 第2列:15个字符宽度
sheet.setColumnWidth(2, 8 * 256); // 第3列:20个字符宽度
sheet.setColumnWidth(3, 9 * 256); // 第4列:10个字符宽度
sheet.setColumnWidth(4, 8 * 256); // 第5列:20个字符宽度
sheet.setColumnWidth(5, 8 * 256); // 第6列:10个字符宽度
sheet.setColumnWidth(6, 6 * 256); // 第7列:15个字符宽度
sheet.setColumnWidth(7, 9 * 256); // 第8列:12个字符宽度
sheet.setColumnWidth(8, 6 * 256); // 第9列:20个字符宽度
}
// 工作簿处理完毕后调用
@Override
public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
// --- 【备注行】 ---
// 创建用于备注的单元格样式(左对齐)
CellStyle style1 = workbook.createCellStyle();
style1.setBorderLeft(BorderStyle.THIN);
style1.setBorderTop(BorderStyle.THIN);
style1.setBorderRight(BorderStyle.THIN);
style1.setBorderBottom(BorderStyle.THIN);
style1.setVerticalAlignment(VerticalAlignment.CENTER);
style1.setAlignment(HorizontalAlignment.LEFT);
style1.setWrapText(true);
// 在数据后添加备注行
int lastRowNum = sheet.getLastRowNum();
Row avgRow = sheet.createRow(lastRowNum + 1);
//avgRow.setHeight((short) 800);
Cell avgCell1 = avgRow.createCell(0);
avgCell1.setCellValue("平均值:");
avgCell1.setCellStyle(style1);
sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 0, 2));
setBorderForMergedRegion(sheet, new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 0, 2), BorderStyle.THIN);
Cell avgCell2 = avgRow.createCell(3);
avgCell2.setCellValue(mainVo.getAvgweight().doubleValue());
avgCell2.setCellStyle(style1);
Cell avgCell3 = avgRow.createCell(4);
avgCell3.setCellValue(mainVo.getAvgwater().doubleValue());
avgCell3.setCellStyle(style1);
Cell checkPersonCell1 = avgRow.createCell(5);
checkPersonCell1.setCellValue("检查人:");
checkPersonCell1.setCellStyle(style1);
sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 5, 6));
setBorderForMergedRegion(sheet, new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 5, 6), BorderStyle.THIN);
Cell checkPersonCell2 = avgRow.createCell(7);
checkPersonCell2.setCellValue(mainVo.getCheckPersonName());
checkPersonCell2.setCellStyle(style1);
sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 7, 8));
setBorderForMergedRegion(sheet, new CellRangeAddress(lastRowNum + 1, lastRowNum + 1, 7, 8), BorderStyle.THIN);
Row zhuRow = sheet.createRow(lastRowNum + 2);
zhuRow.setHeight((short) 600);
Cell zhuCell1 = zhuRow.createCell(0);
zhuCell1.setCellValue("注");
zhuCell1.setCellStyle(style1);
Cell zhuCell2 = zhuRow.createCell(1);
zhuCell2.setCellValue(matYlQcNewVo.getJudgerule());
zhuCell2.setCellStyle(style1);
// 合并单元格区域(跨0-38列,占3行)
sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 2, lastRowNum + 2, 1, 8));
setBorderForMergedRegion(sheet, new CellRangeAddress(lastRowNum + 2, lastRowNum + 2, 1, 8), BorderStyle.THIN);
// --- 【最后一行】 ---
Row row4 = sheet.createRow(lastRowNum + 3); // 创建第三行
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.TOP);
cellStyle.setAlignment(HorizontalAlignment.LEFT);
Font font = workbook.createFont();
font.setBold(false);
font.setFontHeightInPoints((short) 10); // 使用磅值(points)设置,常规大小
cellStyle.setFont(font);
Cell cell13 = row4.createCell(0);
cell13.setCellValue("备注:");
cell13.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(lastRowNum + 3, lastRowNum + 4, 0, 8));
setBorderForMergedRegion(sheet, new CellRangeAddress(lastRowNum + 3, lastRowNum + 4, 0, 8), BorderStyle.THIN);
}
/**.
* 为合并区域设置完整边框
* @param sheet sheet
* @param region region
* @param borderStyle borderStyle
*/
private void setBorderForMergedRegion(Sheet sheet, CellRangeAddress region, BorderStyle borderStyle) {
for (int row = region.getFirstRow(); row <= region.getLastRow(); row++) {
Row rowObj = sheet.getRow(row);
if (rowObj == null) {
rowObj = sheet.createRow(row);
}
for (int col = region.getFirstColumn(); col <= region.getLastColumn(); col++) {
Cell cell = rowObj.getCell(col);
if (cell == null) {
cell = rowObj.createCell(col);
}
// 获取或创建单元格样式
CellStyle style = cell.getCellStyle();
if (style == null) {
style = sheet.getWorkbook().createCellStyle();
} else {
// 复制现有样式
CellStyle newStyle = sheet.getWorkbook().createCellStyle();
newStyle.cloneStyleFrom(style);
style = newStyle;
}
// 设置上边框(只有最上面的行)
if (row == region.getFirstRow()) {
style.setBorderTop(borderStyle);
}
// 设置下边框(只有最下面的行)
if (row == region.getLastRow()) {
style.setBorderBottom(borderStyle);
}
// 设置左边框(只有最左边的列)
if (col == region.getFirstColumn()) {
style.setBorderLeft(borderStyle);
}
// 设置右边框(只有最右边的列)
if (col == region.getLastColumn()) {
style.setBorderRight(borderStyle);
}
cell.setCellStyle(style);
}
}
}
}