数据集单个导出

This commit is contained in:
sunxiqing 2024-12-30 15:35:03 +08:00
parent c823592ba0
commit c1c54b0504

View File

@ -1,10 +1,17 @@
package cn.iocoder.yudao.module.llm.controller.admin.dataset;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetAnswerRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetQuestionDO;
import cn.iocoder.yudao.module.llm.service.dataset.DatasetQuestionService;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
@ -18,6 +25,7 @@ import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@ -56,6 +64,76 @@ public class DatasetQuestionController {
return success(pageResult);
}
@GetMapping("/export-excel")
@Operation(summary = "导出数据集数据文件 Excel")
@PreAuthorize("@ss.hasPermission('llm:dataset-files:export')")
@ApiAccessLog(operateType = EXPORT)
public void exportDatasetFilesExcel(@Valid DatasetQuestionPageReqVO pageReqVO,
HttpServletResponse response) throws IOException {
HSSFWorkbook template = new HSSFWorkbook();
HSSFSheet sheet = template.createSheet();
// 创建样式并设置垂直居中
HSSFCellStyle cellStyle = template.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
int count = 0;
HSSFRow row = sheet.createRow(count);
row.createCell(0).setCellValue("问题内容");
row.createCell(1).setCellValue("标注内容");
count++;
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<DatasetQuestionRespVO> list = datasetQuestionService.getDatasetQuestionPage(pageReqVO).getList();
for (DatasetQuestionRespVO item : list){
String question = item.getQuestion();
List<DatasetAnswerRespVO> datasetAnswerRespVO = item.getDatasetAnswerRespVO();
List<String> collect = datasetAnswerRespVO.stream().map(DatasetAnswerRespVO::getAnswer).collect(Collectors.toList());
if (collect.size() == 0){
row = sheet.createRow(count);
row.createCell(0).setCellValue(question);
row.createCell(1).setCellValue("");
count++;
}
for (String s : collect){
row = sheet.createRow(count);
row.createCell(0).setCellValue(question);
row.createCell(1).setCellValue(s);
count++;
}
}
//合并相同内容的单元格
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;
}
lastValue = question;
}
// 处理最后一段合并区域确保最后一段合并区域至少有两个单元格
if (count > 0 && startRow != count - 1) {
sheet.addMergedRegion(new CellRangeAddress(startRow, count - 1, 0, 0));
}
// 导出 Excel
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
template.write(response.getOutputStream());
response.getOutputStream().close();
template.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/* @PutMapping("/update")
@Operation(summary = "更新数据集数据问题")
@PreAuthorize("@ss.hasPermission('llm:dataset-question:update')")
@ -104,4 +182,4 @@ public class DatasetQuestionController {
return success(datasetQuestionService.createDatasetQuestion(createReqVO));
}*/
}
}