Browse Source

指标明细导入实现

‘xugp 1 year ago
parent
commit
abd5d23535

+ 10 - 0
ims-service/ims-eval/pom.xml

@@ -60,6 +60,16 @@
             <artifactId>feign-jackson</artifactId>
             <artifactId>feign-jackson</artifactId>
             <version>8.18.0</version>
             <version>8.18.0</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>3.17-beta1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>3.17-beta1</version>
+        </dependency>
     </dependencies>
     </dependencies>
     <build>
     <build>
         <plugins>
         <plugins>

+ 45 - 2
ims-service/ims-eval/src/main/java/com/ims/eval/controller/OrganizationEvaluationInfoController.java

@@ -1,15 +1,22 @@
 package com.ims.eval.controller;
 package com.ims.eval.controller;
 
 
 
 
+import com.alibaba.fastjson.JSONObject;
 import com.ims.eval.entity.OrganizationEvaluationInfo;
 import com.ims.eval.entity.OrganizationEvaluationInfo;
 import com.ims.eval.entity.dto.result.R;
 import com.ims.eval.entity.dto.result.R;
 import com.ims.eval.service.IOrganizationEvaluationInfoService;
 import com.ims.eval.service.IOrganizationEvaluationInfoService;
+import com.ims.eval.util.ExcelUtil;
+import io.jsonwebtoken.Claims;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
@@ -38,7 +45,6 @@ public class OrganizationEvaluationInfoController {
 
 
 
 
 	/**
 	/**
-	 *
 	 * @param organizationEvaluationId 目标责任书id
 	 * @param organizationEvaluationId 目标责任书id
 	 * @return
 	 * @return
 	 */
 	 */
@@ -72,10 +78,47 @@ public class OrganizationEvaluationInfoController {
 				return R.error().data("保存失败!");
 				return R.error().data("保存失败!");
 			}
 			}
 		} catch (Exception e) {
 		} catch (Exception e) {
-			log.error("错误",e);
+			log.error("错误", e);
 			return R.customError(e.getMessage()).data("失败!");
 			return R.customError(e.getMessage()).data("失败!");
 		}
 		}
 	}
 	}
 
 
 
 
+	@PostMapping(value = "/import")
+	@ResponseBody
+	public R importAlertrule(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
+
+		if (!file.isEmpty()) {
+			try {
+				//获取原始的文件名
+				String originalFilename = file.getOriginalFilename();
+				//获取文件类型
+				String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());
+				//默认从第一行开始读取
+				Integer startRows = 1;
+				//获取输入流
+				InputStream is = file.getInputStream();
+				List<OrganizationEvaluationInfo> bindingList = new ArrayList<>();
+				//Excel导入导出的单元类
+				List<String[]> strings = ExcelUtil.readData(fileType, startRows, true, is);
+				//遍历Excel表每一行的数据
+				for (String[] str : strings) {
+					OrganizationEvaluationInfo organizationEvaluationInfo = new OrganizationEvaluationInfo();
+					organizationEvaluationInfo.setId(str[0]);
+					organizationEvaluationInfo.setQuantifiedValue(Double.parseDouble(str[8]));
+					bindingList.add(organizationEvaluationInfo);
+				}
+				boolean b = organizationEvaluationInfoService.saveOrUpdateBatch(bindingList);
+				if (b) {
+					return R.ok().data(b);
+				} else {
+					return R.error().data("保存失败!");
+				}
+			} catch (Exception e) {
+				log.error("错误", e);
+				return R.customError(e.getMessage()).data("失败!");
+			}
+		}
+		return R.customError("上传文件为空!");
+	}
 }
 }

+ 147 - 0
ims-service/ims-eval/src/main/java/com/ims/eval/util/ExcelUtil.java

@@ -0,0 +1,147 @@
+package com.ims.eval.util;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.ss.usermodel.Cell;
+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.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class ExcelUtil {
+
+
+
+    /**
+     * 读取Excel的内容
+     *
+     * @param fileType 文件类型,xls或xlsx
+     * @param startRows 开始读取行数,比喻行头不需要读入 忽略的行数为1
+     * @param ignoreRowBlank 是否忽略空行
+     * @param is 文件输入流
+     * @return 读出的Excel中数据的内容
+     * @throws IOException duxxxxx
+     */
+    public static List<String[]> readData(String fileType, int startRows, boolean ignoreRowBlank, InputStream is) throws IOException {
+        List<String[]> result = new ArrayList<>();
+
+        Workbook wb = readExcel(fileType, is);
+        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
+            Sheet sheet = wb.getSheetAt(sheetIndex);
+
+            for (int rowIndex = startRows, z = sheet.getLastRowNum(); rowIndex <= z; rowIndex++) {
+                Row row = sheet.getRow(rowIndex);
+                if (row == null) {
+                    continue;
+                }
+
+                int rowSize = sheet.getRow(0).getLastCellNum();
+                String[] values = new String[rowSize];
+                boolean hasValue = false;
+                for (int columnIndex = 0; columnIndex < rowSize; columnIndex++) {
+                    String value = "";
+                    Cell cell = row.getCell(columnIndex);
+                    if (cell != null) {
+                        // 注意:一定要设成这个,否则可能会出现乱码,后面版本默认设置
+                        switch (cell.getCellType()) {
+                            case HSSFCell.CELL_TYPE_STRING:
+                                value = cell.getStringCellValue();
+                                break;
+                            case HSSFCell.CELL_TYPE_NUMERIC:
+                                if (HSSFDateUtil.isCellDateFormatted(cell)) {
+                                    Date date = cell.getDateCellValue();
+                                    if (date != null) {
+                                        value = new SimpleDateFormat("yyyy-MM-dd")
+                                                .format(date);
+                                    } else {
+                                        value = "";
+                                    }
+                                } else {
+                                    //value = new DecimalFormat("0").format(cell.getNumericCellValue());
+                                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
+                                        value = String.valueOf(cell.getDateCellValue());
+                                    } else {
+                                        cell.setCellType(Cell.CELL_TYPE_STRING);
+                                        String temp = cell.getStringCellValue();
+                                        // 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
+                                        if (temp.indexOf(".") > -1) {
+                                            value = String.valueOf(new Double(temp)).trim();
+                                        } else {
+                                            value = temp.trim();
+                                        }
+                                    }
+                                }
+                                break;
+                            case HSSFCell.CELL_TYPE_FORMULA:
+                                // 导入时如果为公式生成的数据则无值
+                                if (!cell.getStringCellValue().equals("")) {
+                                    value = cell.getStringCellValue();
+                                } else {
+                                    value = cell.getNumericCellValue() + "";
+                                }
+                                break;
+                            case HSSFCell.CELL_TYPE_BLANK:
+                                break;
+                            case HSSFCell.CELL_TYPE_ERROR:
+                                value = "";
+                                break;
+                            case HSSFCell.CELL_TYPE_BOOLEAN:
+                                value = (cell.getBooleanCellValue() == true ? "Y"
+
+                                        : "N");
+                                break;
+                            default:
+                                value = "";
+                        }
+                    }
+                    values[columnIndex] = value;
+                    if (!value.isEmpty()) {
+                        hasValue = true;
+                    }
+                }
+                if (!ignoreRowBlank || hasValue) {//不为忽略空行模式或不为空行
+                    result.add(values);
+                }
+            }
+        }
+        return result;
+    }
+
+    //读取excel
+    private static Workbook readExcel(String fileType, InputStream is) throws IOException {
+        if ("xls".equals(fileType)) {
+            return new HSSFWorkbook(is);
+        } else if ("xlsx".equals(fileType)) {
+            return new XSSFWorkbook(is);
+        } else {
+            throw new IllegalArgumentException("不支持的文件类型,仅支持xls和xlsx");
+        }
+    }
+
+    /**
+     * 去掉字符串右边的空格
+     *
+     * @param str 要处理的字符串
+     * @return 处理后的字符串
+     */
+    private static String rightTrim(String str) {
+        if (str == null) {
+            return "";
+        }
+        int length = str.length();
+        for (int i = length - 1; i >= 0; i--) {
+            if (str.charAt(i) != 0x20) {
+                break;
+            }
+            length--;
+        }
+        return str.substring(0, length);
+    }
+}