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.

38 lines
1.3 KiB

package com.yyt.moviecrawler.util;
import com.yyt.moviecrawler.model.Movie;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.util.List;
public class ExcelUtil {
public static void exportToExcel(List<Movie> list, String filePath) throws Exception {
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("作者/来源");
// 填充数据
for (int i = 0; i < list.size(); i++) {
Movie m = list.get(i);
Row row = sheet.createRow(i + 1);
row.createCell(0).setCellValue(m.getTitle());
row.createCell(1).setCellValue(m.getScore());
row.createCell(2).setCellValue(m.getType());
row.createCell(3).setCellValue(m.getAuthor());
}
// 写出文件
try (FileOutputStream out = new FileOutputStream(filePath)) {
workbook.write(out);
}
workbook.close();
}
}