<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 10J 高級 WebDriver – 使用 POI 從 excel 讀取數據 > 原文: [https://javabeginnerstutorial.com/selenium/10j-advanced-webdriver-reading-data-from-excel-using-poi/](https://javabeginnerstutorial.com/selenium/10j-advanced-webdriver-reading-data-from-excel-using-poi/) 朋友! 今天,讓我們深入研究 excel 表并了解如何從中讀取數據。 作為自動化和構建自動化框架的一部分,我們傾向于將數據以預定義的格式(通常遵循模板)存儲在 excel 表中。 我們存儲的數據主要是測試數據,不同的測試 URL,發布特定的參數等。在這種情況下,知道如何在我們的代碼中處理 excel 表就變得非常重要。 這將是另一篇純 Java 文章。 因此,您該喝一杯咖啡(Java)了!! 我們將使用 POI jar 來實現此目的。 ## 步驟 1: 與往常一樣,我們的第一步是下載所需的 POI JAR。 轉至 [Apache POI](https://poi.apache.org/download.html#POI-3.17) ,然后下載最新穩定版本的二進制發行版(在撰寫本文時,3.17 是最新穩定發行版)。 單擊該 zip 文件的二進制版本,重定向到實際的[下載頁面](https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.zip)。 ![POI download link](https://img.kancloud.cn/9b/45/9b455a2f8c46a20962c4177212a6a2a0_859x433.png) ## 步驟 2: 將這些 JAR 添加到我們的項目構建路徑中。 確保選擇“`poi-x.xx`”,“`ooxml-lib`”和“`lib`”文件夾下的所有 JAR。 我還將這些以及其他所有代碼文件都放在了我們的 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium/tree/master/AdvancedWebDriver/Reading%20data%20from%20excel)中。 我們之前已經多次看到這種添加 JAR 來構建路徑過程的內容,因此我沒有在重復它(有關詳細說明,請參閱此[文章](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/)的步驟 3)。 ## 步驟 3: 創建一個新類“`ExcelOperationsUsingPOI.java`”。 在此類中,讓我們有一種從特定位置讀取 excel 文件的特定圖紙的方法。 * 通過傳遞您要打開的 excel 文件的完整文件路徑來創建`File`類的對象 - `File file = new File(filePath+"\\"+fileName);` * 下一步是創建一個`FileInputStream`對象,以獲取 excel 文件的輸入字節 - `FileInputStream inputStream = new FileInputStream(file);` * 創建一個工作簿對象 - `Workbook myWorkbook = null;` * Excel 文件在大多數情況下可以具有兩個擴展名。 “`.xls`”或“`.xlsx`”。 通過使用子字符串方法拆分文件名來找到擴展名,并相應地創建`Workbook`對象。 ```java //indexOf gives the index of . in the file name //substring method splits the string starting from index of . to the end String fileExtensionName = fileName.substring(fileName.indexOf(".")); //Check condition if the file is xlsx file if(fileExtensionName.equals(".xlsx")){ //If it is xlsx file then create object of XSSFWorkbook class myWorkbook = new XSSFWorkbook(inputStream); } //Check condition if the file is xls file else if(fileExtensionName.equals(".xls")){ //If it is xls file then create object of HSSFWorkbook class myWorkbook = new HSSFWorkbook(inputStream); } ``` * 使用傳遞的確切工作表名稱,可以讀取特定工作表 - `Sheet mySheet = myWorkbook.getSheet(sheetName);` 現在,使用行和列很容易,它們的交點將為我們提供我們希望讀取的單元格內容。 現在讓我們來看一下實現到目前為止討論的全部功能的代碼, ### `ExcelOperationsUsingPOI.java` ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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; public class ExcelOperationsUsingPOI { public static void readExcel(String filePath,String fileName,String sheetName) throws IOException{ //Create a object of File class to open xlsx file File file = new File(filePath+"\\"+fileName); //Create an object of FileInputStream class to read excel file FileInputStream inputStream = new FileInputStream(file); Workbook myWorkbook = null; //Find the file extension by spliting file name in substring and getting only extension name //indexOf gives the index of . in the file name //substring method splits the string starting from index of . to the end String fileExtensionName = fileName.substring(fileName.indexOf(".")); //Check condition if the file is xlsx file if(fileExtensionName.equals(".xlsx")){ //If it is xlsx file then create object of XSSFWorkbook class myWorkbook = new XSSFWorkbook(inputStream); } //Check condition if the file is xls file else if(fileExtensionName.equals(".xls")){ //If it is xls file then create object of HSSFWorkbook class myWorkbook = new HSSFWorkbook(inputStream); } //Read sheet inside the workbook by its name Sheet mySheet = myWorkbook.getSheet(sheetName); //Find number of rows in excel file int rowCount = mySheet.getLastRowNum()- mySheet.getFirstRowNum(); //Create a loop over all the rows of excel file to read it for (int i = 0; i < rowCount+1; i++) { Row row = mySheet.getRow(i); //Create a loop to print cell values in a row for (int j = 0; j < row.getLastCellNum(); j++) { //Print excel data in console System.out.print(row.getCell(j).getStringCellValue()+"|| "); } System.out.println(); } } } ``` ### `ReadExcelData.java` 用于調用`readExcel`方法并傳遞必需的參數。 ```java import java.io.IOException; import com.blog.utility.ExcelOperationsUsingPOI; public class ReadExcelData { public static void main(String[] args) { try { ExcelOperationsUsingPOI.readExcel("E:\\Selenium", "ReadUsingPOI.xlsx", "Demographics"); } catch (IOException e) { e.printStackTrace(); } } } ``` 注釋使代碼不言自明。 所考慮的 Excel 工作表中的數據如下所示, ![Excel Sheet](https://img.kancloud.cn/0e/f1/0ef19a1f00bc46444ec056d1a968d2ac_357x287.png) 使用我們的代碼訪問此信息將按預期方式打印出所有用管道分隔的值,以便將其控制臺。 ![excel console output](https://img.kancloud.cn/22/82/22825d8854dd33bd592cc9b44d0525ed_450x177.png) 如果您想檢索代碼段,請在評論部分留言, 1. 給定條目的從零開始的行和列索引 2. 使用給定的從零開始的行和列索引的值 3. 基于給定條目的列表中的所有行值 4. 基于給定條目的列表中的所有列值 試用這些功能,讓我知道您是否遇到顛簸。 祝你今天愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看