修改合并相同内容的单元格
This commit is contained in:
parent
3da84843b6
commit
84ea2ba899
@ -80,6 +80,7 @@ public class DatasetQuestionController {
|
||||
row.createCell(0).setCellValue("问题内容");
|
||||
row.createCell(1).setCellValue("标注内容");
|
||||
count++;
|
||||
List<Integer> id = new ArrayList<>();
|
||||
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
||||
List<DatasetQuestionRespVO> list = datasetQuestionService.getDatasetQuestionPage(pageReqVO).getList();
|
||||
for (DatasetQuestionRespVO item : list){
|
||||
@ -90,37 +91,23 @@ public class DatasetQuestionController {
|
||||
row = sheet.createRow(count);
|
||||
row.createCell(0).setCellValue(question);
|
||||
row.createCell(1).setCellValue("");
|
||||
id.add(count);
|
||||
count++;
|
||||
}
|
||||
for (String s : collect){
|
||||
row = sheet.createRow(count);
|
||||
row.createCell(0).setCellValue(question);
|
||||
row.createCell(1).setCellValue(s);
|
||||
count++;
|
||||
}else {
|
||||
for (String s : collect) {
|
||||
row = sheet.createRow(count);
|
||||
row.createCell(0).setCellValue(question);
|
||||
row.createCell(1).setCellValue(s);
|
||||
count++;
|
||||
}
|
||||
id.add(count-1);
|
||||
}
|
||||
}
|
||||
//合并相同内容的单元格
|
||||
int startRow = 0;
|
||||
// 上一次的值
|
||||
String lastValue = null;
|
||||
for (int i = 0; i < count; i++) {
|
||||
HSSFRow row1 = sheet.getRow(i);
|
||||
String question = row1.getCell(0).getStringCellValue();
|
||||
if (!question.equals(lastValue)) {
|
||||
// 如果当前值与上一次值不同,并且不是第一行,则合并之前的区域
|
||||
// 确保合并区域至少有两个单元格
|
||||
if (i > 0 && startRow != i - 1) {
|
||||
sheet.addMergedRegion(new CellRangeAddress(startRow, i - 1, 0, 0));
|
||||
}
|
||||
startRow = i;
|
||||
for (int i = 0; i < id.size() - 1; i++){
|
||||
if (id.get(i+1)-id.get(i)>1) {
|
||||
sheet.addMergedRegion(new CellRangeAddress(id.get(i)+1, id.get(i + 1), 0, 0));
|
||||
}
|
||||
|
||||
lastValue = question;
|
||||
}
|
||||
|
||||
// 处理最后一段合并区域,确保最后一段合并区域至少有两个单元格
|
||||
if (count > 0 && startRow != count - 1) {
|
||||
sheet.addMergedRegion(new CellRangeAddress(startRow, count - 1, 0, 0));
|
||||
}
|
||||
// 导出 Excel
|
||||
try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user