|
@@ -2,14 +2,17 @@ package com.css.simulation.resource.common.controller;
|
|
|
|
|
|
import api.common.pojo.common.ResponseBodyVO;
|
|
import api.common.pojo.common.ResponseBodyVO;
|
|
import api.common.util.ExcelUtil;
|
|
import api.common.util.ExcelUtil;
|
|
|
|
+import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestPart;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@@ -18,11 +21,36 @@ import java.util.List;
|
|
@RequestMapping("/excel")
|
|
@RequestMapping("/excel")
|
|
public class ExcelController {
|
|
public class ExcelController {
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 不解析第一列
|
|
|
|
+ * @param excel
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
@PostMapping(value = "/parse", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
@PostMapping(value = "/parse", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
- public ResponseBodyVO<List<List<String>>> parseExcel(@RequestPart("excel") MultipartFile excel) {
|
|
|
|
- try {
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public ResponseBodyVO<List<List<String>>> parse(@RequestPart("excel") MultipartFile excel) {
|
|
|
|
+ String originalFilename = excel.getOriginalFilename();
|
|
|
|
+ log.info("excel 名称为:" + originalFilename);
|
|
|
|
+ String suffix = originalFilename.split("\\.")[1];
|
|
|
|
+ InputStream inputStream = excel.getInputStream();
|
|
|
|
+ Workbook workbook = ExcelUtil.getExcel(inputStream, suffix);
|
|
|
|
+ Sheet sheet = ExcelUtil.getSheetByIndex(workbook, 0);
|
|
|
|
+ List<List<String>> table = ExcelUtil.getTable(sheet, 0, Integer.MAX_VALUE, 1, Integer.MAX_VALUE);
|
|
|
|
+ workbook.close();
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, table);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 解析全部字段
|
|
|
|
+ * @param excel
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @PostMapping(value = "/parseAll", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
|
|
|
+ @SneakyThrows
|
|
|
|
+ public ResponseBodyVO<List<List<String>>> parseAll(@RequestPart("excel") MultipartFile excel) {
|
|
String originalFilename = excel.getOriginalFilename();
|
|
String originalFilename = excel.getOriginalFilename();
|
|
- log.info("parseExcel() excel 名称为:" + originalFilename);
|
|
|
|
|
|
+ log.info("excel 名称为:" + originalFilename);
|
|
String suffix = originalFilename.split("\\.")[1];
|
|
String suffix = originalFilename.split("\\.")[1];
|
|
InputStream inputStream = excel.getInputStream();
|
|
InputStream inputStream = excel.getInputStream();
|
|
Workbook workbook = ExcelUtil.getExcel(inputStream, suffix);
|
|
Workbook workbook = ExcelUtil.getExcel(inputStream, suffix);
|
|
@@ -30,8 +58,8 @@ public class ExcelController {
|
|
List<List<String>> table = ExcelUtil.getTable(sheet, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
|
|
List<List<String>> table = ExcelUtil.getTable(sheet, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE);
|
|
workbook.close();
|
|
workbook.close();
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, table);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, table);
|
|
- } catch (IOException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|