소스 검색

读取excel入库

admin 2 년 전
부모
커밋
7aecd58269

+ 28 - 4
ipfcst/ipfcst-reportquery/src/main/frontend/views/parameterConfiguration/FanUnitInfo/index.vue

@@ -4,8 +4,26 @@
       <div slot="header" class="clearfix">
         <span>机组信息</span>
       </div>
-      <el-button type="primary" size="small" @click="insertEvent" >新增</el-button>
-      <el-button type="primary" size="small" :loading="loading" @click="readFanUnitInfo" >读取excel</el-button>
+
+      <el-row>
+        <el-button type="primary" size="small" @click="insertEvent" >新增</el-button>
+        <el-upload
+          ref="upload"
+          name="file"
+          :http-request="this.readFanUnitInfo"
+          class="link-block"
+          action=""
+        >
+          <el-button size="small" type="primary" >读取excel</el-button>
+
+        </el-upload>
+
+      </el-row>
+
+
+
+
+
       <div>
         <el-table
           :data="tableData"
@@ -223,9 +241,12 @@ export default {
       this.currentPage = pageNum
       this.initPage()
     },
-    readFanUnitInfo(){
+    readFanUnitInfo(obj){
+      const formData = new FormData()
+      formData.append("multipartFile",obj.file)
       this.loading = true
-      this.$axios.get("/readToMysql/getFanUnitInfo").then(res=>{
+      this.$axios.post("/readToMysql/getFanUnitInfo", formData
+      ).then(res=>{
         this.$message.success(
           res.message
         )
@@ -245,6 +266,9 @@ export default {
   background: transparent;
   color: #ffffff;
 }
+.link-block{
+  display: inline-block;
+}
 .el-button {
   round-clip: 10px;
   color: #ffffff;

+ 5 - 7
ipfcst/ipfcst-reportquery/src/main/java/com/jiayue/ipfcst/controller/ReadToMysqlController.java

@@ -5,9 +5,8 @@ import com.jiayue.ipfcst.common.data.repository.WindSpeedPointInfoRepository;
 import com.jiayue.ipfcst.common.data.repository.WindTurbinePowerCurveRepository;
 import com.jiayue.ipfcst.util.ReadtoMysql;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.sql.SQLException;
 
@@ -27,11 +26,10 @@ public class ReadToMysqlController {
    * 读取机组信息
    * @return
    */
-  @GetMapping("/getFanUnitInfo")
-  public ResponseVO getFanUnitInfo() {
+  @PostMapping("/getFanUnitInfo")
+  public ResponseVO getFanUnitInfo(@RequestParam("multipartFile") MultipartFile multipartFile){
     try {
-
-      return ReadtoMysql.readFanUnitInfo();
+      return ReadtoMysql.readFanUnitInfo(multipartFile);
     } catch (Exception e) {
       e.printStackTrace();
       return ResponseVO.error(e);

+ 56 - 25
ipfcst/ipfcst-reportquery/src/main/java/com/jiayue/ipfcst/util/ReadtoMysql.java

@@ -13,7 +13,11 @@ import com.jiayue.ipfcst.common.data.repository.WindSpeedPointInfoRepository;
 import com.jiayue.ipfcst.common.data.repository.WindTurbinePowerCurveRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
 
+import javax.swing.plaf.multi.MultiInternalFrameUI;
+import java.io.File;
+import java.io.InputStream;
 import java.sql.SQLException;
 import java.text.DecimalFormat;
 import java.util.*;
@@ -30,16 +34,18 @@ public class ReadtoMysql {
 
   private static WindSpeedPointInfoRepository windSpeedPointInfoRepository;
   private static WindTurbinePowerCurveRepository windTurbinePowerCurveRepository;
+
   @Autowired
-  public void setWindSpeedPointInfoRepository(WindSpeedPointInfoRepository windSpeedPointInfoRepository){
+  public void setWindSpeedPointInfoRepository(WindSpeedPointInfoRepository windSpeedPointInfoRepository) {
     ReadtoMysql.windSpeedPointInfoRepository = windSpeedPointInfoRepository;
   }
+
   @Autowired
-  public void setWindTurbinePowerCurveRepository(WindTurbinePowerCurveRepository windTurbinePowerCurveRepository){
+  public void setWindTurbinePowerCurveRepository(WindTurbinePowerCurveRepository windTurbinePowerCurveRepository) {
     ReadtoMysql.windTurbinePowerCurveRepository = windTurbinePowerCurveRepository;
   }
 
-  public static void main(String[] args) throws Exception{
+  public static void main(String[] args) throws Exception {
 //        读取机组信息到数据库
 //        readFanUnitInfo();
     /**
@@ -59,12 +65,33 @@ public class ReadtoMysql {
 
   /**
    * 读取机组信息
+   *
    * @return
    */
-  public static ResponseVO readFanUnitInfo() throws Exception{
+  public static ResponseVO readFanUnitInfo(MultipartFile multipartFile) throws Exception {
+    //获取到文件名
+    String originalFilename = multipartFile.getOriginalFilename();
+    File fileP = new File("");
+    //获取项目路径
+    String filePath = fileP.getCanonicalPath();
+
+    String route = filePath + "/" + "Temporary/";
+    File file1 = new File(route);
+
+    if (!file1.exists()) {
+      file1.mkdirs();
+    }
+    File[] files = file1.listFiles();
+    //判断文件夹中是否有文件
+    if(files.length < 1){
+      multipartFile.transferTo(new File(route + originalFilename));
+    }
+    File file = new File(route + originalFilename);
+
     //机组信息读取 sheet 0
     List<FanUnitInfo> fanUnitInfoList;
-    try (ExcelReader reader = ExcelUtil.getReader(modleParamFilePath, 0)) {
+    try (
+      ExcelReader reader = ExcelUtil.getReader(file.getPath(), 0)) {
       reader.addHeaderAlias("序号", "id");
       reader.addHeaderAlias("机组名称", "fanName");
       reader.addHeaderAlias("风机容量KW", "fanUnitCapacity");
@@ -79,17 +106,18 @@ public class ReadtoMysql {
           Entity.create("t_fan_unit_info")
 //            .set("C_ID",fanUnitInfo.getId())
             .set("C_FAN_NAME", fanUnitInfo.getFanName())
-            .set("C_FAN_UNIT_CAPACITY",fanUnitInfo.getFanUnitCapacity())
-            .set("C_FULL_WIND_SPEED",fanUnitInfo.getFullWindSpeed())
-            .set("C_CUT_OUT_SPEED",fanUnitInfo.getCutOutSpeed())
-            .set("C_BENCHMARK_FAN",fanUnitInfo.getBenchmarkFan())
-            .set("C_FAN_NUM_ARRS",fanUnitInfo.getFanNumArrs())
+            .set("C_FAN_UNIT_CAPACITY", fanUnitInfo.getFanUnitCapacity())
+            .set("C_FULL_WIND_SPEED", fanUnitInfo.getFullWindSpeed())
+            .set("C_CUT_OUT_SPEED", fanUnitInfo.getCutOutSpeed())
+            .set("C_BENCHMARK_FAN", fanUnitInfo.getBenchmarkFan())
+            .set("C_FAN_NUM_ARRS", fanUnitInfo.getFanNumArrs())
         );
       }
-    }catch (cn.hutool.poi.exceptions.POIException e){
+    } catch (cn.hutool.poi.exceptions.POIException e) {
       return ResponseVO.error(e);
     }
-
+    //删除文件
+    file.delete();
     return ResponseVO.success(1);
 
   }
@@ -98,9 +126,9 @@ public class ReadtoMysql {
    * 读取 东北风向风速 东南风向风速  西南风向风速  西北风向风速 4个sheet表风速点表
    */
   public static ResponseVO readSpeedSheet() throws SQLException {
-    Map<Integer,List<String>> fanidInfo = new HashMap<>();
+    Map<Integer, List<String>> fanidInfo = new HashMap<>();
     List<WindSpeedPointInfo> windSpeedPointInfoList = new ArrayList<>();
-    DecimalFormat decimalFormat=new DecimalFormat(".00");
+    DecimalFormat decimalFormat = new DecimalFormat(".00");
 
     try (ExcelReader reader = ExcelUtil.getReader(modleParamFilePath, 0)) {
       reader.addHeaderAlias("序号", "id");
@@ -153,16 +181,17 @@ public class ReadtoMysql {
         }
       }
       windSpeedPointInfoRepository.saveAll(windSpeedPointInfoList);
-    }catch (cn.hutool.poi.exceptions.POIException e){
+    } catch (cn.hutool.poi.exceptions.POIException e) {
       return ResponseVO.error(e);
     }
     return ResponseVO.success(1);
   }
+
   /**
    * 读取风速曲线
    */
   public static ResponseVO readWindTurbinePowerCurveMap() throws SQLException {
-    for(int sheelIndex=10;sheelIndex<=14;sheelIndex++){
+    for (int sheelIndex = 10; sheelIndex <= 14; sheelIndex++) {
       //读取风速曲线 sheet
       String sheetName;
       List<List<Object>> readRows;
@@ -172,8 +201,8 @@ public class ReadtoMysql {
         sheetName = readerWind.getSheet().getSheetName();
         //按行读取所有数据
         readRows = readerWind.read();
-        for(List<Object> r: readRows){
-          if(String.valueOf(r.get(0)).contains("风")){
+        for (List<Object> r : readRows) {
+          if (String.valueOf(r.get(0)).contains("风")) {
             continue;
           }
           WindTurbinePowerCurve windTurbinePowerCurve = new WindTurbinePowerCurve();
@@ -184,14 +213,16 @@ public class ReadtoMysql {
         }
 
         windTurbinePowerCurveRepository.saveAll(windTurbinePowerCurves);
-      }catch (cn.hutool.poi.exceptions.POIException e){
+      } catch (cn.hutool.poi.exceptions.POIException e) {
         return ResponseVO.error(e);
       }
     }
     return ResponseVO.success(1);
   }
+
   /**
    * 满发风速之后各风速发电曲线
+   *
    * @return
    */
   public static ResponseVO readFanCutOutSpeedInfo() throws SQLException {
@@ -204,17 +235,17 @@ public class ReadtoMysql {
       reader.addHeaderAlias("发电功率kW", "powerGeneration");
       reader.addHeaderAlias("所属机组", "UnitBelongs");
       cutOutSpeedSpecifyInfoList = reader.readAll(CutOutSpeedSpecifyInfo.class);
-      for(CutOutSpeedSpecifyInfo cs : cutOutSpeedSpecifyInfoList){
+      for (CutOutSpeedSpecifyInfo cs : cutOutSpeedSpecifyInfoList) {
         Db.use().insertForGeneratedKey(
           Entity.create("t_cut_out_speed_specify_info")
 //            .set("C_ID",cs.getId())
-            .set("C_LOWER_WIND_SPEED_LIMIT",cs.getLowerWindSpeedLimit())
-            .set("C_HIGH_WIND_SPEED_LIMIT",cs.getHighWindSpeedLimit())
-            .set("C_POWER_GENERATION",cs.getPowerGeneration())
-            .set("C_UNIT_BELONGS",cs.getFanId())
+            .set("C_LOWER_WIND_SPEED_LIMIT", cs.getLowerWindSpeedLimit())
+            .set("C_HIGH_WIND_SPEED_LIMIT", cs.getHighWindSpeedLimit())
+            .set("C_POWER_GENERATION", cs.getPowerGeneration())
+            .set("C_UNIT_BELONGS", cs.getFanId())
         );
       }
-    } catch (cn.hutool.poi.exceptions.POIException e){
+    } catch (cn.hutool.poi.exceptions.POIException e) {
       return ResponseVO.error(e);
     }
     return ResponseVO.success(1);