java poi读取 excel(xls/xlsx)包含引用jar包下载

jar包下载(c4by)

代码:

 1 package com.util;
 2 import java.io.FileInputStream;
 3 import java.io.FileOutputStream;
 4 import java.io.InputStream;
 5 import java.io.OutputStream;
 6 
 7 import org.apache.poi.xssf.usermodel.XSSFCell;
 8 import org.apache.poi.xssf.usermodel.XSSFRow;
 9 import org.apache.poi.xssf.usermodel.XSSFSheet;
10 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
11 /**
12  * @auther 周博
13  * @date 2021/5/8 19:07
14  * @Blogurl https://www.cnblogs.com/smartisn/
15  */
16 public class Read_EXCEL {
17     private XSSFSheet sheet;
18 
19     /**
20      * 构造函数,初始化excel数据
21      * @param filePath  excel路径
22      * @param sheetName sheet表名
23      */
24     Read_EXCEL(String filePath,String sheetName){
25         FileInputStream fileInputStream = null;
26         try {
27             fileInputStream = new FileInputStream(filePath);
28             XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
29             //获取sheet
30             sheet = sheets.getSheet(sheetName);
31         } catch (Exception e) {
32             e.printStackTrace();
33         }
34     }
35 
36     /**
37      * 根据行和列的索引获取单元格的数据
38      * @param row
39      * @param column
40      * @return
41      */
42     public String getExcelDateByIndex(int row,int column){
43         XSSFRow row1 = sheet.getRow(row);
44         String cell = row1.getCell(column).toString();
45         return cell;
46     }
47 
48     /**
49      * 根据某一列值为“******”的这一行,来获取该行第x列的值
50      * @param caseName
51      * @param currentColumn 当前单元格列的索引
52      * @param targetColumn 目标单元格列的索引
53      * @return
54      */
55     public String getCellByCaseName(String caseName,int currentColumn,int targetColumn){
56         String operateSteps="";
57         //获取行数
58         int rows = sheet.getPhysicalNumberOfRows();
59         for(int i=0;i<rows;i++){
60             XSSFRow row = sheet.getRow(i);
61             String cell = row.getCell(currentColumn).toString();
62             if(cell.equals(caseName)){
63                 operateSteps = row.getCell(targetColumn).toString();
64                 break;
65             }
66         }
67         return operateSteps;
68     }
69 
70     //打印excel数据
71     public void readRead_EXCEL(){
72         //获取行数
73         int rows = sheet.getPhysicalNumberOfRows();
74         for(int i=0;i<rows;i++){
75             //获取列数
76             XSSFRow row = sheet.getRow(i);
77             int columns = row.getPhysicalNumberOfCells();
78             for(int j=0;j<columns;j++){
79                 String cell = row.getCell(j).toString();
80                 System.out.println(cell);
81             }
82         }
83     }
84 
85     //测试方法
86     public static void main(String[] args){
87         Read_EXCEL sheet1 = new Read_EXCEL("E:\\A兴趣代码\\JAR\\读取EXCEL\\党徽.xls", "Sheet1");
88         //获取第二行第4列
89         String cell2 = sheet1.getExcelDateByIndex(1, 1);
90         //根据第3列值为“customer23”的这一行,来获取该行第2列的值
91         String cell3 = sheet1.getCellByCaseName("名字", 0,0);
92         System.out.println(cell2);
93         System.out.println(cell3);
94     }
95 
96 }

结果:

 

posted @ 2021-05-08 20:24  博二爷  阅读(861)  评论(0编辑  收藏  举报