|
@@ -4480,8 +4480,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
|
|
|
@Override
|
|
|
public ResponseEntity<Resource> exportTree(Long contractId, HttpServletResponse response) throws IOException, InvalidFormatException {
|
|
|
- String templatePath = "/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/gcdcTemplate.xlsx";
|
|
|
- //String templatePath = "C:\\upload\\excel\\gcdc.xlsx";
|
|
|
+ //String templatePath = "/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/gcdcTemplate.xlsx";
|
|
|
+ String templatePath = "C:\\upload\\excel\\gcdc.xlsx";
|
|
|
// 查询数据
|
|
|
String sql = "select *,CONCAT(ancestors_p_id, ',', p_key_id) AS ancestors_p_id from m_wbs_tree_contract where contract_id = " + contractId +
|
|
|
" and is_deleted = 0 and type = 1 and node_type != 6 and p_id != 0";
|
|
@@ -4552,8 +4552,8 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
}
|
|
|
|
|
|
//保存文件到本地(本地测试放开,正式环境不需要)
|
|
|
- //String outputPath = "C:\\upload\\excel\\111.xlsx";
|
|
|
- //saveWorkbookToFile(workbook, outputPath);
|
|
|
+ String outputPath = "C:\\upload\\excel\\111.xlsx";
|
|
|
+ saveWorkbookToFile(workbook, outputPath);
|
|
|
|
|
|
// 同时返回给浏览器下载
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
@@ -5067,8 +5067,11 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
mergeMap.get(col).add(new CellRangeAddress(rowNum, rowNum, col, col));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// 应用合并单元格 - 添加居中样式参数
|
|
|
private void applyMergedRegions(Sheet sheet, Map<Integer, List<CellRangeAddress>> mergeMap, CellStyle centerStyle) {
|
|
|
+
|
|
|
// 使用Set来记录已经合并的区域,避免重复合并
|
|
|
Set<String> mergedRegions = new HashSet<>();
|
|
|
|
|
@@ -5174,7 +5177,7 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
} else {
|
|
|
// 值不同,合并之前的区域
|
|
|
if (mergeStart < mergeEnd) {
|
|
|
- mergeSingleColumnIfNotExists(sheet, col, mergeStart, mergeEnd, centerStyle, mergedRegions);
|
|
|
+ mergeSingleColumnIfNotExists(sheet, col, mergeStart, mergeEnd, centerStyle, mergedRegions,false);
|
|
|
}
|
|
|
// 开始新的合并区域
|
|
|
mergeStart = rowNum;
|
|
@@ -5185,26 +5188,27 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
|
|
|
// 合并最后一段区域
|
|
|
if (mergeStart < mergeEnd) {
|
|
|
- mergeSingleColumnIfNotExists(sheet, col, mergeStart, mergeEnd, centerStyle, mergedRegions);
|
|
|
+ mergeSingleColumnIfNotExists(sheet, col, mergeStart, mergeEnd, centerStyle, mergedRegions,false);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 合并名称列和对应的划分编码列(检查是否已存在)
|
|
|
+ // 在合并名称列和对应编码列时,需要清空被合并单元格的值,只保留第一个单元格的值
|
|
|
private void mergeNameAndCodeColumns(Sheet sheet, int nameCol, int startRow, int endRow, CellStyle centerStyle, Set<String> mergedRegions) {
|
|
|
// 合并名称列
|
|
|
- mergeSingleColumnIfNotExists(sheet, nameCol, startRow, endRow, centerStyle, mergedRegions);
|
|
|
+ mergeSingleColumnIfNotExists(sheet, nameCol, startRow, endRow, centerStyle, mergedRegions, true);
|
|
|
|
|
|
// 合并对应的划分编码列
|
|
|
int codeCol = getCorrespondingCodeColumn(nameCol);
|
|
|
if (codeCol != -1) {
|
|
|
- mergeSingleColumnIfNotExists(sheet, codeCol, startRow, endRow, centerStyle, mergedRegions);
|
|
|
+ mergeSingleColumnIfNotExists(sheet, codeCol, startRow, endRow, centerStyle, mergedRegions, true);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 合并单列并设置样式(检查是否已存在)
|
|
|
- private void mergeSingleColumnIfNotExists(Sheet sheet, int col, int startRow, int endRow, CellStyle style, Set<String> mergedRegions) {
|
|
|
+ // 合并单列并设置样式(添加clearValues参数来控制是否清空被合并单元格的值)
|
|
|
+ private void mergeSingleColumnIfNotExists(Sheet sheet, int col, int startRow, int endRow, CellStyle style,
|
|
|
+ Set<String> mergedRegions, boolean clearValues) {
|
|
|
try {
|
|
|
String regionKey = col + ":" + startRow + ":" + endRow;
|
|
|
|
|
@@ -5224,6 +5228,30 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
}
|
|
|
|
|
|
if (!alreadyExists) {
|
|
|
+ // 如果需要清空值,保留第一个单元格的值,清空其他单元格的值
|
|
|
+ if (clearValues && startRow < endRow) {
|
|
|
+ // 获取第一个单元格的值
|
|
|
+ Row firstRow = sheet.getRow(startRow);
|
|
|
+ Cell firstCell = firstRow != null ? firstRow.getCell(col) : null;
|
|
|
+ String firstValue = firstCell != null ? getCellStringValue(firstCell) : "";
|
|
|
+
|
|
|
+ // 清空被合并区域中其他单元格的值
|
|
|
+ for (int rowNum = startRow + 1; rowNum <= endRow; rowNum++) {
|
|
|
+ Row row = sheet.getRow(rowNum);
|
|
|
+ if (row != null) {
|
|
|
+ Cell cell = row.getCell(col);
|
|
|
+ if (cell != null) {
|
|
|
+ cell.setCellValue(""); // 清空值
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保第一个单元格有正确的值(防止被意外清空)
|
|
|
+ if (firstCell != null) {
|
|
|
+ firstCell.setCellValue(firstValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
sheet.addMergedRegion(mergedRegion);
|
|
|
setMergedRegionStyle(sheet, mergedRegion, style);
|
|
|
mergedRegions.add(regionKey);
|
|
@@ -5232,7 +5260,6 @@ public class WbsTreeContractServiceImpl extends BaseServiceImpl<WbsTreeContractM
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
// 判断是否是名称列
|