如何从 Excel 文件读取/写入数据: Selenium POI
文件 IO 是任何软件流程的关键部分。我们经常在计算机中创建文件、打开文件并更新或删除文件。 Selenium 自动化。我们需要一个流程来处理文件 Selenium.
Java 为我们提供不同的文件操作类 Selenium在本教程中,我们将学习如何在 Excel 文件的帮助下 Java IO 包和 阿帕奇 POI 库。
Apache POI Selenium
这个 Apache POI Selenium 是 selenium 数据驱动测试中广泛使用的 API。它是一个用 selenium 编写的 POI 库 Java 它为用户提供了操作 API Microsoft .xls 和 .xlsx 等文档。用户可以轻松创建、修改和读取/写入 excel 文件。POI 代表“Poor Obfuscation Implementation(糟糕的混淆实施)”。
导出 Excel
如何使用 POI(Maven POM 依赖项)处理 Excel 文件
读写 Excel 文件 JavaApache 提供了一个非常著名的库 POI。这个库足以读写 XLS 和 XLSX Excel 文件格式。
读书 XLS 文件, 高速SF 实现由 POI 库提供。
读书 XLSX, 跨系统安全框架 实施 POI 图书馆 将是选择。让我们详细研究一下这些实现。
如果你在项目中使用 Maven,那么 Maven 依赖项将是
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency>
或者你也可以直接下载最新版本的 POI jars http://poi.apache.org/download.html & 下载最新的 zip 文件
当您下载此 jar 的 zip 文件时,您需要解压缩它并将所有这些 jar 添加到项目的类路径中。
POI 中的类和接口

以下是不同的列表 Java 接口和类 POI 阅读 XLS 和 XLSX 文件-
- 工作簿:XSSFWorkbook 和 HSSFWorkbook 类实现了该接口。
- XSSF工作簿:是XLSX文件的类表示。
- HSSF工作簿:是XLS文件的类表示。
- 片:XSSFSheet 和 HSSFSheet 类实现了此接口。
- XSSF表:是表示 XLSX 文件中工作表的类。
- HSSF板材:是表示 XLS 文件中工作表的类。
- 行:XSSFRow 和 HSSFRow 类实现了此接口。
- 行距:是表示 XLSX 文件表中某一行的类。
- 高速钢排:是表示 XLS 文件工作表中某一行的类。
- 手机:XSSFCell 和 HSSFCell 类实现此接口。
- 星火:是表示 XLSX 文件某一行中单元格的类。
- [0177] HSSFCell: 是一个代表 XLS 文件某一行中单元格的类。
读/写操作
为了举个例子,我们将考虑下面给出的 Excel 文件格式
从 Excel 文件读取数据
完整示例:我们尝试从 Excel 中读取数据 Selenium:
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");
}
}
注意:我们不使用 测试 框架。运行类 Java 使用函数读取 Excel 中的应用程序 Selenium 如上例所示。
在 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);
}
}
使用 JXL API 进行 Excel 操作
JXL 也是另一个著名的用于读取 Excel 文件的 jar Java 和写入文件。如今,POI 已用于大多数项目,但在 POI 之前,JXL 仅用于 Java Excel 操作 API。这是一个非常小巧简单的 Excel 读取 API Selenium.
提示:我的建议不要在任何新项目中使用 JXL,因为该库自 2010 年以来就没有积极开发过,并且与 POI API 相比缺少功能。
下载JXL:
如果你想使用 JXL,你可以从此链接下载
https://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/
您还可以在此压缩文件内获取 JXL 的演示示例。
一些功能:
- JXL 能够读取 Excel 文件 Selenium 适用于 95、97、2000、XP、2003 工作簿。
- 我们可以使用英语、法语、西班牙语、德语。
- 可以复制图表并在 Excel 中插入图像
退税:
- 我们只能写入 Excel 97 及更高版本(不支持写入 Excel 95)。
- JXL不支持XLSX格式的excel文件。
- 它生成 Excel 2000 格式的电子表格。
结语
- Excel 文件可以通过 Java IO 操作。为此,我们需要使用 Apache POI Jar.
- Excel 文件中有两种工作簿, XLSX 和 XLS 文件。
- POI 有不同的界面工作簿、工作表、行、单元格。
- 这些接口由相应的 XLS (HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell) 以及 XLSX (XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell) 文件操作类。
- JXL 是另一个用于 Excel 处理的 API Selenium.
- JXL 无法与 Excel 的 XLSX 格式一起使用。









