whycxzp
2022-09-08 200b7aa1a55bf453424955875a8aa22791e7e2a6
src/main/java/com/whyc/service/BattRealdataService.java
@@ -1,6 +1,7 @@
package com.whyc.service;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.BattDataDTO;
import com.whyc.dto.BattTestData;
import com.whyc.dto.Response;
import com.whyc.dto.result.RealDateDTO;
@@ -9,10 +10,19 @@
import com.whyc.pojo.BattRealdata;
import com.whyc.util.ActionUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.ReflectUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
@@ -164,4 +174,50 @@
        PageInfo pageInfo = new PageInfo(list);
        return new Response().set(1,pageInfo);*/
    }
    public Response excelParse(MultipartFile file) throws IOException, InvalidFormatException {
        List dataList = new LinkedList();
        Workbook workbook = null;
        InputStream inputStream = file.getInputStream();
        workbook = WorkbookFactory.create(inputStream);
        Sheet sheet = workbook.getSheet("数据表");
        int lastRowNum = sheet.getLastRowNum();
        short lastCellNum = sheet.getRow(0).getLastCellNum();
        //获取单体的单元格值
        List<String> monKey = new LinkedList<>();
        for (int i = 4; i < lastCellNum-1; i++) {
            monKey.add(sheet.getRow(0).getCell(i).getStringCellValue());
        }
        sheet.getRow(0);
        //从第2行开始
        for (int i = 1; i < lastRowNum - 1; i++) {
            BattDataDTO data = new BattDataDTO();
            Map<String, Object> addProperties = new HashMap<>();
            for (int j = 0; j < lastCellNum-1; j++) {
                Cell cell = sheet.getRow(i).getCell(j);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String value = cell.getStringCellValue();
                if(value.equals("")){
                    break;
                }
                switch (j){
                    case 0:{data.setTime(value);}break;
                    case 1:{data.setTotalVol(value);}break;
                    case 2:{data.setTotalCurr(value);}break;
                    case 3:{data.setCap(value);}break;
                    default:{
                        int index = j-4;
                        addProperties.put(monKey.get(index),value);
                    }
                }
            }
            Object target = ReflectUtil.getTarget(data, addProperties);
            dataList.add(target);
        }
        return new Response().set(1,dataList);
    }
}