You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
JAVA/ExcelExporter.java

59 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// src/main/java/com/llm/analysis/ExcelExporter.java
package com.llm.analysis;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class ExcelExporter {
/**
* 将词频统计结果写入XLSX文件。
* @param topWords Top N 词语列表
* @param filename 输出文件名
*/
public static void exportToXLSX(List<Map.Entry<String, Long>> topWords, String filename) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("LLM应用案例词频");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("排名");
headerRow.createCell(1).setCellValue("词语/LLM应用案例");
headerRow.createCell(2).setCellValue("词频 (总数)");
// 填充数据
int rowNum = 1;
for (int i = 0; i < topWords.size(); i++) {
Map.Entry<String, Long> entry = topWords.get(i);
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(i + 1); // 排名
row.createCell(1).setCellValue(entry.getKey()); // 词语
row.createCell(2).setCellValue(entry.getValue()); // 词频
}
// 自动调整列宽
for (int i = 0; i < 3; i++) {
sheet.autoSizeColumn(i);
}
// 写入文件
try (FileOutputStream fileOut = new FileOutputStream(filename)) {
workbook.write(fileOut);
System.out.println("✅ 数据成功导出到 " + filename);
} catch (IOException e) {
System.err.println("导出Excel失败" + e.getMessage());
} finally {
try {
workbook.close();
} catch (IOException e) {
// Ignore
}
}
}
}