parent
							
								
									c71830fd87
								
							
						
					
					
						commit
						bf9f6b8b38
					
				| @ -1,7 +1,48 @@ | ||||
| package me.lensfrex.doubandps; | ||||
| 
 | ||||
| import com.google.gson.*; | ||||
| import me.lensfrex.doubandps.data.MovieInformation; | ||||
| import me.lensfrex.doubandps.utils.IOUtil; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.ArrayList; | ||||
| import java.util.logging.Logger; | ||||
| 
 | ||||
| public class Main { | ||||
|     private final static Logger log = Logger.getLogger("MainLogger"); | ||||
| 
 | ||||
|     public static void main(String[] args) { | ||||
|         new Main().run(); | ||||
|     } | ||||
| 
 | ||||
|     // 忙,先这样写吧
 | ||||
|     // todo: 等什么时候稍微有空再改改
 | ||||
|     private void run() { | ||||
|         // information不可数,但我就是要数
 | ||||
|         ArrayList<MovieInformation> movieInformations = new ArrayList<>(); | ||||
| 
 | ||||
|         try { | ||||
|             InputStream sourceFileStream = getClass().getResourceAsStream("/douban.json"); | ||||
|             if (sourceFileStream == null) { | ||||
|                 log.severe("源数据文件不存在,润了,不干了"); | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             String sourceFileString = IOUtil.inputStreamToString(sourceFileStream, StandardCharsets.UTF_8); | ||||
|             JsonArray dataArray = new Gson().fromJson(sourceFileString, JsonArray.class); | ||||
| 
 | ||||
|             for (int i = 0; i < dataArray.size(); i++) { | ||||
|                 movieInformations.add(new Gson().fromJson(dataArray.get(i), MovieInformation.class)); | ||||
|             } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         } catch (IOException e) { | ||||
|             log.severe("读文件出问题啦:" + e.getMessage()); | ||||
|         } catch (JsonSyntaxException e) { | ||||
|             System.err.println("Json文件数据不对捏:" + e.getMessage()); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,20 +0,0 @@ | ||||
| package me.lensfrex.doubandps; | ||||
| 
 | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import lombok.Data; | ||||
| 
 | ||||
| @Data | ||||
| public class MovieInformation { | ||||
|     @SerializedName("name_Ch") | ||||
|     private String chineseName; | ||||
| 
 | ||||
|     @SerializedName("name_Fr") | ||||
|     private String frenchName; | ||||
| 
 | ||||
|     @SerializedName("num") | ||||
|     private int id; | ||||
| 
 | ||||
|     private float point; | ||||
| 
 | ||||
|     private int year; | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package me.lensfrex.doubandps.data; | ||||
| 
 | ||||
| import com.alibaba.excel.annotation.ExcelProperty; | ||||
| import com.google.gson.annotations.SerializedName; | ||||
| import lombok.EqualsAndHashCode; | ||||
| import lombok.Getter; | ||||
| 
 | ||||
| 
 | ||||
| @Getter | ||||
| @EqualsAndHashCode | ||||
| public class MovieInformation { | ||||
|     @SerializedName("name_Ch") | ||||
|     @ExcelProperty("影片名称") | ||||
|     private String chineseName; | ||||
| 
 | ||||
|     @SerializedName("外文名") | ||||
|     private String alienName; | ||||
| 
 | ||||
|     @ExcelProperty("上映年份") | ||||
|     private int year; | ||||
| 
 | ||||
|     @ExcelProperty("评分") | ||||
|     private float point; | ||||
| 
 | ||||
|     @SerializedName("num") | ||||
|     @ExcelProperty("评价人数") | ||||
|     private int evaluationAmount; | ||||
| } | ||||
| @ -0,0 +1,9 @@ | ||||
| package me.lensfrex.doubandps.excel; | ||||
| 
 | ||||
| import me.lensfrex.doubandps.data.MovieInformation; | ||||
| 
 | ||||
| public class DataExporter { | ||||
| 
 | ||||
|     public DataExporter() { | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,74 @@ | ||||
| package me.lensfrex.doubandps.excel; | ||||
| 
 | ||||
| import com.alibaba.excel.EasyExcel; | ||||
| import com.alibaba.excel.ExcelWriter; | ||||
| import com.alibaba.excel.write.metadata.WriteSheet; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collection; | ||||
| 
 | ||||
| /** | ||||
|  * 针对特定数据类进行excel操作的Worker操作类 | ||||
|  * 其中生成的excel中每个sheet的标题都是根据数据类自动生成的 | ||||
|  * | ||||
|  * @param <T> 指定的数据类class | ||||
|  */ | ||||
| public final class ExcelWorker<T> { | ||||
|     private final ExcelWriter excelWriter; | ||||
|     private final ArrayList<WriteSheet> sheets = new ArrayList<>(); | ||||
| 
 | ||||
|     private int sheetAmount = 0; | ||||
| 
 | ||||
|     /** | ||||
|      * 根据指定的数据对象来构造一个ExcelWorker对象,其中每个sheet的标题都是根据传入的clazz设定的,建议使用空白的文件,防止数据意外覆盖 | ||||
|      * | ||||
|      * @param excelFilePath 欲操作的excel文件 | ||||
|      * @param clazz         初设定的sheet标题 | ||||
|      * @throws IOException 如果在初始化操作的时候发生I/O异常 | ||||
|      */ | ||||
|     public ExcelWorker(String excelFilePath, Class<T> clazz) throws IOException { | ||||
|         File excelFile = new File(excelFilePath); | ||||
|         if (!excelFile.exists()) { | ||||
|             File parent = excelFile.getParentFile(); | ||||
|             if (parent.mkdirs()) { | ||||
|                 parent.createNewFile(); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         excelWriter = EasyExcel.write(excelFile).head(clazz).build(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 根据指定的名字创建一个新的Sheet | ||||
|      * | ||||
|      * @param sheetName 新的Sheet名字 | ||||
|      * @return 创建后表格中Sheet数量 | ||||
|      */ | ||||
|     public int createNewSheet(String sheetName) { | ||||
|         sheets.add(EasyExcel.writerSheet(sheetAmount++, sheetName).build()); | ||||
|         return sheetAmount; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 向指定Index添加数据 | ||||
|      * | ||||
|      * @param sheetIndex 欲添加数据的Sheet的索引 | ||||
|      * @param source     数据源 | ||||
|      */ | ||||
|     public void addAllDataToSheet(int sheetIndex, Collection<T> source) { | ||||
|         if (sheetIndex >= sheetAmount) { | ||||
|             System.out.println(("sheet索引过大。传入索引:" + sheetIndex + ", 目前sheet总数:" + sheetAmount)); | ||||
|             return; | ||||
|         } | ||||
|         excelWriter.write(source, sheets.get(sheetIndex)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 结束操作,写入文件 | ||||
|      */ | ||||
|     public void finish() { | ||||
|         excelWriter.finish(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,23 @@ | ||||
| package me.lensfrex.test; | ||||
| 
 | ||||
| import com.alibaba.excel.EasyExcel; | ||||
| import com.alibaba.excel.ExcelWriter; | ||||
| import com.alibaba.excel.annotation.ExcelProperty; | ||||
| import com.alibaba.excel.write.metadata.WriteSheet; | ||||
| import me.lensfrex.doubandps.utils.IOUtil; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class Douban { | ||||
|     public static void main(String[] args) { | ||||
|         new Douban().run(); | ||||
|     } | ||||
| 
 | ||||
|     private void run() { | ||||
| 
 | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue