<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 從 Selenium Webdriver 中的 Excel 文件中讀取&寫入數據:POI & JXL > 原文: [https://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html](https://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html) 文件 IO 是任何軟件過程的關鍵部分。 我們經常創建文件,打開文件&或在我們的計算機中將其刪除。 Selenium Automation 也是如此。 我們需要一個使用 Selenium 處理文件的過程。 Java 為我們提供了使用 Selenium 進行文件操作的不同類。 在本教程中,我們將學習如何借助 [Java](/java-tutorial.html) IO 包和 [Apache](/apache.html) POI 庫在 [Excel](/excel-tutorials.html) 文件上進行讀寫。 * [如何使用 POI(Maven POM 依賴性)處理 excel 文件](#1) * POI 中的[類和接口](#2) * [讀/寫操作](#3) * [從 Excel 文件](#4)讀取數據 * [將數據寫入 Excel 文件](#5) * [使用 JXL API](#6) 的 Excel 操作 ## 匯出 Excel ### 如何使用 POI(Maven POM 依賴項)處理 excel 文件 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/9a/bf/9abf26315e50a4359a2cb5db0f239849_404x214.png "All About Excel in Selenium: POI & JXL") 為了讀取或編寫 Excel,Apache 提供了一個非常著名的庫 POI。 該庫足以讀取和寫入 Excel 的 **XLS** 和 **XLSX** 文件格式。 要讀取 **XLS** 文件,POI 庫提供了 **HSSF** 實現。 要讀取 **XLSX,將選擇 **POI** **庫**的** **XSSF** 實現。 讓我們詳細研究這些實現。 如果您在項目中使用 Maven,則 Maven 依賴項將是 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/77/65/7765cdf35dc175c12776a78523574e16_486x155.png "All About Excel in Selenium: POI & JXL") ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency> ``` 或者,您可以直接從[下載最新版本的 POI 罐子 http://poi.apache.org/download.html](http://poi.apache.org/download.html) &下載最新的 zip 文件 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/38/47/38476cf737a0ce76642e8d5cb21a9d2d_794x596.png "All About Excel in Selenium: POI & JXL") 當下載該 jar 的 zip 文件時,您需要將其解壓縮并將所有這些 jar 添加到項目的類路徑中。 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/e7/2a/e72ab5d07eace0e0c3fa0ab479cb1b7e_1000x686.png "All About Excel in Selenium: POI & JXL") ## POI 中的 類和接口: ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/12/a9/12a95d9c81c2469278961378f03a0966_412x226.png "All About Excel in Selenium: POI & JXL") 以下是 **POI** 中不同的 Java 接口和類的列表,用于讀取 **XLS** 和 **XLSX** 文件- * **工作簿**:XSSFWorkbook 和 HSSFWorkbook 類實現此接口。 * **XSSFWorkbook** :是 XLSX 文件的類表示。 * **HSSFWorkbook** :是 XLS 文件的類表示。 * **工作表**:XSSFSheet 和 HSSFSheet 類實現此接口。 * **XSSFSheet** :是表示 XLSX 文件中的圖紙的類。 * **HSSFSheet** :是表示 XLS 文件中的圖紙的類。 * **第**行:XSSFRow 和 HSSFRow 類實現此接口。 * **XSSFRow** :是一個類,表示 XLSX 文件工作表中的一行。 * **HSSFRow** :是表示 XLS 文件工作表中的一行的類。 * **單元格**:XSSFCell 和 HSSFCell 類實現此接口。 * **XSSFCell** :是表示 XLSX 文件行中的單元格的類。 * **HSSFCell:**是表示 XLS 文件行中單元格的類。 ## **讀/寫操作-** 對于我們的示例,我們將考慮以下給定的 Excel 文件格式 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/38/e7/38e78903819f311be2f61a9d0bfbfeaa_583x283.png "All About Excel in Selenium: POI & JXL") ## **從 Excel 文件**讀取數據 完整示例:在這里,我們嘗試從 Excel 文件中讀取數據 ``` package excelExportAndFileIO; 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 ReadGuru99ExcelFile { public void readExcel(String filePath,String fileName,String sheetName) throws IOException{ //Create an 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 guru99Workbook = null; //Find the file extension by splitting file name in substring and getting only extension name 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 guru99Workbook = 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 guru99Workbook = new HSSFWorkbook(inputStream); } //Read sheet inside the workbook by its name Sheet guru99Sheet = guru99Workbook.getSheet(sheetName); //Find number of rows in excel file int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum(); //Create a loop over all the rows of excel file to read it for (int i = 0; i < rowCount+1; i++) { Row row = guru99Sheet.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(); } } //Main function is calling readExcel function to read data from excel file public static void main(String...strings) throws IOException{ //Create an object of ReadGuru99ExcelFile class ReadGuru99ExcelFile objExcelFile = new ReadGuru99ExcelFile(); //Prepare the path of excel file String filePath = System.getProperty("user.dir")+"\\src\\excelExportAndFileIO"; //Call read file method of the class to read data objExcelFile.readExcel(filePath,"ExportExcel.xlsx","ExcelGuru99Demo"); } } ``` 注意:此處我們不使用 [Testng](/all-about-testng-and-selenium.html) 框架。 將類作為 Java 應用程序運行 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/3c/99/3c991996eb3109409e9334bfe1b9b8bb_501x182.png "All About Excel in Selenium: POI & JXL") ## 將數據寫入 Excel 文件 完整示例:在這里,我們嘗試通過在 Excel 文件中添加新行來從 Excel 文件寫入數據 ``` package excelExportAndFileIO; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 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; public class WriteGuru99ExcelFile { public void writeExcel(String filePath,String fileName,String sheetName,String[] dataToWrite) throws IOException{ //Create an 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 guru99Workbook = null; //Find the file extension by splitting file name in substring and getting only extension name 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 guru99Workbook = 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 XSSFWorkbook class guru99Workbook = new HSSFWorkbook(inputStream); } //Read excel sheet by sheet name Sheet sheet = guru99Workbook.getSheet(sheetName); //Get the current count of rows in excel file int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum(); //Get the first row from the sheet Row row = sheet.getRow(0); //Create a new row and append it at last of sheet Row newRow = sheet.createRow(rowCount+1); //Create a loop over the cell of newly created Row for(int j = 0; j < row.getLastCellNum(); j++){ //Fill data in row Cell cell = newRow.createCell(j); cell.setCellValue(dataToWrite[j]); } //Close input stream inputStream.close(); //Create an object of FileOutputStream class to create write data in excel file FileOutputStream outputStream = new FileOutputStream(file); //write data in the excel file guru99Workbook.write(outputStream); //close output stream outputStream.close(); } public static void main(String...strings) throws IOException{ //Create an array with the data in the same order in which you expect to be filled in excel file String[] valueToWrite = {"Mr. E","Noida"}; //Create an object of current class WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile(); //Write the file using file name, sheet name and the data to be filled objExcelFile.writeExcel(System.getProperty("user.dir")+"\\src\\excelExportAndFileIO","ExportExcel.xlsx","ExcelGuru99Demo",valueToWrite); } } ``` ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/5a/53/5a533de99d4c4ea46513799598945fd1_543x225.png "All About Excel in Selenium: POI & JXL") ## 使用 JXL API 的 Excel 操作 ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/71/b3/71b37254930a4ea93f34e79a19abdc3c_624x99.png "All About Excel in Selenium: POI & JXL") JXL 還是另一個著名的讀取 Excel 文件的 jar。 現在,大多數項目都使用一天的 POI,但是在 POI 之前,JXL 只是用于 Excel 操作的 Java API。 這是一個非常小而簡單的 API。 提示:*我的建議是不要在任何新項目中使用 JXL,因為該庫從 2010 年開始就沒有積極開發,并且與 POI API 相比缺少該功能。* 下載 JXL: 如果要使用 JXL,可以從此鏈接下載 [https://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/](https://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/) ![Read & Write Data from Excel File in Selenium Webdriver: POI & JXL](https://img.kancloud.cn/c1/42/c1420bca64b5a6db1aa28f1f75579d68_624x289.png "All About Excel in Selenium: POI & JXL") 您還可以在 Jzip 的壓縮文件中獲得演示示例。 一些功能: * JXL 能夠閱讀 Excel 95、97、2000,XP,2003 工作簿。 * 我們可以使用英語,法語,西班牙語,德語。 * 可以復制圖表并在 Excel 中插入圖像 退稅: * 我們只能編寫 Excel 97 和更高版本(不支持在 Excel 95 中編寫)。 * JXL 不支持 Excel 文件的 XLSX 格式。 * 它生成 Excel 2000 格式的電子表格。 **摘要:** * 可以通過 Java IO 操作讀取 Excel 文件。 為此,我們需要使用 **Apache POI Jar** 。 * Excel 文件中有兩種工作簿,即 **XLSX** 和 **XLS** 文件。 * POI 具有不同的 Interfaces Workbook,Sheet,Row,Cell。 * 這些接口由相應的 **XLS** ( **HSSFWorkbook,HSSFSheet,HSSFRow,HSSFCell** )和 **XLSX** ( **XSSFWorkbook,XSSFSheet,XSSFRow,XSSFCell** )實現 )文件操作類。 * JXL 是另一個用于 Excel 操作的 API。 * JXL 無法使用 Excel 的 XLSX 格式。
                  <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>

                              哎呀哎呀视频在线观看