|
|
|
@ -25,240 +25,240 @@ import java.util.Map;
|
|
|
|
|
* @author OP
|
|
|
|
|
*/
|
|
|
|
|
public class ExcelReportMapUtil {
|
|
|
|
|
//下载
|
|
|
|
|
public static <T> SXSSFWorkbook initWorkbook(String sheetName , String title, List<ExcelCol> excelCol , List<T> data, Map<String,String> detailMap) throws IOException {
|
|
|
|
|
SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
|
|
int colSize = excelCol.size();
|
|
|
|
|
//下载
|
|
|
|
|
public static <T> SXSSFWorkbook initWorkbook(String sheetName , String title, List<ExcelCol> excelCol , List<T> data, Map<String,String> detailMap) 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);
|
|
|
|
|
}
|
|
|
|
|
//创建Sheet(工作簿)
|
|
|
|
|
Sheet sheet = null;
|
|
|
|
|
if (!StringUtils.hasText(sheetName)){
|
|
|
|
|
sheet = workbook.createSheet();
|
|
|
|
|
}else{
|
|
|
|
|
sheet = workbook.createSheet(sheetName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从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, 11));
|
|
|
|
|
// 获取合并后的单元格的第一个单元格(即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的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(2, 3, 2, 8), detailMap.get("title"));
|
|
|
|
|
// 合并J3到L4的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(2, 3, 9, 12), "编码:"+detailMap.get("fileNo"));
|
|
|
|
|
/**左右表格1**/
|
|
|
|
|
//画边框
|
|
|
|
|
for(int r5=4;r5<13;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=13;d5<(13+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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 从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, 11));
|
|
|
|
|
// 获取合并后的单元格的第一个单元格(即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的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(2, 3, 2, 8), detailMap.get("title"));
|
|
|
|
|
// 合并J3到L4的单元格,并设置样式
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(2, 3, 9, 12), "编码:"+detailMap.get("fileNo"));
|
|
|
|
|
/**左右表格1**/
|
|
|
|
|
//画边框
|
|
|
|
|
for(int r5=4;r5<13;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=13;d5<(13+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("productGroupName"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 4, 5), "生产批号", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 6, 8), detailMap.get("incomeBatchNo"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 9, 10), "报告编号", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 11, 13), detailMap.get("checkNo"), 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("materialName"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 4, 5), "物料编码", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 6, 8), detailMap.get("materialCode"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 9, 10), "供应单位", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 11, 13), detailMap.get("supplierName"), true, false, null);
|
|
|
|
|
Row row9 = sheet.getRow(8);
|
|
|
|
|
// 合并A7到B8的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 0, 1), "供应编码", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 2, 3), detailMap.get("supplierCode"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 4, 5), "来料数量", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9,6, 8), detailMap.get("quality"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 9, 10), "单位", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 11, 13), detailMap.get("unit"), true, false, null);
|
|
|
|
|
Row row10 = sheet.getRow(10);
|
|
|
|
|
// 合并A7到B8的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 0, 1), "物料来源", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 2, 3), detailMap.get("checkType"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 4, 5), "检验依据", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11,6, 8), detailMap.get("standardNo"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 9, 10), "报检日期", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 11, 13), detailMap.get("incomeTime"), true, false, null);
|
|
|
|
|
/**上下表格2**/
|
|
|
|
|
Row row12 = sheet.getRow(12);
|
|
|
|
|
if (row12 == null) {row12 = sheet.createRow(12);}
|
|
|
|
|
Cell cellA13 = row12.createCell(0);
|
|
|
|
|
cellA13.setCellValue("序号");
|
|
|
|
|
cellA13.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 1, 4), "检验项目");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 5, 8), "标准要求");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 9, 10), "实测结果");
|
|
|
|
|
Cell cellL13 = row12.createCell(11);
|
|
|
|
|
cellL13.setCellValue("判定");
|
|
|
|
|
cellL13.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 12, 13), "附件");
|
|
|
|
|
//将data中的值填充到excel
|
|
|
|
|
int rowNum = 13;
|
|
|
|
|
if(!CollectionUtils.isEmpty(data)){
|
|
|
|
|
Iterator<T> iterator = data.iterator();
|
|
|
|
|
//遍历数据
|
|
|
|
|
for (;iterator.hasNext();){
|
|
|
|
|
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("productGroupName"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 4, 5), "生产批号", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 6, 8), detailMap.get("incomeBatchNo"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 9, 10), "报告编号", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row5, new CellRangeAddress(4, 5, 11, 13), detailMap.get("checkNo"), 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("materialName"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 4, 5), "物料编码", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 6, 8), detailMap.get("materialCode"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 9, 10), "供应单位", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row7, new CellRangeAddress(6, 7, 11, 13), detailMap.get("supplierName"), true, false, null);
|
|
|
|
|
Row row9 = sheet.getRow(8);
|
|
|
|
|
// 合并A7到B8的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 0, 1), "供应编码", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 2, 3), detailMap.get("supplierCode"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 4, 5), "来料数量", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9,6, 8), detailMap.get("quality"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 9, 10), "单位", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row9, new CellRangeAddress(8, 9, 11, 13), detailMap.get("unit"), true, false, null);
|
|
|
|
|
Row row10 = sheet.getRow(10);
|
|
|
|
|
// 合并A7到B8的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 0, 1), "物料来源", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并C5到D6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 2, 3), detailMap.get("checkType"), true, false, null);
|
|
|
|
|
// 合并E5到F6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 4, 5), "检验依据", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并G5到I6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11,6, 8), detailMap.get("standardNo"), true, false, null);
|
|
|
|
|
// 合并J5到K6的单元格,并设置样式和内容
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 9, 10), "报检日期", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
// 合并L5到N6的单元格,并设置样式(无背景色,只有边框和内容居中)
|
|
|
|
|
mergeAndStyleCells2(sheet,row10, new CellRangeAddress(10, 11, 11, 13), detailMap.get("incomeTime"), true, false, null);
|
|
|
|
|
/**上下表格2**/
|
|
|
|
|
Row row12 = sheet.getRow(12);
|
|
|
|
|
if (row12 == null) {row12 = sheet.createRow(12);}
|
|
|
|
|
Cell cellA13 = row12.createCell(0);
|
|
|
|
|
cellA13.setCellValue("序号");
|
|
|
|
|
cellA13.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 1, 4), "检验项目");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 5, 8), "标准要求");
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 9, 10), "实测结果");
|
|
|
|
|
Cell cellL13 = row12.createCell(11);
|
|
|
|
|
cellL13.setCellValue("判定");
|
|
|
|
|
cellL13.setCellStyle(getRowStyle(sheet));
|
|
|
|
|
mergeAndStyleCells(sheet, new CellRangeAddress(12, 12, 12, 13), "附件");
|
|
|
|
|
//将data中的值填充到excel
|
|
|
|
|
int rowNum = 13;
|
|
|
|
|
if(!CollectionUtils.isEmpty(data)){
|
|
|
|
|
Iterator<T> iterator = data.iterator();
|
|
|
|
|
//遍历数据
|
|
|
|
|
for (;iterator.hasNext();){
|
|
|
|
|
|
|
|
|
|
T obj = iterator.next();//获取当前行对应的数据
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(obj));
|
|
|
|
|
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));
|
|
|
|
|
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(rowNum-12);
|
|
|
|
|
cellData0.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData0 = dataRow.getCell(0);
|
|
|
|
|
cellData0.setCellValue(rowNum-12);
|
|
|
|
|
cellData0.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
|
|
|
|
|
Cell cellData1 = dataRow.getCell(1);
|
|
|
|
|
cellData1.setCellValue(getValue(jsonObject.get(excelCol.get(0).getField())));
|
|
|
|
|
cellData1.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData1 = dataRow.getCell(1);
|
|
|
|
|
cellData1.setCellValue(getValue(jsonObject.get(excelCol.get(0).getField())));
|
|
|
|
|
cellData1.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
|
|
|
|
|
Cell cellData2 = dataRow.getCell(5);
|
|
|
|
|
cellData2.setCellValue(getValue(jsonObject.get(excelCol.get(1).getField())));
|
|
|
|
|
cellData2.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 5, 8));
|
|
|
|
|
Cell cellData2 = dataRow.getCell(5);
|
|
|
|
|
cellData2.setCellValue(getValue(jsonObject.get(excelCol.get(1).getField())));
|
|
|
|
|
cellData2.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 5, 8));
|
|
|
|
|
|
|
|
|
|
Cell cellData3 = dataRow.getCell(9);
|
|
|
|
|
cellData3.setCellValue(getValue(jsonObject.get(excelCol.get(2).getField())));
|
|
|
|
|
cellData3.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 9, 10));
|
|
|
|
|
Cell cellData3 = dataRow.getCell(9);
|
|
|
|
|
cellData3.setCellValue(getValue(jsonObject.get(excelCol.get(3).getField())));
|
|
|
|
|
cellData3.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 9, 10));
|
|
|
|
|
|
|
|
|
|
Cell cellData4 = dataRow.getCell(11);
|
|
|
|
|
cellData4.setCellValue(getValue(jsonObject.get(excelCol.get(3).getField())));
|
|
|
|
|
cellData4.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
Cell cellData4 = dataRow.getCell(11);
|
|
|
|
|
cellData4.setCellValue(getValue(jsonObject.get(excelCol.get(4).getField())));
|
|
|
|
|
cellData4.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
|
|
|
|
|
Cell cellData5 = dataRow.getCell(12);
|
|
|
|
|
cellData5.setCellValue(getValue(jsonObject.get(excelCol.get(4).getField())));
|
|
|
|
|
cellData5.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 12, 13));
|
|
|
|
|
iterator.remove();
|
|
|
|
|
rowNum++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Cell cellData5 = dataRow.getCell(12);
|
|
|
|
|
cellData5.setCellValue(getValue(jsonObject.get(excelCol.get(2).getField())));
|
|
|
|
|
cellData5.setCellStyle(getDataStyle(sheet));
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 12, 13));
|
|
|
|
|
iterator.remove();
|
|
|
|
|
rowNum++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 指定要检查的列索引(例如,B列的索引是1)
|
|
|
|
|
int columnIndex = 1;
|
|
|
|
|
// 遍历所有行,从第二行开始(索引1),因为第一行可能是标题行
|
|
|
|
|
for (int rowIndex = 13; rowIndex <= sheet.getLastRowNum(); ) {
|
|
|
|
|
Row currentRow = sheet.getRow(rowIndex);
|
|
|
|
|
if (currentRow != null) {
|
|
|
|
|
Cell currentCell = currentRow.getCell(columnIndex);
|
|
|
|
|
if (currentCell != null && currentCell.getCellType() == CellType.STRING) { // 假设我们处理字符串类型的单元格
|
|
|
|
|
String currentValue = currentCell.getStringCellValue();
|
|
|
|
|
// 指定要检查的列索引(例如,B列的索引是1)
|
|
|
|
|
int columnIndex = 1;
|
|
|
|
|
// 遍历所有行,从第二行开始(索引1),因为第一行可能是标题行
|
|
|
|
|
for (int rowIndex = 13; rowIndex <= sheet.getLastRowNum(); ) {
|
|
|
|
|
Row currentRow = sheet.getRow(rowIndex);
|
|
|
|
|
if (currentRow != null) {
|
|
|
|
|
Cell currentCell = currentRow.getCell(columnIndex);
|
|
|
|
|
if (currentCell != null && currentCell.getCellType() == CellType.STRING) { // 假设我们处理字符串类型的单元格
|
|
|
|
|
String currentValue = currentCell.getStringCellValue();
|
|
|
|
|
|
|
|
|
|
// 查找连续相同值的行
|
|
|
|
|
int lastRowIndex = rowIndex;
|
|
|
|
|
while (lastRowIndex + 1 <= sheet.getLastRowNum()) {
|
|
|
|
|
Row nextRow = sheet.getRow(lastRowIndex + 1);
|
|
|
|
|
if (nextRow != null) {
|
|
|
|
|
Cell nextCell = nextRow.getCell(columnIndex);
|
|
|
|
|
if (nextCell != null && nextCell.getCellType() == CellType.STRING &&
|
|
|
|
|
nextCell.getStringCellValue().equals(currentValue)) {
|
|
|
|
|
lastRowIndex++; // 继续向下查找
|
|
|
|
|
} else {
|
|
|
|
|
break; // 找到不同值,停止查找
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break; // 没有更多行,停止查找
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 查找连续相同值的行
|
|
|
|
|
int lastRowIndex = rowIndex;
|
|
|
|
|
while (lastRowIndex + 1 <= sheet.getLastRowNum()) {
|
|
|
|
|
Row nextRow = sheet.getRow(lastRowIndex + 1);
|
|
|
|
|
if (nextRow != null) {
|
|
|
|
|
Cell nextCell = nextRow.getCell(columnIndex);
|
|
|
|
|
if (nextCell != null && nextCell.getCellType() == CellType.STRING &&
|
|
|
|
|
nextCell.getStringCellValue().equals(currentValue)) {
|
|
|
|
|
lastRowIndex++; // 继续向下查找
|
|
|
|
|
} else {
|
|
|
|
|
break; // 找到不同值,停止查找
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break; // 没有更多行,停止查找
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 合并找到的行
|
|
|
|
|
if (rowIndex != lastRowIndex) { // 只有在找到连续相同值的行时才进行合并
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowIndex, lastRowIndex, 1, 4));
|
|
|
|
|
}else{
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 4));
|
|
|
|
|
}
|
|
|
|
|
// 合并找到的行
|
|
|
|
|
if (rowIndex != lastRowIndex) { // 只有在找到连续相同值的行时才进行合并
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowIndex, lastRowIndex, 1, 4));
|
|
|
|
|
}else{
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 4));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新rowIndex以跳过已合并的行
|
|
|
|
|
rowIndex = lastRowIndex + 1; // 从下一个未合并的行开始继续遍历
|
|
|
|
|
} else {
|
|
|
|
|
// 如果当前单元格不是字符串类型,则直接跳到下一行
|
|
|
|
|
rowIndex++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果当前行为空,则直接跳到下一行
|
|
|
|
|
rowIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 更新rowIndex以跳过已合并的行
|
|
|
|
|
rowIndex = lastRowIndex + 1; // 从下一个未合并的行开始继续遍历
|
|
|
|
|
} else {
|
|
|
|
|
// 如果当前单元格不是字符串类型,则直接跳到下一行
|
|
|
|
|
rowIndex++;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果当前行为空,则直接跳到下一行
|
|
|
|
|
rowIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Row rowEnd1 = sheet.createRow(rowNum+2);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 0, 1), "综合检验结论", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 2, 3), detailMap.get("checkResult"), true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 5, 10), "备注:判定合格打“√”,不合格打“×”", 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), "", true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 5, 6), "品质主管", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 7, 8), "", true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 10, 11), "品管员", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 12, 13), detailMap.get("checkManName"), true, false, null);
|
|
|
|
|
Row rowEnd1 = sheet.createRow(rowNum+2);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 0, 1), "综合检验结论", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 2, 3), detailMap.get("checkResult"), true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd1, new CellRangeAddress(rowNum+2, rowNum+3, 5, 10), "备注:判定合格打“√”,不合格打“×”", 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), "", true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 5, 6), "品质主管", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 7, 8), "", true, false, null);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 10, 11), "品管员", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+5, rowNum+5, 12, 13), detailMap.get("checkManName"), true, false, null);
|
|
|
|
|
|
|
|
|
|
Row rowEnd3 = sheet.createRow(rowNum+4);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, rowEnd3, new CellRangeAddress(rowNum+4, rowNum+4, 0, 1), "备注", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, rowEnd3, new CellRangeAddress(rowNum+4, rowNum+4, 2, 13), detailMap.get("remark"), true, false, null);
|
|
|
|
|
Row rowEnd3 = sheet.createRow(rowNum+4);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, rowEnd3, new CellRangeAddress(rowNum+4, rowNum+4, 0, 1), "备注", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
|
mergeAndStyleCellsNoBorder(sheet, rowEnd3, new CellRangeAddress(rowNum+4, rowNum+4, 2, 13), detailMap.get("remark"), true, false, null);
|
|
|
|
|
|
|
|
|
|
// Row rowEnd3 = sheet.createRow(rowNum+8);
|
|
|
|
|
// mergeAndStyleCellsNoBorder(sheet,rowEnd3, new CellRangeAddress(rowNum+5, rowNum+5, 0, 1), "品管经理/主任", true, true, IndexedColors.GREY_25_PERCENT);
|
|
|
|
@ -266,8 +266,8 @@ public class ExcelReportMapUtil {
|
|
|
|
|
// mergeAndStyleCellsNoBorder(sheet,rowEnd2, new CellRangeAddress(rowNum+6, rowNum+6, 2, 13), detailMap.get("remark"), true, false, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// //标题样式
|
|
|
|
|
// public static CellStyle getHeaderFont(Workbook workbook){
|
|
|
|
@ -291,7 +291,7 @@ public class ExcelReportMapUtil {
|
|
|
|
|
// return cellStyle;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// //内容样式
|
|
|
|
|
// //内容样式
|
|
|
|
|
// public static CellStyle getDataFont(Workbook workbook){
|
|
|
|
|
// Font font = workbook.createFont();
|
|
|
|
|
// font.setFontHeightInPoints((short) 12);//字体大小
|
|
|
|
@ -304,15 +304,15 @@ public class ExcelReportMapUtil {
|
|
|
|
|
// return cellStyle;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//处理数据
|
|
|
|
|
public static String getValue(Object object){
|
|
|
|
|
if (object==null){
|
|
|
|
|
return "";
|
|
|
|
|
}else {
|
|
|
|
|
return object.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// //处理数据
|
|
|
|
|
//处理数据
|
|
|
|
|
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;
|
|
|
|
@ -320,163 +320,163 @@ public class ExcelReportMapUtil {
|
|
|
|
|
// return Integer.parseInt(object.toString());
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// 从resources目录获取图片的字节数组
|
|
|
|
|
private static byte[] getImageBytesFromResources(String resourceName) {
|
|
|
|
|
try (InputStream inputStream = ExcelReportMapUtil.class.getClassLoader().getResourceAsStream(resourceName);
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
|
|
|
|
// 从resources目录获取图片的字节数组
|
|
|
|
|
private static byte[] getImageBytesFromResources(String resourceName) {
|
|
|
|
|
try (InputStream inputStream = ExcelReportMapUtil.class.getClassLoader().getResourceAsStream(resourceName);
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
|
|
|
|
|
|
|
|
|
if (inputStream == null) {
|
|
|
|
|
throw new RuntimeException("找不到资源: " + resourceName);
|
|
|
|
|
}
|
|
|
|
|
if (inputStream == null) {
|
|
|
|
|
throw new RuntimeException("找不到资源: " + resourceName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IOUtils.copy(inputStream, byteArrayOutputStream);
|
|
|
|
|
return byteArrayOutputStream.toByteArray();
|
|
|
|
|
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();
|
|
|
|
|
} 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);
|
|
|
|
|
// 设置字体大小(例如,设置为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();
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体为加粗
|
|
|
|
|
Font font = sheet.getWorkbook().createFont();
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
// 设置字体大小(例如,设置为14)
|
|
|
|
|
font.setFontHeightInPoints((short) 14);
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置字体为加粗
|
|
|
|
|
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);
|
|
|
|
|
// 设置水平居中
|
|
|
|
|
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();
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
if (hasBackground && backgroundColor != null) {
|
|
|
|
|
style.setFillForegroundColor(backgroundColor.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
if (hasBackground && backgroundColor != null) {
|
|
|
|
|
style.setFillForegroundColor(backgroundColor.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
// 设置边框线
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
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);
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
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();
|
|
|
|
|
// 创建一个单元格样式
|
|
|
|
|
CellStyle style = sheet.getWorkbook().createCellStyle();
|
|
|
|
|
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
// 设置字体居中
|
|
|
|
|
if (centered) {
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
if (hasBackground && backgroundColor != null) {
|
|
|
|
|
style.setFillForegroundColor(backgroundColor.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
// 设置背景颜色(如果有)
|
|
|
|
|
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); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
// 获取合并后的单元格的第一个单元格,并设置值
|
|
|
|
|
Cell cell = row.getCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
if (cell == null) {
|
|
|
|
|
cell = row.createCell(cellRangeAddress.getFirstColumn());
|
|
|
|
|
}
|
|
|
|
|
cell.setCellValue(cellValue); // 设置单元格的值
|
|
|
|
|
cell.setCellStyle(style); // 应用样式到单元格
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|