XSSFWorkbook讀取Excel文件

1、導(dǎo)入jar包

 <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.17</version>
 </dependency>

2、源代碼

package service;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author cxt
 * @date 2020/7/21
 * 讀取Excel文件中的值
 */
public class DataService {

    public static void main(String[] args) throws IOException {
        redExcel("E:\\aa.xslx");
    }

    /**
     * 讀取excel內(nèi)容
     * <p>
     * 用戶模式下:
     * 弊端:對(duì)于少量的數(shù)據(jù)可以,單數(shù)對(duì)于大量的數(shù)據(jù),會(huì)造成內(nèi)存占據(jù)過大,有時(shí)候會(huì)造成內(nèi)存溢出
     * 建議修改成事件模式
     */
    public static void redExcel(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()){
            throw new Exception("文件不存在!");
        }
        InputStream in = new FileInputStream(file);
        // 讀取整個(gè)Excel
        XSSFWorkbook sheets = new XSSFWorkbook(in);
        // 獲取第一個(gè)表單Sheet
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        ArrayList<Map<String, String>> list = new ArrayList<>();

        //默認(rèn)第一行為標(biāo)題行,i = 0
        XSSFRow titleRow = sheetAt.getRow(0);
        // 循環(huán)獲取每一行數(shù)據(jù)
        for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {
            XSSFRow row = sheetAt.getRow(i);
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            // 讀取每一格內(nèi)容
            for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {
                XSSFCell titleCell = titleRow.getCell(index);
                XSSFCell cell = row.getCell(index);
                // cell.setCellType(XSSFCell.CELL_TYPE_STRING); 過期,使用下面替換
                cell.setCellType(CellType.STRING);
                if (cell.getStringCellValue().equals("")) {
                    continue;
                }
                map.put(getString(titleCell), getString(cell));
            }
            if (map.isEmpty()) {
                continue;
            }
            list.add(map);
        }
        list.forEach(System.out::println);
    }

    /**
     * 把單元格的內(nèi)容轉(zhuǎn)為字符串
     *
     * @param xssfCell 單元格
     * @return String
     */
    public static String getString(XSSFCell xssfCell) {
        if (xssfCell == null) {
            return "";
        }
        if (xssfCell.getCellTypeEnum() == CellType.NUMERIC) {
            return String.valueOf(xssfCell.getNumericCellValue());
        } else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
            return String.valueOf(xssfCell.getBooleanCellValue());
        } else {
            return xssfCell.getStringCellValue();
        }
    }
}

3、讀取結(jié)果

圖片.png

原Excel表格內(nèi)容:


圖片.png

喜歡的點(diǎn)個(gè)贊哦! @^ _ ^@

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容