在公司做项目的时候用到了读取Excel的工具,从网上找了个工具类自己又根据需要改了改。上传一下以备下次使用

      <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
      </dependency>
public class ExcelReader {
    private XSSFSheet sheet;
    ExcelReader(String filePath,String sheetName){
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            //获取sheet
            sheet = sheets.getSheet(sheetName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据行和列的索引获取单元格的数据
     * @param row
     * @param column
     * @return
     */
    public String getExcelDateByIndex(int row,int column){
        XSSFRow row1 = sheet.getRow(row);
        String cell = row1.getCell(column).toString();
        return cell;
    }

    /**
     * 根据某一列值为“******”的这一行,来获取该行第x列的值
     * @param caseName
     * @param currentColumn 当前单元格列的索引
     * @param targetColumn 目标单元格列的索引
     * @return
     */
    public String getCellByCaseName(String caseName,int currentColumn,int targetColumn){
        String operateSteps="";
        //获取行数
        int rows = sheet.getPhysicalNumberOfRows();
        for(int i=0;i<rows;i++){
            XSSFRow row = sheet.getRow(i);
            String cell = row.getCell(currentColumn).toString();
            if(cell.equals(caseName)){
                operateSteps = row.getCell(targetColumn).toString();
                break;
            }
        }
        return operateSteps;
    }
/**
* #Description 获取一个方格内所有的数据
* @param
* @return
* @author Wu Xian
* #Date 2021/6/20
*/
    public List<List<String>> getCellBox(int beginRow,int endRow,int beginColumn,int endRowColumn){
        List<List<String>> rows = new ArrayList<>();
        for(int i = beginRow;i<=endRow;i++){
            XSSFRow row = sheet.getRow(i);
            List<String> cells = new ArrayList<>();
            for(int j = beginColumn;j<=endRowColumn;j++){
                String cell = row.getCell(j).toString();
                cells.add(cell);
            }
            rows.add(cells);
        }
        return rows;
    }
/**
* #Description 获取一个指定列区间从指定一行到最后的数据
* @param        
* @return 
* @author Wu Xian
* #Date 2021/6/21
*/
    public List<List<String>> getCellBox(int beginRow,int beginColumn,int endRowColumn){
        List<List<String>> rows = new ArrayList<>();
        int i = beginRow;
        while(!sheet.getRow(i).getCell(0).toString().equals("")){
            XSSFRow row = sheet.getRow(i);
            List<String> cells = new ArrayList<>();
            for(int j = beginColumn;j<=endRowColumn;j++){
                String cell = row.getCell(j).toString();
                cells.add(cell);
            }
            rows.add(cells);
            i++;
        }
        return rows;
    }
}