feat: 丰富一下导出excel的内容和样式

This commit is contained in:
liu.zixi
2025-12-11 19:05:25 +08:00
parent e29aa5b00d
commit 4fba985ddd

View File

@@ -39,9 +39,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jetbrains.annotations.NotNull;
@@ -933,18 +931,89 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
// 写入Excel文件
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("AI会话数据");
// 样式
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
headerStyle.setFont(headerFont);
headerStyle.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
CellStyle dataStyle = workbook.createCellStyle();
dataStyle.setBorderTop(BorderStyle.THIN);
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setAlignment(HorizontalAlignment.CENTER);
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 增加一个汇总Sheet
Sheet summarySheet = workbook.createSheet("汇总");
Row summaryHeaderRow = summarySheet.createRow(0);
Cell monthHeaderCell = summaryHeaderRow.createCell(0);
monthHeaderCell.setCellValue("月份");
monthHeaderCell.setCellStyle(headerStyle);
Cell sessionCountHeaderCell = summaryHeaderRow.createCell(1);
sessionCountHeaderCell.setCellValue("会话总次数");
sessionCountHeaderCell.setCellStyle(headerStyle);
Cell userCountHeaderCell = summaryHeaderRow.createCell(2);
userCountHeaderCell.setCellValue("会话总人数");
userCountHeaderCell.setCellStyle(headerStyle);
Row summaryDataRow = summarySheet.createRow(1);
Cell monthDataCell = summaryDataRow.createCell(0);
monthDataCell.setCellValue(startTime.format(DateTimeFormatter.ofPattern("yyyy年MM月")));
monthDataCell.setCellStyle(dataStyle);
Cell sessionCountDataCell = summaryDataRow.createCell(1);
sessionCountDataCell.setCellValue(conversations.size());
sessionCountDataCell.setCellStyle(dataStyle);
Cell userCountDataCell = summaryDataRow.createCell(2);
userCountDataCell.setCellValue(conversations.stream().map(CaseAiConversations::getConversationUser).distinct().count());
userCountDataCell.setCellStyle(dataStyle);
// 列宽
for (int i = 0; i < 3; i++) {
summarySheet.autoSizeColumn(i);
summarySheet.setColumnWidth(i, summarySheet.getColumnWidth(i) + 500);
}
Sheet dataSheet = workbook.createSheet("AI会话数据");
// 标题行
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("会话ID");
headerRow.createCell(1).setCellValue("会话名称");
headerRow.createCell(2).setCellValue("用户");
headerRow.createCell(3).setCellValue("提问");
headerRow.createCell(4).setCellValue("回答");
headerRow.createCell(5).setCellValue("开始时间");
headerRow.createCell(6).setCellValue("问答时长(秒)");
headerRow.createCell(7).setCellValue("消息状态");
headerRow.createCell(8).setCellValue("错误信息");
Row dataHeaderRow = dataSheet.createRow(0);
Cell conversationIdHeaderCell = dataHeaderRow.createCell(0);
conversationIdHeaderCell.setCellValue("会话ID");
conversationIdHeaderCell.setCellStyle(headerStyle);
Cell conversationNameHeaderCell = dataHeaderRow.createCell(1);
conversationNameHeaderCell.setCellValue("会话名称");
conversationNameHeaderCell.setCellStyle(headerStyle);
Cell userHeaderCell = dataHeaderRow.createCell(2);
userHeaderCell.setCellValue("用户ID");
userHeaderCell.setCellStyle(headerStyle);
Cell queryHeaderCell = dataHeaderRow.createCell(3);
queryHeaderCell.setCellValue("提问");
queryHeaderCell.setCellStyle(headerStyle);
Cell answerHeaderCell = dataHeaderRow.createCell(4);
answerHeaderCell.setCellValue("回答");
answerHeaderCell.setCellStyle(headerStyle);
Cell startTimeHeaderCell = dataHeaderRow.createCell(5);
startTimeHeaderCell.setCellValue("开始时间");
startTimeHeaderCell.setCellStyle(headerStyle);
Cell durationHeaderCell = dataHeaderRow.createCell(6);
durationHeaderCell.setCellValue("问答时长(秒)");
durationHeaderCell.setCellStyle(headerStyle);
Cell statusHeaderCell = dataHeaderRow.createCell(7);
statusHeaderCell.setCellValue("消息状态");
statusHeaderCell.setCellStyle(headerStyle);
Cell errorMsgHeaderCell = dataHeaderRow.createCell(8);
errorMsgHeaderCell.setCellValue("错误信息");
errorMsgHeaderCell.setCellStyle(headerStyle);
// 内容行
if (!excelDataList.isEmpty()) {
@@ -958,48 +1027,78 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
// 遍历每个消息
for (CaseAiMessageVo message : messages) {
Row row = sheet.createRow(rowNum++);
Row row = dataSheet.createRow(rowNum++);
// 填充每行数据
row.createCell(0).setCellValue(excelData.getConversationId());
row.createCell(1).setCellValue(excelData.getConversationName());
row.createCell(2).setCellValue(excelData.getUser());
row.createCell(3).setCellValue(message.getQuery() != null ? message.getQuery() : "");
row.createCell(4).setCellValue(message.getAnswer() != null ? message.getAnswer() : "");
Cell conversationIdCell = row.createCell(0);
conversationIdCell.setCellValue(excelData.getConversationId());
conversationIdCell.setCellStyle(dataStyle);
Cell conversationNameCell = row.createCell(1);
conversationNameCell.setCellValue(excelData.getConversationName());
conversationNameCell.setCellStyle(dataStyle);
Cell userCell = row.createCell(2);
userCell.setCellValue(excelData.getUser());
userCell.setCellStyle(dataStyle);
Cell queryCell = row.createCell(3);
queryCell.setCellValue(message.getQuery() != null ? message.getQuery() : "");
queryCell.setCellStyle(dataStyle);
Cell answerCell = row.createCell(4);
answerCell.setCellValue(message.getAnswer() != null ? message.getAnswer() : "");
answerCell.setCellStyle(dataStyle);
Cell startTimeCell = row.createCell(5);
LocalDateTime messageStartTime = message.getStartTime();
if (messageStartTime != null) {
String startTimeStr = messageStartTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
row.createCell(5).setCellValue(startTimeStr);
startTimeCell.setCellValue(startTimeStr);
} else {
row.createCell(5).setCellValue("");
startTimeCell.setCellValue("");
}
row.createCell(6).setCellValue(message.getDurationSeconds() != null ? message.getDurationSeconds() : 0);
startTimeCell.setCellStyle(dataStyle);
Cell durationCell = row.createCell(6);
durationCell.setCellValue(message.getDurationSeconds() != null ? message.getDurationSeconds() : 0);
durationCell.setCellStyle(dataStyle);
Cell statusCell = row.createCell(7);
if (message.getStatus() != null) {
int status = message.getStatus();
CaseAiChatErrCodeEnum errCodeEnum = CaseAiChatErrCodeEnum.getByCode(status);
row.createCell(7).setCellValue(errCodeEnum.getLabel());
statusCell.setCellValue(errCodeEnum.getLabel());
}
statusCell.setCellStyle(dataStyle);
Cell errorMsgCell = row.createCell(8);
if (StringUtils.isNotBlank(message.getErrorMsg())) {
row.createCell(8).setCellValue(message.getErrorMsg());
errorMsgCell.setCellValue(message.getErrorMsg());
}
errorMsgCell.setCellStyle(dataStyle);
}
// 合并单元格会话ID、会话名称、用户三列
// 参数说明:起始行号,结束行号,起始列号,结束列号
if (rowNum > startRow + 1) { // 只有当有多行时才合并
sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 1, 1));
sheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 2, 2));
dataSheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 0, 0));
dataSheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 1, 1));
dataSheet.addMergedRegion(new CellRangeAddress(startRow, rowNum - 1, 2, 2));
}
} else {
// 如果没有消息,则仍然创建一行显示基本信息
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(excelData.getConversationId());
row.createCell(1).setCellValue(excelData.getConversationName());
row.createCell(2).setCellValue(excelData.getUser());
Row row = dataSheet.createRow(rowNum++);
Cell conversationIdCell = row.createCell(0);
conversationIdCell.setCellValue(excelData.getConversationId());
conversationIdCell.setCellStyle(dataStyle);
Cell conversationNameCell = row.createCell(1);
conversationNameCell.setCellValue(excelData.getConversationName());
conversationNameCell.setCellStyle(dataStyle);
Cell userCell = row.createCell(2);
userCell.setCellValue(excelData.getUser());
userCell.setCellStyle(dataStyle);
}
}
}
// 列宽
for (int i = 0; i < 9; i++) {
summarySheet.autoSizeColumn(i);
summarySheet.setColumnWidth(i, summarySheet.getColumnWidth(i) + 500);
}
return workbook;
}