基本完成,可能还会稍微修改

main
lensfrex 3 years ago
parent bf9f6b8b38
commit 60343430de
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 3
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/Main.java
  2. 12
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/data/MovieInformation.java
  3. 66
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/data/MovieInformationDataExporter.java
  4. 9
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/excel/DataExporter.java
  5. 17
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/excel/ExcelWorker.java

@ -2,6 +2,7 @@ package me.lensfrex.doubandps;
import com.google.gson.*; import com.google.gson.*;
import me.lensfrex.doubandps.data.MovieInformation; import me.lensfrex.doubandps.data.MovieInformation;
import me.lensfrex.doubandps.data.MovieInformationDataExporter;
import me.lensfrex.doubandps.utils.IOUtil; import me.lensfrex.doubandps.utils.IOUtil;
import java.io.IOException; import java.io.IOException;
@ -37,7 +38,9 @@ public class Main {
movieInformations.add(new Gson().fromJson(dataArray.get(i), MovieInformation.class)); movieInformations.add(new Gson().fromJson(dataArray.get(i), MovieInformation.class));
} }
MovieInformationDataExporter movieInformationDataExporter = new MovieInformationDataExporter("./douban.xlsx", MovieInformation.class);
movieInformationDataExporter.exportInformationByYear(movieInformations);
} catch (IOException e) { } catch (IOException e) {
log.severe("读文件出问题啦:" + e.getMessage()); log.severe("读文件出问题啦:" + e.getMessage());

@ -5,24 +5,24 @@ import com.google.gson.annotations.SerializedName;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
@Getter @Getter
@EqualsAndHashCode @EqualsAndHashCode
public class MovieInformation { public class MovieInformation {
@SerializedName("name_Ch") @SerializedName("name_Ch")
@ExcelProperty("影片名称") @ExcelProperty(index = 0, value = "影片名称")
private String chineseName; private String chineseName;
@SerializedName("外文名") @SerializedName("name_Fr")
@ExcelProperty(index = 1, value = "外文名")
private String alienName; private String alienName;
@ExcelProperty("上映年份") @ExcelProperty(index = 2, value = "上映年份")
private int year; private int year;
@ExcelProperty("评分") @ExcelProperty(index = 3, value = "评分")
private float point; private float point;
@SerializedName("num") @SerializedName("num")
@ExcelProperty("评价人数") @ExcelProperty(index = 4, value = "评价人数")
private int evaluationAmount; private int evaluationAmount;
} }

@ -0,0 +1,66 @@
package me.lensfrex.doubandps.data;
import me.lensfrex.doubandps.excel.ExcelWorker;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
public class MovieInformationDataExporter {
private static final Logger log = Logger.getLogger("DataExporterLogger");
ExcelWorker<MovieInformation> excelWorker = null;
private final Class<MovieInformation> dataClass;
public MovieInformationDataExporter(String excelFilePath, Class<MovieInformation> dataClass) {
this.dataClass = dataClass;
this.createNewExcelWorker(excelFilePath);
}
public boolean createNewExcelWorker(String newExcelFilePath) {
try {
excelWorker = new ExcelWorker<>(newExcelFilePath, dataClass);
} catch (IOException e) {
excelWorker = null;
log.severe("创建ExcelWorker时出现I/O问题。ErrMessage:" + e.getMessage() +
"指定的Excel文件路径:" + newExcelFilePath);
return false;
} catch (Exception e) {
excelWorker = null;
log.severe("创建ExcelWorker时发生未知错误.ErrMessage:" + e.getMessage() +
"指定的Excel文件路径:" + newExcelFilePath);
return false;
}
return true;
}
public void insertDataIntoExcel(int sheetIndex, Collection<MovieInformation> source) {
excelWorker.addAllDataToSheet(sheetIndex, source);
}
public void exportInformationByYear(List<MovieInformation> source) {
excelWorker.createNewSheet("2000年前");
int sheetAmount = excelWorker.createNewSheet("2000年后");
ArrayList<MovieInformation> movieBefore2000 = new ArrayList<>();
ArrayList<MovieInformation> movieAfter2000 = new ArrayList<>();
for (MovieInformation movieInformation : source) {
if (movieInformation.getYear() < 2000) {
movieBefore2000.add(movieInformation);
} else {
movieAfter2000.add(movieInformation);
}
}
excelWorker.addAllDataToSheet(sheetAmount - 2, movieBefore2000);
excelWorker.addAllDataToSheet(sheetAmount - 1, movieAfter2000);
excelWorker.finish();
log.info("已完成导出。");
}
}

@ -1,9 +0,0 @@
package me.lensfrex.doubandps.excel;
import me.lensfrex.doubandps.data.MovieInformation;
public class DataExporter {
public DataExporter() {
}
}

@ -8,6 +8,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.logging.Logger;
/** /**
* 针对特定数据类进行excel操作的Worker操作类 * 针对特定数据类进行excel操作的Worker操作类
@ -16,6 +17,8 @@ import java.util.Collection;
* @param <T> 指定的数据类class * @param <T> 指定的数据类class
*/ */
public final class ExcelWorker<T> { public final class ExcelWorker<T> {
private final static Logger log = Logger.getLogger("ExcelWorkerLogger");
private final ExcelWriter excelWriter; private final ExcelWriter excelWriter;
private final ArrayList<WriteSheet> sheets = new ArrayList<>(); private final ArrayList<WriteSheet> sheets = new ArrayList<>();
@ -30,11 +33,11 @@ public final class ExcelWorker<T> {
*/ */
public ExcelWorker(String excelFilePath, Class<T> clazz) throws IOException { public ExcelWorker(String excelFilePath, Class<T> clazz) throws IOException {
File excelFile = new File(excelFilePath); File excelFile = new File(excelFilePath);
if (!excelFile.exists()) { if (excelFile.exists()) {
File parent = excelFile.getParentFile(); log.warning("目标excel文件\"" + excelFilePath + "\"已存在,可能会覆盖原有数据或excel文件读取失败。");
if (parent.mkdirs()) { } else {
parent.createNewFile(); excelFile.getParentFile().mkdirs();
} excelFile.createNewFile();
} }
excelWriter = EasyExcel.write(excelFile).head(clazz).build(); excelWriter = EasyExcel.write(excelFile).head(clazz).build();
@ -71,4 +74,8 @@ public final class ExcelWorker<T> {
public void finish() { public void finish() {
excelWriter.finish(); excelWriter.finish();
} }
public int getSheetAmount() {
return sheetAmount;
}
} }

Loading…
Cancel
Save