|
@@ -1,88 +1,120 @@
|
|
|
package com.gyee.frame.util.excel;
|
|
|
|
|
|
|
|
|
+import com.gyee.frame.common.domain.AjaxResult;
|
|
|
+import com.gyee.frame.common.exception.QiNiuException;
|
|
|
+import com.gyee.frame.common.exception.enums.QiNiuErrorEnum;
|
|
|
import com.gyee.frame.util.DateUtils;
|
|
|
import com.gyee.frame.util.StringUtils;
|
|
|
+import com.qiniu.common.QiniuException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.util.*;
|
|
|
|
|
|
+/**
|
|
|
+ * mis日报数据导入
|
|
|
+ */
|
|
|
@Slf4j
|
|
|
+@Component
|
|
|
public class MisDailyUtil {
|
|
|
|
|
|
static String name = "C:\\Users\\HP\\Desktop\\2022年数据\\2022年8月国电电力MIS每日数据.xlsx";
|
|
|
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
- * @param is 导入的数据流
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static List<MisDailyData> importStream(String fileName, InputStream is) throws Exception {
|
|
|
- if (StringUtils.isEmpty(fileName) || is == null)
|
|
|
- return null;
|
|
|
-
|
|
|
- String year = fileName.substring(0, 4);
|
|
|
- String month = fileName.substring(5,fileName.indexOf("月"));
|
|
|
- Calendar cal = Calendar.getInstance();
|
|
|
- cal.setTime(DateUtils.parseStrtoDate(year + "-" + month + "-01", DateUtils.YYYY_MM_DD));
|
|
|
- int dayMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
-
|
|
|
- List<MisDailyData> list = new ArrayList<>();
|
|
|
+ public List<MisDailyData> importStream(MultipartFile file){
|
|
|
+ InputStream is = null;
|
|
|
Workbook wb = null;
|
|
|
+ List<MisDailyData> list = new ArrayList<>();
|
|
|
+ try{
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ is = file.getInputStream();
|
|
|
+
|
|
|
+ String year = fileName.substring(0, 4);
|
|
|
+ String month = fileName.substring(5,fileName.indexOf("月"));
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(DateUtils.parseStrtoDate(year + "-" + month + "-01", DateUtils.YYYY_MM_DD));
|
|
|
+ int dayMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
|
|
+
|
|
|
+ //校验文件类型
|
|
|
+ if (checkFile(fileName) == 1) {
|
|
|
+ wb = new XSSFWorkbook(is);
|
|
|
+ } else if (checkFile(fileName) == 2) {
|
|
|
+ wb = new HSSFWorkbook(is);
|
|
|
+ } else {
|
|
|
+ throw new QiNiuException(QiNiuErrorEnum.FILE_NAME_ERROR);
|
|
|
+ }
|
|
|
|
|
|
- //校验文件类型
|
|
|
- if (checkFile(fileName) == 1) {
|
|
|
- wb = new XSSFWorkbook(is);
|
|
|
- } else if (checkFile(fileName) == 2) {
|
|
|
- wb = new HSSFWorkbook(is);
|
|
|
- } else {
|
|
|
- log.error("解析的文件类型错误!");
|
|
|
- return null;
|
|
|
- }
|
|
|
- Iterator<Sheet> sheets = wb.sheetIterator();
|
|
|
- while(sheets.hasNext()){
|
|
|
- Sheet sheet = sheets.next();
|
|
|
-
|
|
|
- //如果sheet不是日报内容则过滤
|
|
|
- String name = sheet.getSheetName();
|
|
|
- if (StringUtils.isEmpty(name) || !name.contains("日"))
|
|
|
- continue;
|
|
|
- String day = name.substring(0, name.indexOf("日"));
|
|
|
-
|
|
|
- for (int i = 8; i <= sheet.getLastRowNum(); i++){
|
|
|
- Row row = sheet.getRow(i);
|
|
|
- if (row == null)
|
|
|
- continue;
|
|
|
- if (i > 25)
|
|
|
- break;
|
|
|
-
|
|
|
- MisDailyData data;
|
|
|
- //单元格格式不统一,特殊处理
|
|
|
- if (i == 9 || i == 12 || i == 14){
|
|
|
- data = new MisDailyData(row, sheet.getRow(i + 1));
|
|
|
- }else{
|
|
|
- data = new MisDailyData(row, null);
|
|
|
- }
|
|
|
-
|
|
|
- if (i == 10 || i == 13 || i == 15)
|
|
|
- continue;
|
|
|
+ Iterator<Sheet> sheets = wb.sheetIterator();
|
|
|
+ while(sheets.hasNext()){
|
|
|
+ Sheet sheet = sheets.next();
|
|
|
|
|
|
- //比如6/31没有这天,系统会转成7/1,需要过滤掉
|
|
|
- if (Integer.valueOf(day) > dayMaximum)
|
|
|
+ //如果sheet不是日报内容则过滤
|
|
|
+ String name = sheet.getSheetName();
|
|
|
+ if (StringUtils.isEmpty(name) || !name.contains("日"))
|
|
|
continue;
|
|
|
-
|
|
|
- data.setimportdate(DateUtils.parseStrtoDate(year + "-" + month + "-" + day, DateUtils.YYYY_MM_DD));
|
|
|
- Map<Integer, String> map = projectMap();
|
|
|
- data.setProjectId(map.containsKey(data.getunitNo()) ? map.get(data.getunitNo()) : "");
|
|
|
- list.add(data);
|
|
|
+ String day = name.substring(0, name.indexOf("日"));
|
|
|
+
|
|
|
+ for (int i = 8; i <= sheet.getLastRowNum(); i++){
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ if (row == null)
|
|
|
+ continue;
|
|
|
+ if (i > 25)
|
|
|
+ break;
|
|
|
+ //sheet有表格但里边没数据
|
|
|
+ if (sheet.getRow(20).getCell(0).getNumericCellValue() <= 0)
|
|
|
+ break;
|
|
|
+
|
|
|
+ MisDailyData data;
|
|
|
+ //单元格格式不统一,特殊处理
|
|
|
+ if (i == 9 || i == 12 || i == 14){
|
|
|
+ data = new MisDailyData(row, sheet.getRow(i + 1));
|
|
|
+ }else{
|
|
|
+ data = new MisDailyData(row, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (i == 10 || i == 13 || i == 15)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ //比如6/31没有这天,系统会转成7/1,需要过滤掉
|
|
|
+ if (Integer.valueOf(day) > dayMaximum)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ data.setimportdate(DateUtils.parseStrtoDate(year + "-" + month + "-" + day, DateUtils.YYYY_MM_DD));
|
|
|
+ Map<Integer, String> map = projectMap();
|
|
|
+ data.setProjectId(map.containsKey(data.getunitNo()) ? map.get(data.getunitNo()) : "");
|
|
|
+ list.add(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IndexOutOfBoundsException e){
|
|
|
+ throw new QiNiuException(QiNiuErrorEnum.FILE_NAME_ERROR);
|
|
|
+ } catch (Exception e){
|
|
|
+ throw new QiNiuException(QiNiuErrorEnum.FILE_LONG_ERROR);
|
|
|
+ } finally {
|
|
|
+ if (is != null) {
|
|
|
+ try {
|
|
|
+ is.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (wb != null){
|
|
|
+ try {
|
|
|
+ wb.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -96,7 +128,7 @@ public class MisDailyUtil {
|
|
|
* @param fileName 传入的文件
|
|
|
* @return 1-XLS文件,2-XLSX文件,3-文件类型错误
|
|
|
*/
|
|
|
- private static int checkFile(String fileName) {
|
|
|
+ private int checkFile(String fileName) {
|
|
|
if (fileName.endsWith(".xlsx")) {
|
|
|
return 1;
|
|
|
}
|
|
@@ -107,8 +139,7 @@ public class MisDailyUtil {
|
|
|
return 3;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- private static Map<Integer, String> projectMap(){
|
|
|
+ private Map<Integer, String> projectMap(){
|
|
|
Map<Integer, String> map = new HashMap<>();
|
|
|
map.put(206, "MHS01_GC");
|
|
|
map.put(310, "XS02_GC");
|
|
@@ -130,14 +161,14 @@ public class MisDailyUtil {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
- File file = new File(name);
|
|
|
- String fileName = file.getName();
|
|
|
-
|
|
|
- InputStream is = new FileInputStream(file);
|
|
|
-
|
|
|
- List<MisDailyData> list = importStream(fileName, is);
|
|
|
-
|
|
|
- for (MisDailyData d : list)
|
|
|
- System.out.println(d.toString());
|
|
|
+// File file = new File(name);
|
|
|
+// String fileName = file.getName();
|
|
|
+//
|
|
|
+// InputStream is = new FileInputStream(file);
|
|
|
+//
|
|
|
+// List<MisDailyData> list = importStream(fileName, is);
|
|
|
+//
|
|
|
+// for (MisDailyData d : list)
|
|
|
+// System.out.println(d.toString());
|
|
|
}
|
|
|
}
|