ExcelImport

This commit is contained in:
MaxKey
2021-11-16 21:35:53 +08:00
parent fc0a4348d7
commit ba9b6ff9f9
6 changed files with 365 additions and 567 deletions

View File

@@ -18,11 +18,18 @@
package org.maxkey.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.IOException;
import java.io.InputStream;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.apache.mybatis.jpa.persistence.JpaBaseEntity;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
/**
@@ -48,6 +55,10 @@ public class ExcelImport extends JpaBaseEntity {
String updateExist;
InputStream inputStream = null;
Workbook workbook = null;
public ExcelImport() {
super();
}
@@ -76,5 +87,39 @@ public class ExcelImport extends JpaBaseEntity {
this.excelFile = excelFile;
}
public boolean isExcelNotEmpty() {
return excelFile != null && !excelFile.isEmpty() ;
}
public Workbook biuldWorkbook() throws IOException {
workbook = null;
inputStream = excelFile.getInputStream();
if (excelFile.getOriginalFilename().toLowerCase().endsWith(".xls")) {
workbook = new HSSFWorkbook(inputStream);
} else if (excelFile.getOriginalFilename().toLowerCase().endsWith(".xlsx")) {
workbook = new XSSFWorkbook(inputStream);
} else {
throw new RuntimeException("Excel suffix error.");
}
return workbook;
}
public void closeWorkbook() {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}