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.
103 lines
4.3 KiB
103 lines
4.3 KiB
package com.yyt.moviecrawler.util;
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import java.io.FileOutputStream;
|
|
import java.util.List;
|
|
import java.util.function.Function;
|
|
|
|
public class ExcelExporter {
|
|
// 导出电影数据
|
|
public static void exportMovies(List<? extends com.yyt.moviecrawler.model.Movie> movies, String fileName) {
|
|
try (Workbook workbook = new XSSFWorkbook()) {
|
|
Sheet sheet = workbook.createSheet("电影数据");
|
|
Row header = sheet.createRow(0);
|
|
header.createCell(0).setCellValue("电影名称");
|
|
header.createCell(1).setCellValue("评分");
|
|
header.createCell(2).setCellValue("类型");
|
|
header.createCell(3).setCellValue("作者/导演");
|
|
|
|
int rowIndex = 1;
|
|
for (com.yyt.moviecrawler.model.Movie movie : movies) {
|
|
Row row = sheet.createRow(rowIndex++);
|
|
row.createCell(0).setCellValue(movie.getTitle());
|
|
row.createCell(1).setCellValue(movie.getScore());
|
|
row.createCell(2).setCellValue(movie.getType());
|
|
row.createCell(3).setCellValue(movie.getAuthor());
|
|
}
|
|
|
|
autoSizeColumns(sheet, 4);
|
|
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
|
workbook.write(fos);
|
|
}
|
|
System.out.println("✅ " + fileName + " 导出成功!共" + movies.size() + "条数据");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// 导出图书数据
|
|
public static void exportBooks(List<com.yyt.moviecrawler.model.Book> books, String fileName) {
|
|
try (Workbook workbook = new XSSFWorkbook()) {
|
|
Sheet sheet = workbook.createSheet("图书数据");
|
|
Row header = sheet.createRow(0);
|
|
header.createCell(0).setCellValue("书名");
|
|
header.createCell(1).setCellValue("价格(£)");
|
|
header.createCell(2).setCellValue("星级");
|
|
header.createCell(3).setCellValue("来源");
|
|
|
|
int rowIndex = 1;
|
|
for (com.yyt.moviecrawler.model.Book book : books) {
|
|
Row row = sheet.createRow(rowIndex++);
|
|
row.createCell(0).setCellValue(book.getTitle());
|
|
row.createCell(1).setCellValue(book.getPrice());
|
|
row.createCell(2).setCellValue(book.getStarRating());
|
|
row.createCell(3).setCellValue(book.getCategory());
|
|
}
|
|
|
|
autoSizeColumns(sheet, 4);
|
|
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
|
workbook.write(fos);
|
|
}
|
|
System.out.println("✅ " + fileName + " 导出成功!共" + books.size() + "条数据");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// 导出新闻数据
|
|
public static void exportNews(List<com.yyt.moviecrawler.model.NewsArticle> articles, String fileName) {
|
|
try (Workbook workbook = new XSSFWorkbook()) {
|
|
Sheet sheet = workbook.createSheet("新闻数据");
|
|
Row header = sheet.createRow(0);
|
|
header.createCell(0).setCellValue("标题");
|
|
header.createCell(1).setCellValue("摘要");
|
|
header.createCell(2).setCellValue("发布时间");
|
|
header.createCell(3).setCellValue("分类");
|
|
|
|
int rowIndex = 1;
|
|
for (com.yyt.moviecrawler.model.NewsArticle article : articles) {
|
|
Row row = sheet.createRow(rowIndex++);
|
|
row.createCell(0).setCellValue(article.getTitle());
|
|
row.createCell(1).setCellValue(article.getSummary());
|
|
row.createCell(2).setCellValue(article.getPublishTime());
|
|
row.createCell(3).setCellValue(article.getCategory());
|
|
}
|
|
|
|
autoSizeColumns(sheet, 4);
|
|
try (FileOutputStream fos = new FileOutputStream(fileName)) {
|
|
workbook.write(fos);
|
|
}
|
|
System.out.println("✅ " + fileName + " 导出成功!共" + articles.size() + "条数据");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static void autoSizeColumns(Sheet sheet, int count) {
|
|
for (int i = 0; i < count; i++) {
|
|
sheet.autoSizeColumn(i);
|
|
}
|
|
}
|
|
}
|