Explorar el Código

返点-首投有效素材_导出

zhaoxian hace 1 año
padre
commit
d86c8779bc

+ 5 - 0
jeecg-boot-module-system/pom.xml

@@ -148,6 +148,11 @@
 			<version>1.4.2</version>
 		</dependency>
 		<dependency>
+			<groupId>com.monitorjbl</groupId>
+			<artifactId>xlsx-streamer</artifactId>
+			<version>2.2.0</version>
+		</dependency>
+		<dependency>
 			<groupId>org.bytedeco</groupId>
 			<artifactId>javacv-platform</artifactId>
 			<version>1.4.2</version>

+ 5 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/material/controller/FirstDeliveryValidController.java

@@ -45,6 +45,11 @@ public class FirstDeliveryValidController {
             if (Check.isNull(file)) {
                 return Result.error("执行失败,请上传应用Excel文件");
             }
+            String fileName = file.getOriginalFilename();
+            String substring = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
+            if (!"XLSX".equals(substring)) {
+                return Result.error("请上传正确格式的文件,仅支持扩展名.xlsx");
+            }
             return firstDeliveryValidService.importExcelMaterials(file);
         } catch (Exception e) {
             e.printStackTrace();

+ 77 - 3
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/material/service/impl/FirstDeliveryValidServiceImpl.java

@@ -2,6 +2,9 @@ package cn.com.ctop.toutiao.modules.material.service.impl;
 
 import cn.com.ctop.common.module.utils.Check;
 import cn.com.ctop.common.module.utils.ExportExcelUtils;
+import cn.com.ctop.common.module.utils.LoadFileUtil;
+import cn.com.ctop.shiwan.modules.FileUtil;
+import cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload;
 import cn.com.ctop.toutiao.modules.material.entity.FirstDeliveryValidMaterials;
 import cn.com.ctop.toutiao.modules.material.entity.FirstDeliveryValidRatio;
 import cn.com.ctop.toutiao.modules.material.mapper.FirstDeliveryValidMapper;
@@ -10,7 +13,9 @@ import com.alibaba.fastjson.JSONObject;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.google.common.collect.Lists;
+import com.monitorjbl.xlsx.StreamingReader;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -19,11 +24,18 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.UUID;
 
 @Slf4j
 @Service
@@ -31,14 +43,18 @@ public class FirstDeliveryValidServiceImpl implements FirstDeliveryValidService
 
     @Autowired
     private FirstDeliveryValidMapper deliveryValidMapper;
-
+    @Value("${zip.local.download-path}")
+    private String downloadPath;
 
     @Override
     public Result<Object> importExcelMaterials(MultipartFile file) throws Exception {
         List<FirstDeliveryValidMaterials> dataList = new ArrayList<>();
+        StringBuffer path = new StringBuffer();
         try {
-            String fileName = file.getOriginalFilename();
-            List<JSONObject> list = this.analysisFile(file, fileName);
+            path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.SHORT_FORMAT)).append("/").append(UUID.randomUUID().toString()).append("/");
+            log.info("-------下载到服务器地址:{}", path.toString());
+            List<JSONObject> list = this.analysisBIGBIGFile(file, path.toString());
+            LoadFileUtil.delFile(path.toString());
             for (JSONObject object : list) {
                 FirstDeliveryValidMaterials material = new FirstDeliveryValidMaterials(object);
                 if (!Check.isNull(material.getStatDate())) {
@@ -49,6 +65,8 @@ public class FirstDeliveryValidServiceImpl implements FirstDeliveryValidService
         } catch (Exception e) {
             e.printStackTrace();
             throw new Exception("导入异常," + e.getMessage());
+        } finally {
+            LoadFileUtil.delFile(path.toString());
         }
         if (!Check.isNull(dataList)) {
             //根据当前年份和月份,删除旧数据
@@ -193,6 +211,62 @@ public class FirstDeliveryValidServiceImpl implements FirstDeliveryValidService
     }
 
 
+    public List<JSONObject> analysisBIGBIGFile(MultipartFile mfile, String path) throws Exception {
+        InputStream is = null;
+        String newFileUrl = FileUtil.approvalFile(mfile, path);
+        List<JSONObject> list = null;
+        try {
+            File file = new File(newFileUrl);
+            is = new FileInputStream(file);
+            Workbook workbook = StreamingReader.builder()
+                    .rowCacheSize(20000)
+                    .bufferSize(4096)
+                    .open(is);
+            ExportExcelUtils eeu = new ExportExcelUtils();
+            //最后一列(列数)
+            int lastCellNum = 0;
+            // list存储数据集
+            list = new ArrayList<>();
+            for (Sheet sheet : workbook) {
+                // sheet.getLastRowNum()获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
+                int sumPageNum = (sheet.getLastRowNum() + 1) / 20000 + 1;       //计算分片数
+                for (int i = 1; i <= sumPageNum; i++) {     //读取每片范围内的内容
+                    try {
+                        int minRowNum = 20000 * (i - 1) + 1;    //计算每片读取的行数范围
+                        int maxRowNum = 20000 * i;
+                        for (Row row : sheet) {
+                            if (lastCellNum == 0) {
+                                lastCellNum = row.getLastCellNum();
+                            }
+                            if (row != null) {
+                                Map<String, Object> columnValueMap = new HashMap<>();
+                                if (row.getRowNum() >= minRowNum && row.getRowNum() <= maxRowNum) {//控制每片读取的行
+                                    //每行数据对象,按照顺序从0到(lastCellNum-1)记录
+                                    JSONObject cellEntity = new JSONObject();
+                                    for (int k = 0; k < lastCellNum; k++) {
+                                        cellEntity.put(String.valueOf(k), eeu.getCellValue2(row.getCell(k)));
+                                    }
+                                    list.add(cellEntity);
+                                    if (row.getRowNum() == maxRowNum) {//读取到每片最大行数限制,跳出,执行数据写入
+                                        break;
+                                    }
+                                }
+                            }
+                        }
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+            workbook.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            IOUtils.closeQuietly(is);
+        }
+        return list;
+    }
+
     public static List<JSONObject> analysisFile(MultipartFile file, String fileName) throws Exception {
         JSONObject returnJson = new JSONObject();
         Workbook workbook = null;