您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關springboot中怎么讀取Excel文件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
首先引入相關依賴
<!--解析office相關文件--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!--解析office相關文件-->
工具類
import org.apache.poi.hssf.usermodel.HSSFWorkbook;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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.multipart.MultipartFile;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Map;public class OfficeUtils { protected static final Logger logger = LoggerFactory.getLogger(OfficeUtils.class); public static Map<Integer, Map<Integer, Object>> readExcelContentz(MultipartFile file) throws Exception { Map<Integer, Map<Integer, Object>> content = new HashMap<Integer, Map<Integer, Object>>(); // 上傳文件名 Workbook wb = getWb(file); if (wb == null) { throw new BusinessException(ErrorType.WORK_BOOK_EMPTY); } Sheet sheet = wb.getSheetAt(0); // 得到總行數 int rowNum = sheet.getLastRowNum(); Row row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文內容應該從第二行開始,第一行為表頭的標題 for (int i = 1; i <= rowNum; i++) { row = sheet.getRow(i); int j = 0; Map<Integer, Object> cellValue = new HashMap<Integer, Object>(); while (j < colNum) { Object obj = getCellFormatValue(row.getCell(j)); cellValue.put(j, obj); j++; } content.put(i, cellValue); } return content; } //根據Cell類型設置數據 private static Object getCellFormatValue(Cell cell) { Object cellvalue = ""; if (cell != null) { switch (cell.getCellTypeEnum()) { case NUMERIC: cellvalue = String.valueOf(cell.getNumericCellValue()); break; case FORMULA: { cellvalue = cell.getDateCellValue(); break; } case STRING: cellvalue = cell.getRichStringCellValue().getString(); break; default: cellvalue = ""; } } else { cellvalue = ""; } return cellvalue; } private static Workbook getWb(MultipartFile mf) { String filepath = mf.getOriginalFilename(); String ext = filepath.substring(filepath.lastIndexOf(".")); Workbook wb = null; try { InputStream is = mf.getInputStream(); if (".xls".equals(ext)) { wb = new HSSFWorkbook(is); } else if (".xlsx".equals(ext)) { wb = new XSSFWorkbook(is); } else { wb = null; } } catch (FileNotFoundException e) { logger.error("FileNotFoundException", e); } catch (IOException e) { logger.error("IOException", e); } return wb; }}
service層
public Map<Integer, Map<Integer,Object>> addCustomerInfo(MultipartFile file) { Map<Integer, Map<Integer,Object>> map = new HashMap<>(); try { map = ReadExcelUtil.readExcelContentz(file); } catch (Exception e) { e.printStackTrace(); } //excel數據存在map里,map.get(0).get(0)為excel第1行第1列的值,此處可對數據進行處理}
controller層
@PostMappingpublic String add(@RequestParam("file")MultipartFile file){ Map<Integer, Map<Integer,Object>> map = customerService.addCustomerInfo(file); return "success";}
關于springboot中怎么讀取Excel文件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。