Java cung cấp cho chúng ta các lớp khác nhau để Thao tác tệp với Selenium. Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách đọc và ghi trên tệp Excel với sự trợ giúp của gói Java IO và thư viện Apache POI
Apache POI trong Selenium
Apache POI trong Selenium là một API được sử dụng rộng rãi để thử nghiệm dựa trên dữ liệu Selenium. Đó là thư viện POI được viết bằng Java cung cấp cho người dùng API để thao tác với các tài liệu của Microsoft như. xls và. xlsx. Người dùng có thể dễ dàng tạo, chỉnh sửa và đọc/ghi vào file excel. POI là viết tắt của “Triển khai Obfuscation kém. ”
Xuất Excel
Cách xử lý tệp excel bằng POI [Phụ thuộc Maven POM]
Để đọc ghi file Excel trong Java, Apache cung cấp thư viện rất nổi tiếng POI. Thư viện này đủ khả năng để đọc và ghi cả định dạng tệp XLS và XLSX của Excel
Để đọc các tệp XLS, thư viện POI cung cấp triển khai HSSF
Để đọc XLSX, triển khai XSSF của thư viện POI sẽ là lựa chọn. Hãy nghiên cứu chi tiết những triển khai này
Nếu bạn đang sử dụng Maven trong dự án của mình, phần phụ thuộc Maven sẽ là
org.apache.poi poi 4.1.1
Hoặc bạn có thể chỉ cần tải xuống các lọ POI phiên bản mới nhất từ http. //poi. apache. tổ chức/tải xuống. html & tải xuống tệp zip mới nhất
Khi bạn tải xuống tệp zip cho tệp jar này, bạn cần giải nén nó và thêm tất cả các tệp này vào đường dẫn lớp của dự án của bạn
Các lớp và giao diện trong POI
Sau đây là danh sách các lớp và Giao diện Java khác nhau trong POI để đọc tệp XLS và XLSX-
- Sách bài tập. Các lớp XSSFWorkbook và HSSFWorkbook triển khai giao diện này
- XSSFSách bài tập. Là một đại diện lớp của tệp XLSX
- HSSFSách bài tập. Là một đại diện lớp của tệp XLS
- Tờ giấy. Các lớp XSSFSheet và HSSFSheet triển khai giao diện này
- Bảng XSSF. Là một lớp đại diện cho một trang tính trong tệp XLSX
- Bảng HSSF. Là một lớp đại diện cho một trang tính trong tệp XLS
- Chèo thuyền. Các lớp XSSFrow và HSSFrow triển khai giao diện này
- XSSFrow. Là một lớp đại diện cho một hàng trong trang tính của tệp XLSX
- HSSFHàng. Là một lớp đại diện cho một hàng trong trang tính của tệp XLS
- Tế bào. Các lớp XSSFCell và HSSFCell triển khai giao diện này
- XSSFcell. Là một lớp đại diện cho một ô trong một hàng của tệp XLSX
HSSF Tế bào. Là một lớp đại diện cho một ô trong một hàng của tệp XLS
Thao tác Đọc/Ghi-
Đối với ví dụ của chúng tôi, chúng tôi sẽ xem xét định dạng tệp Excel được cung cấp bên dưới
Đọc dữ liệu từ file Excel
Hoàn thành ví dụ. Ở đây chúng tôi đang cố đọc dữ liệu từ Excel trong 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"]; } }
Ghi chú. Chúng tôi không sử dụng khung Testng ở đây. Chạy lớp dưới dạng Ứng dụng Java bằng cách sử dụng chức năng đọc excel trong Selenium như trong ví dụ trên
Ghi dữ liệu lên file Excel
Hoàn thành ví dụ. Ở đây chúng tôi đang cố gắng ghi dữ liệu từ tệp Excel bằng cách thêm hàng mới vào tệp 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]; } }
Thao tác Excel bằng API JXL
JXL cũng là một jar nổi tiếng khác để đọc tệp Excel trong Java và ghi tệp. Ngày nay, POI được sử dụng trong hầu hết các dự án, nhưng trước POI, JXL chỉ là Java API để thao tác trên Excel. Nó là một API rất nhỏ và đơn giản để đọc excel trong Selenium
LỜI KHUYÊN. Đề xuất của tôi là không sử dụng JXL trong bất kỳ dự án mới nào vì thư viện không được phát triển tích cực từ năm 2010 và thiếu tính năng so với API POI