package com.util; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.sms.tenantexamineserver.domain.BaseStationEntity; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 功能描述 解析基站excel工具类 * * @author kangjinfan * @date 2019/3/23 */ public class HttpExcelUtil { public static List<BaseStationEntity> inputExcel(MultipartFile myFile,long messageId) { //1. 使用HSSFWorkbook 打开或者创建 “Excel对象” //2. 用HSSFWorkbook返回对象或者创建sheet对象 //3. 用sheet返回行对象,用行对象得到Cell对象 //4. 对Cell对象进行读写 List tbagents = new ArrayList<>(); Workbook workbook = null; String fileName = myFile.getOriginalFilename();// 获取文件名 if (fileName.endsWith("xls")) { try { workbook = new HSSFWorkbook(myFile.getInputStream());// 2003版本 } catch (IOException e) { e.printStackTrace(); } } else if (fileName.endsWith("xlsx")) { try { workbook = new XSSFWorkbook(myFile.getInputStream());// 2007版本 } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("不是Excel文件"); // 文件不是Excel文件 } Sheet sheet = workbook.getSheet("sheet1"); int rows = sheet.getLastRowNum(); if (rows == 0) { System.out.println("数据为空");// 数据为空 请填写数据 } long startTime = System.currentTimeMillis(); List<BaseStationEntity> list = new ArrayList<BaseStationEntity>(); for (int i = 1; i <= rows + 1; i++) { Row row = sheet.getRow(i); if (row != null) { BaseStationEntity baseStationEntity = BaseStationEntity.builder() .stationId(IdWorker.getId()) .stationName(row.getCell(0).getStringCellValue()) .bci(row.getCell(1).getStringCellValue()) .ci(row.getCell(2).getStringCellValue()) .longitude(row.getCell(3).getStringCellValue()) .dimension(row.getCell(4).getStringCellValue()) .messageId(messageId) .build(); list.add(baseStationEntity); } } return list; } }
解析上传excel
最新推荐文章于 2021-11-04 19:15:31 发布