這篇文章將為大家詳細(xì)講解有關(guān)springboot中怎么讀取Excel文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)是專業(yè)的米林網(wǎng)站建設(shè)公司,米林接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行米林網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
首先引入相關(guān)依賴
<!--解析office相關(guān)文件--> <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相關(guān)文件-->
工具類
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); // 得到總行數(shù) int rowNum = sheet.getLastRowNum(); Row row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文內(nèi)容應(yīng)該從第二行開始,第一行為表頭的標(biāo)題 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; } //根據(jù)Cell類型設(shè)置數(shù)據(jù) 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數(shù)據(jù)存在map里,map.get(0).get(0)為excel第1行第1列的值,此處可對數(shù)據(jù)進(jìn)行處理}
controller層
@PostMappingpublic String add(@RequestParam("file")MultipartFile file){ Map<Integer, Map<Integer,Object>> map = customerService.addCustomerInfo(file); return "success";}
關(guān)于springboot中怎么讀取Excel文件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
標(biāo)題名稱:springboot中怎么讀取Excel文件
標(biāo)題來源:http://chinadenli.net/article4/pigjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站內(nèi)鏈、營銷型網(wǎng)站建設(shè)、網(wǎng)站排名、動態(tài)網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)