You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package com.iflytop.nuclear.service.impl;
import com.iflytop.nuclear.model.Account; import com.iflytop.nuclear.model.NuclearCoreConfig; import com.iflytop.nuclear.service.NuclearCoreConfigService; import com.iflytop.nuclear.service.UploadService; 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.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List;
/** * @author cool * @date 2023/6/28 14:21 */ @Service public class UploadServiceImpl implements UploadService {
@Autowired NuclearCoreConfigService nuclearCoreConfigService;
@Override public boolean verifyFileFormat(MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); Workbook wb = new XSSFWorkbook(inputStream); //默认会跳过第一行标题
// 得到第一个shell
Sheet sheet = wb.getSheetAt(0); // 得到Excel的行数
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); int totalCells = 0; // 得到Excel的列数(前提是有行数)
if (physicalNumberOfRows > 1 && sheet.getRow(1) != null) { totalCells = sheet.getRow(1).getPhysicalNumberOfCells(); } if (physicalNumberOfRows == 41 && totalCells == 14) { return true; } return false; }
@Override public boolean saveFileContentToDB(MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); Workbook wb = new XSSFWorkbook(inputStream); Sheet sheet = wb.getSheetAt(0); int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); int physicalNumberOfColumns = sheet.getRow(1).getPhysicalNumberOfCells(); // 通过传递参数获取当前excel所处的taskid并存储
List<ArrayList> result = new ArrayList<>(); for (int r = 2; r < physicalNumberOfRows; r += 3) { List<List<String>> list = new ArrayList<>(); // 一次性对三行数据进行汇总并创建存储对象
for(int f = r; f < r + 3; f++){ List<String> line = new ArrayList<>(); Row row = sheet.getRow(f); for(int c = 1; c < physicalNumberOfColumns; c++) { Cell cell = row.getCell(c); if (null != cell) { String stringCellValue = cell.getStringCellValue(); line.add(stringCellValue); } } list.add(line); } result.add((ArrayList) list); } System.out.println(result); // 对result进行遍历组合成每条数据 插入数据库中
List<NuclearCoreConfig> nuclearCoreConfigArrayList = new ArrayList<NuclearCoreConfig>(); for(int i = 0; i < result.size(); i++) { int startIndex = i + 1; ArrayList rowData = result.get(i); for (int j = 0; j < 13; j++) { ArrayList line1 = (ArrayList) rowData.get(0); ArrayList line2 = (ArrayList) rowData.get(1); ArrayList line3 = (ArrayList) rowData.get(2); String s1 = (String) line1.get(j); String s2 = (String) line2.get(j); String s3 = (String) line3.get(j); String serialNumber = startIndex + "-" + (j + 1) ; // 创建存储数据库对象list
// 临时taskid
Long taskid = 1L; NuclearCoreConfig nuclearCoreConfig = NuclearCoreConfig.builder() .percent(s2) .firstSign(s1) .secondSign(s3) .taskId(taskid) .serialNumber(serialNumber) .build(); nuclearCoreConfigArrayList.add(nuclearCoreConfig); } } // 批量插入 如果该taskid 存在数据 需要删除 在前端进行判断
boolean b = nuclearCoreConfigService.saveBatch(nuclearCoreConfigArrayList); return b; } }
|