|
|
|
@ -0,0 +1,504 @@
|
|
|
|
|
package com.op.common.core.utils.poi;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.op.common.core.domain.ExcelCol;
|
|
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Excel导出报告相关处理
|
|
|
|
|
*
|
|
|
|
|
* @author OP
|
|
|
|
|
*/
|
|
|
|
|
public class ExcelSCXJReportMapUtil {
|
|
|
|
|
//下载
|
|
|
|
|
public static <T> SXSSFWorkbook initWorkbook(String sheetName , String title, List<ExcelCol> excelCol , List<T> data, Map<String,String> detailMap,String shiftId) throws IOException {
|
|
|
|
|
SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
|
|
int colSize = excelCol.size();
|
|
|
|
|
|
|
|
|
|
//创建Sheet(工作簿)
|
|
|
|
|
Sheet sheet = null;
|
|
|
|
|
if (!StringUtils.hasText(sheetName)){
|
|
|
|
|
sheet = workbook.createSheet();
|
|
|
|
|
}else{
|
|
|
|
|
sheet = workbook.createSheet(sheetName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//单元格宽度
|
|
|
|
|
for(int md=0;md<14;md++){
|
|
|
|
|
if(md==1){
|
|
|
|
|
sheet.setColumnWidth(md, 6000);
|
|
|
|
|
}else if(md>1){
|
|
|
|
|
sheet.setColumnWidth(md, 4000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从resources目录获取图片字节数组
|
|
|
|
|
byte[] imageBytes = getImageBytesFromResources("image/logo.png");
|
|
|
|
|
/**logo**/
|
|
|
|
|
int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
|
|
|
|
|
CreationHelper helper = workbook.getCreationHelper();
|
|
|
|
|
Drawing<?> drawing = sheet.createDrawingPatriarch();
|
|
|
|
|
ClientAnchor anchor = helper.createClientAnchor();
|
|
|
|
|
anchor.setCol1(0); // B列
|
|
|
|
|
anchor.setRow1(0); // 第2行(Excel的行和列都是从0开始计数的)
|
|
|
|
|
anchor.setCol2(1); // C列(图片宽度跨越的列数,这里设置为1列宽)
|
|
|
|
|
anchor.setRow2(2); // 第6行(图片高度跨越的行数,这里设置为4行高,可以根据图片大小调整)
|
|
|
|
|
Picture pict = drawing.createPicture(anchor, pictureIdx);
|
|
|
|
|
pict.resize(); // 根据图片的实际大小调整图片在Excel中的显示大小
|
|
|
|
|
/**报告标题**/
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 13));
|
|
|
|
|
// 获取合并后的单元格的第一个单元格(即C1),并设置值
|
|
|
|
|
Row row1 = sheet.getRow(0); // 获取第1行(索引为0)
|
|
|
|
|
if (row1 == null) {row1 = sheet.createRow(0); // 如果第1行不存在,则创建它
|
|
|
|
|
}
|
|
|
|
|
Cell cell = row1.getCell(2); // 获取C列(索引为2)的单元格
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row1.createCell(2); // 如果C列的单元格不存在,则创建它
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue("中山榄菊日化实业有限公司"); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(getTitelStyle(workbook)); // 应用样式到单元格
|
|
|
|
|
/**报告二级标题、检验标准**/
|
|
|
|
|
// 合并C3到I4的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, new CellRangeAddress(2, 3, 2, 9), detailMap.get("title"));
|
|
|
|
|
// 合并J3到L4的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, new CellRangeAddress(2, 3, 10, 12), "编码"+detailMap.get("standardNo"));
|
|
|
|
|
/**左右表格1**/
|
|
|
|
|
//画边框
|
|
|
|
|
for(int r5=4;r5<12;r5++){
|
|
|
|
|
Row row05 = sheet.getRow(r5);
|
|
|
|
|
if (row05 == null) {row05 = sheet.createRow(r5);}
|
|
|
|
|
for (int col = 0; col < 14; col++) {
|
|
|
|
|
Cell cell1 = row05.createCell(col);
|
|
|
|
|
cell1.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int d5=12;d5<(12+data.size());d5++){
|
|
|
|
|
Row row05 = sheet.getRow(d5);
|
|
|
|
|
if (row05 == null) {row05 = sheet.createRow(d5);}
|
|
|
|
|
for (int col = 0; col < 14; col++) {
|
|
|
|
|
Cell cell1 = row05.createCell(col);
|
|
|
|
|
cell1.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Row row5 = sheet.getRow(4);
|
|
|
|
|
// 合并A5到B6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 0, 1), "生产车间/线体", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 2, 3), detailMap.get("supplierName")+"/"+detailMap.get("checkLoc"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 4, 4), "产品名称", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 5, 8), detailMap.get("materialName"), true, false, null);
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 9, 10), "生产类型", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 11, 13), detailMap.get("productType"), true, false, null);
|
|
|
|
|
|
|
|
|
|
Row row7 = sheet.getRow(6);
|
|
|
|
|
// 合并A7到B8的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 0, 1), "检验时间", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 2, 3), detailMap.get("incomeTime"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 4, 4), "生产批次", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 5, 13), detailMap.get("incomeBatchNo"), true, false, null);
|
|
|
|
|
/**上下表格2**///-------------------------------------------------
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 9, 0, 0), "序号");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 9, 1, 1), "项目名称");
|
|
|
|
|
|
|
|
|
|
//画时间区间
|
|
|
|
|
if("5".equals(shiftId)){
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 8, 2, 5), "上午");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 8, 6, 9), "下午");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 8, 10, 13), "加班");
|
|
|
|
|
drawDatatimeDay(sheet);
|
|
|
|
|
}else{
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(8, 8, 2, 13), "时间");
|
|
|
|
|
drawDatatimeNight(sheet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//将data中的值填充到excel
|
|
|
|
|
int rowNum = 10;
|
|
|
|
|
if(!CollectionUtils.isEmpty(data)){
|
|
|
|
|
Iterator<T> iterator = data.iterator();
|
|
|
|
|
//遍历数据
|
|
|
|
|
for (;iterator.hasNext();){
|
|
|
|
|
|
|
|
|
|
T obj = iterator.next();//获取当前行对应的数据
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(obj));
|
|
|
|
|
|
|
|
|
|
Row dataRow = sheet.getRow(rowNum);//创建行
|
|
|
|
|
// 假设使用了默认字体和字符宽度来计算行高(这是一个简化的示例)
|
|
|
|
|
int cellHeight = (int) (cell.getStringCellValue().length() * 6); // 每个字符大约15个单位高度
|
|
|
|
|
dataRow.setHeightInPoints(Math.max(dataRow.getHeightInPoints(), cellHeight));
|
|
|
|
|
|
|
|
|
|
Cell cellData0 = dataRow.getCell(0);
|
|
|
|
|
cellData0.setCellValue(getValue(jsonObject.get(excelCol.get(0).getField())));
|
|
|
|
|
cellData0.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData1 = dataRow.getCell(1);
|
|
|
|
|
cellData1.setCellValue(getValue(jsonObject.get(excelCol.get(1).getField())));
|
|
|
|
|
cellData1.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData2 = dataRow.getCell(2);
|
|
|
|
|
cellData2.setCellValue(getValue(jsonObject.get(excelCol.get(2).getField())));
|
|
|
|
|
cellData2.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData3 = dataRow.getCell(3);
|
|
|
|
|
cellData3.setCellValue(getValue(jsonObject.get(excelCol.get(3).getField())));
|
|
|
|
|
cellData3.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData4 = dataRow.getCell(4);
|
|
|
|
|
cellData4.setCellValue(getValue(jsonObject.get(excelCol.get(4).getField())));
|
|
|
|
|
cellData4.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData5 = dataRow.getCell(5);
|
|
|
|
|
cellData5.setCellValue(getValue(jsonObject.get(excelCol.get(5).getField())));
|
|
|
|
|
cellData5.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData6 = dataRow.getCell(6);
|
|
|
|
|
cellData6.setCellValue(getValue(jsonObject.get(excelCol.get(6).getField())));
|
|
|
|
|
cellData6.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData7 = dataRow.getCell(7);
|
|
|
|
|
cellData7.setCellValue(getValue(jsonObject.get(excelCol.get(7).getField())));
|
|
|
|
|
cellData7.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData8 = dataRow.getCell(8);
|
|
|
|
|
cellData8.setCellValue(getValue(jsonObject.get(excelCol.get(8).getField())));
|
|
|
|
|
cellData8.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData9 = dataRow.getCell(9);
|
|
|
|
|
cellData9.setCellValue(getValue(jsonObject.get(excelCol.get(9).getField())));
|
|
|
|
|
cellData9.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData10 = dataRow.getCell(10);
|
|
|
|
|
cellData10.setCellValue(getValue(jsonObject.get(excelCol.get(10).getField())));
|
|
|
|
|
cellData10.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData11 = dataRow.getCell(11);
|
|
|
|
|
cellData11.setCellValue(getValue(jsonObject.get(excelCol.get(11).getField())));
|
|
|
|
|
cellData11.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData12 = dataRow.getCell(12);
|
|
|
|
|
cellData12.setCellValue(getValue(jsonObject.get(excelCol.get(12).getField())));
|
|
|
|
|
cellData12.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
if("5".equals(shiftId)){
|
|
|
|
|
Cell cellData13 = dataRow.getCell(13);
|
|
|
|
|
cellData13.setCellValue(getValue(jsonObject.get(excelCol.get(13).getField())));
|
|
|
|
|
cellData13.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
iterator.remove();
|
|
|
|
|
rowNum++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//合并
|
|
|
|
|
|
|
|
|
|
Row rowEnd0 = sheet.createRow(rowNum);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd0, new CellRangeAddress(rowNum, rowNum+1, 0, 3), "批量或致命性质量异常", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd0, new CellRangeAddress(rowNum, rowNum+1, 4, 13), detailMap.get("remark"), true, true,null);
|
|
|
|
|
|
|
|
|
|
Row rowEnd1 = sheet.createRow(rowNum+2);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 0, 6), "备注:无异常的打√,有异常的描述异常及数量比例,有数据的需要填写数据", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(rowNum+2, rowNum+3, 7, 7), "说明");
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 8, 13), "", true, true, null);
|
|
|
|
|
Row rowEnd2 = sheet.createRow(rowNum+5);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 0, 1), "检查人", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 2, 3), detailMap.get("checkManName"), true, true,null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 7, 8), "品质主管", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//处理数据
|
|
|
|
|
public static String getValue(Object object){
|
|
|
|
|
if (object==null){
|
|
|
|
|
return "";
|
|
|
|
|
}else {
|
|
|
|
|
return object.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// //处理数据
|
|
|
|
|
// public static Integer getValueNum(Object object){
|
|
|
|
|
// if (object==null){
|
|
|
|
|
// return 0;
|
|
|
|
|
// }else {
|
|
|
|
|
// return Integer.parseInt(object.toString());
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// 从resources目录获取图片的字节数组
|
|
|
|
|
private static byte[] getImageBytesFromResources(String resourceName) {
|
|
|
|
|
try (InputStream inputStream = ExcelSCXJReportMapUtil.class.getClassLoader().getResourceAsStream(resourceName);
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
|
|
|
|
|
|
|
|
|
if (inputStream == null) {
|
|
|
|
|
throw new RuntimeException("找不到资源: " + resourceName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IOUtils.copy(inputStream, byteArrayOutputStream);
|
|
|
|
|
return byteArrayOutputStream.toByteArray();
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("读取资源时出错: " + resourceName, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//报告大标题样式-1
|
|
|
|
|
public static CellStyle getTitelStyle(Workbook workbook){
|
|
|
|
|
Font font = workbook.createFont();
|
|
|
|
|
// 设置字体为加粗
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 设置字体大小(例如,设置为16)
|
|
|
|
|
font.setFontHeightInPoints((short) 24);
|
|
|
|
|
CellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);//设置水平居中
|
|
|
|
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
|
|
|
|
|
cellStyle.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
return cellStyle;
|
|
|
|
|
}
|
|
|
|
|
public static CellStyle getRowStyle(Sheet sheet){
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
// 设置水平居中
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
// 设置垂直居中
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
Font font = sheet.getWorkbook().createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 设置字体大小(例如,设置为14)
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
public static CellStyle getDataStyle(Sheet sheet){
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
Font font = sheet.getWorkbook().createFont();
|
|
|
|
|
|
|
|
|
|
// 设置字体大小(例如,设置为14)
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
style.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
// 设置水平居中
|
|
|
|
|
style.setAlignment(HorizontalAlignment.LEFT);
|
|
|
|
|
// 设置垂直居中
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
return style;
|
|
|
|
|
}
|
|
|
|
|
private static void mergeAndStyleCells(Sheet sheet, CellRangeAddress cellRangeAddress, String cellValue) {
|
|
|
|
|
// 合并单元格
|
|
|
|
|
sheet.addMergedRegion(cellRangeAddress);
|
|
|
|
|
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体为加粗
|
|
|
|
|
Font font = sheet.getWorkbook().createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 设置字体大小(例如,设置为14)
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
// 设置水平居中
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
// 设置垂直居中
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
style.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
Row row = sheet.getRow(cellRangeAddress.getFirstRow());
|
|
|
|
|
if (row == null) {
|
|
|
|
|
row = sheet.createRow(cellRangeAddress.getFirstRow());
|
|
|
|
|
}
|
|
|
|
|
Cell cell = row.getCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row.createCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(cellValue); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(style); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
private static void mergeAndStyleCellsNoBorder(Sheet sheet, CellRangeAddress cellRangeAddress, String cellValue) {
|
|
|
|
|
// 合并单元格
|
|
|
|
|
sheet.addMergedRegion(cellRangeAddress);
|
|
|
|
|
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体为加粗
|
|
|
|
|
Font font = sheet.getWorkbook().createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 设置字体大小(例如,设置为14)
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置水平居中
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
// 设置垂直居中
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
style.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
Row row = sheet.getRow(cellRangeAddress.getFirstRow());
|
|
|
|
|
if (row == null) {
|
|
|
|
|
row = sheet.createRow(cellRangeAddress.getFirstRow());
|
|
|
|
|
}
|
|
|
|
|
Cell cell = row.getCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row.createCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(cellValue); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(style); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
private static void mergeAndStyleCells2(Sheet sheet,Row row, CellRangeAddress cellRangeAddress, String cellValue, boolean centered, boolean hasBackground, IndexedColors backgroundColor) {
|
|
|
|
|
// 合并单元格
|
|
|
|
|
sheet.addMergedRegion(cellRangeAddress);
|
|
|
|
|
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
if (hasBackground && backgroundColor != null) {
|
|
|
|
|
style.setFillForegroundColor(backgroundColor.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
Cell cell = row.getCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row.createCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(cellValue); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(style); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
private static void mergeAndStyleCellsNoBorder(Sheet sheet,Row row, CellRangeAddress cellRangeAddress, String cellValue, boolean centered, boolean hasBackground, IndexedColors backgroundColor) {
|
|
|
|
|
// 合并单元格
|
|
|
|
|
sheet.addMergedRegion(cellRangeAddress);
|
|
|
|
|
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
if (hasBackground && backgroundColor != null) {
|
|
|
|
|
style.setFillForegroundColor(backgroundColor.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
Cell cell = row.getCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row.createCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(cellValue); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(style); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
//白班
|
|
|
|
|
private static void drawDatatimeDay(Sheet sheet){
|
|
|
|
|
Row dataTR1 = sheet.getRow(9);
|
|
|
|
|
if (dataTR1 == null) {dataTR1 = sheet.createRow(9);}
|
|
|
|
|
Cell dataTC1 = dataTR1.createCell(2);
|
|
|
|
|
dataTC1.setCellValue("8:00-9:00");
|
|
|
|
|
dataTC1.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC2 = dataTR1.createCell(3);
|
|
|
|
|
dataTC2.setCellValue("9:00-10:00");
|
|
|
|
|
dataTC2.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC3 = dataTR1.createCell(4);
|
|
|
|
|
dataTC3.setCellValue("10:00-11:00");
|
|
|
|
|
dataTC3.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC4 = dataTR1.createCell(5);
|
|
|
|
|
dataTC4.setCellValue("11:00-12:00");
|
|
|
|
|
dataTC4.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC5 = dataTR1.createCell(6);
|
|
|
|
|
dataTC5.setCellValue("13:30-14:30");
|
|
|
|
|
dataTC5.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC6 = dataTR1.createCell(7);
|
|
|
|
|
dataTC6.setCellValue("14:30-15:30");
|
|
|
|
|
dataTC6.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC7 = dataTR1.createCell(8);
|
|
|
|
|
dataTC7.setCellValue("15:30-16:30");
|
|
|
|
|
dataTC7.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC8 = dataTR1.createCell(9);
|
|
|
|
|
dataTC8.setCellValue("16:30-17:30");
|
|
|
|
|
dataTC8.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC9 = dataTR1.createCell(10);
|
|
|
|
|
dataTC9.setCellValue("12:30-13:30");
|
|
|
|
|
dataTC9.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC10 = dataTR1.createCell(11);
|
|
|
|
|
dataTC10.setCellValue("18:00-19:00");
|
|
|
|
|
dataTC10.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC11 = dataTR1.createCell(12);
|
|
|
|
|
dataTC11.setCellValue("19:00-20:00");
|
|
|
|
|
dataTC11.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC12 = dataTR1.createCell(13);
|
|
|
|
|
dataTC12.setCellValue("20:00-21:00");
|
|
|
|
|
dataTC12.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
}
|
|
|
|
|
//白班
|
|
|
|
|
private static void drawDatatimeNight(Sheet sheet){
|
|
|
|
|
Row dataTR1 = sheet.getRow(9);
|
|
|
|
|
if (dataTR1 == null) {dataTR1 = sheet.createRow(9);}
|
|
|
|
|
Cell dataTC1 = dataTR1.createCell(2);
|
|
|
|
|
dataTC1.setCellValue("20:30-21:30");
|
|
|
|
|
dataTC1.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC2 = dataTR1.createCell(3);
|
|
|
|
|
dataTC2.setCellValue("21:30-22:30");
|
|
|
|
|
dataTC2.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC3 = dataTR1.createCell(4);
|
|
|
|
|
dataTC3.setCellValue("22:30-23:30");
|
|
|
|
|
dataTC3.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC4 = dataTR1.createCell(5);
|
|
|
|
|
dataTC4.setCellValue("23:30-1:30");
|
|
|
|
|
dataTC4.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC5 = dataTR1.createCell(6);
|
|
|
|
|
dataTC5.setCellValue("1:30-2:30");
|
|
|
|
|
dataTC5.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC6 = dataTR1.createCell(7);
|
|
|
|
|
dataTC6.setCellValue("2:30-3:30");
|
|
|
|
|
dataTC6.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC7 = dataTR1.createCell(8);
|
|
|
|
|
dataTC7.setCellValue("3:30-4:30");
|
|
|
|
|
dataTC7.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC8 = dataTR1.createCell(9);
|
|
|
|
|
dataTC8.setCellValue("4:30-5:30");
|
|
|
|
|
dataTC8.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC9 = dataTR1.createCell(10);
|
|
|
|
|
dataTC9.setCellValue("5:30-6:30");
|
|
|
|
|
dataTC9.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC10 = dataTR1.createCell(11);
|
|
|
|
|
dataTC10.setCellValue("6:30-7:30");
|
|
|
|
|
dataTC10.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC11 = dataTR1.createCell(12);
|
|
|
|
|
dataTC11.setCellValue("7:30-8:30");
|
|
|
|
|
dataTC11.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
Cell dataTC12 = dataTR1.createCell(13);
|
|
|
|
|
}
|
|
|
|
|
}
|