初步完成excel操作类

main
lensfrex 3 years ago
parent c71830fd87
commit bf9f6b8b38
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 5
      .idea/jarRepositories.xml
  2. 26
      DoubanDataPraserS/DoubanDataPraserS.iml
  3. 18
      DoubanDataPraserS/pom.xml
  4. 41
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/Main.java
  5. 20
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/MovieInformation.java
  6. 28
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/data/MovieInformation.java
  7. 9
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/excel/DataExporter.java
  8. 74
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/excel/ExcelWorker.java
  9. 25
      DoubanDataPraserS/src/main/java/me/lensfrex/doubandps/utils/IOUtil.java
  10. 23
      DoubanDataPraserS/src/test/java/me/lensfrex/test/Douban.java

@ -16,5 +16,10 @@
<option name="name" value="JBoss Community repository" /> <option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
</component> </component>
</project> </project>

@ -13,5 +13,31 @@
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" /> <orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" /> <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.22" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi:5.2.2" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-collections4:4.4" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.11.0" level="project" />
<orderEntry type="library" name="Maven: com.zaxxer:SparseBitSet:1.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml:5.2.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-lite:5.2.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.xmlbeans:xmlbeans:5.0.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-compress:1.21" level="project" />
<orderEntry type="library" name="Maven: com.github.virtuald:curvesapi:1.07" level="project" />
<orderEntry type="library" name="Maven: com.alibaba:easyexcel:3.0.5" level="project" />
<orderEntry type="library" name="Maven: org.apache.poi:poi-ooxml-schemas:4.1.2" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-csv:1.8" level="project" />
<orderEntry type="library" name="Maven: cglib:cglib:3.3.0" level="project" />
<orderEntry type="library" name="Maven: org.ow2.asm:asm:7.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.32" level="project" />
<orderEntry type="library" name="Maven: org.ehcache:ehcache:3.8.1" level="project" />
<orderEntry type="library" name="Maven: org.glassfish.jaxb:jaxb-runtime:2.3.1" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
<orderEntry type="library" name="Maven: org.glassfish.jaxb:txw2:2.3.1" level="project" />
<orderEntry type="library" name="Maven: com.sun.istack:istack-commons-runtime:3.0.7" level="project" />
<orderEntry type="library" name="Maven: org.jvnet.staxex:stax-ex:1.8" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.fastinfoset:FastInfoset:1.2.15" level="project" />
<orderEntry type="library" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
</component> </component>
</module> </module>

@ -20,12 +20,6 @@
<version>2.8.6</version> <version>2.8.6</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
@ -38,6 +32,18 @@
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>5.2.2</version> <version>5.2.2</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies> </dependencies>

@ -1,7 +1,48 @@
package me.lensfrex.doubandps; 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 { public class Main {
private final static Logger log = Logger.getLogger("MainLogger");
public static void main(String[] args) { 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();
}
}

@ -1,27 +1,18 @@
package me.lensfrex.doubandps.utils; package me.lensfrex.doubandps.utils;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
public class IOUtil { public class IOUtil {
public static String inputStreamToString(InputStream inputStream, Charset charSet) { public static String inputStreamToString(InputStream inputStream, Charset charSet) throws IOException {
byte[] bytes = readDataFromInputStream(inputStream); return new String(readDataFromInputStream(inputStream), charSet);
if (bytes != null) {
return new String(bytes, charSet);
} else {
return null;
}
} }
public static byte[] readDataFromInputStream(InputStream inputStream) { public static byte[] readDataFromInputStream(InputStream inputStream) throws IOException {
return readDataFromInputStream(inputStream, 5);// read every 5kb in default return readDataFromInputStream(inputStream, 5);// read every 5kb in default
} }
public static byte[] readDataFromInputStream(InputStream inputStream, int byteAllocation) { public static byte[] readDataFromInputStream(InputStream inputStream, int byteAllocation) throws IOException {
try {
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024 * byteAllocation]; byte[] bytes = new byte[1024 * byteAllocation];
@ -35,10 +26,10 @@ public class IOUtil {
byteArrayInputStream.close(); byteArrayInputStream.close();
return byteArrayInputStream.toByteArray(); return byteArrayInputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
return null;
} }
public static byte[] getFileData(String path) throws IOException {
return readDataFromInputStream(new FileInputStream(path));
} }
public static void writeFile(byte[] bytes, File file) throws Exception { public static void writeFile(byte[] bytes, File file) throws Exception {

@ -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…
Cancel
Save