วิธีอ่าน/เขียนข้อมูลจากไฟล์ Excel: Selenium จุดที่น่าสนใจ
File IO เป็นส่วนสำคัญของกระบวนการซอฟต์แวร์ใดๆ เราสร้างไฟล์ เปิดและอัปเดตบางอย่างหรือลบไฟล์ในคอมพิวเตอร์ของเราบ่อยครั้ง เช่นเดียวกับกรณีของ Selenium ระบบอัตโนมัติ เราจำเป็นต้องมีกระบวนการในการจัดการไฟล์ด้วย Selenium.
Java ให้คลาสที่แตกต่างกันสำหรับการจัดการไฟล์ด้วย Selenium- ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีการอ่านและเขียน Excel ไฟล์ด้วยความช่วยเหลือของ Java แพ็คเกจ IO และ อาปาเช่ ห้องสมุดจุดที่น่าสนใจ
อาปาเช่ POI ใน Selenium
เทศกาล อาปาเช่ POI ใน Selenium เป็น API ที่ใช้กันอย่างแพร่หลายสำหรับการทดสอบที่ขับเคลื่อนด้วยข้อมูลเซเลเนียม เป็นไลบรารี POI ที่เขียนด้วย Java ที่ให้ API แก่ผู้ใช้สำหรับการจัดการ Microsoft เอกสารเช่น .xls และ .xlsx ผู้ใช้สามารถสร้าง แก้ไข และอ่าน/เขียนลงในไฟล์ Excel ได้อย่างง่ายดาย POI ย่อมาจาก “การดำเนินการสร้างความสับสนที่ไม่ดี”
การส่งออก Excel
วิธีจัดการไฟล์ Excel โดยใช้ POI (การพึ่งพา Maven POM)
หากต้องการอ่านและเขียนไฟล์ Excel ใน Java, Apache มี POI ห้องสมุดที่มีชื่อเสียงมาก ห้องสมุดนี้สามารถอ่านและเขียนได้ทั้งสองอย่าง xls และ XLSX รูปแบบไฟล์ของ Excel
อ่าน xls ไฟล์, ไฟล์ HSSF การใช้งานมีให้โดยห้องสมุด POI
อ่าน XLSX, XSSF การใช้งาน จุดที่น่าสนใจ ห้องสมุด จะเป็นทางเลือก มาศึกษาการใช้งานเหล่านี้โดยละเอียด
หากคุณใช้ 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 ล่าสุด
เมื่อคุณดาวน์โหลดไฟล์ zip สำหรับ Jar นี้ คุณจะต้องแตกไฟล์และเพิ่ม Jar ทั้งหมดเหล่านี้ลงในพาธคลาสของโปรเจ็กต์ของคุณ
คลาสและอินเทอร์เฟซใน POI
ต่อไปนี้เป็นรายการที่แตกต่างกัน Java อินเทอร์เฟซและคลาสใน จุดที่น่าสนใจ สำหรับการอ่าน xls และ XLSX เนื้อ-
- สมุดงาน: คลาส XSSFWorkbook และ HSSFWorkbook ใช้อินเทอร์เฟซนี้
- XSSFสมุดงาน: เป็นตัวแทนคลาสของไฟล์ XLSX
- HSSFสมุดงาน: เป็นตัวแทนคลาสของไฟล์ XLS
- แผ่น: คลาส XSSFSheet และ HSSFSheet ใช้อินเทอร์เฟซนี้
- XSSFSheet: เป็นคลาสที่แสดงแผ่นงานในไฟล์ XLSX
- HSSFSheet: เป็นคลาสที่แสดงแผ่นงานในไฟล์ XLS
- แถว: คลาส XSSFRow และ HSSFRow ใช้อินเทอร์เฟซนี้
- XSSFแถว: เป็นคลาสที่แสดงแถวในชีตของไฟล์ XLSX
- HSSFแถว: เป็นคลาสที่แสดงแถวในชีตของไฟล์ XLS
- เซลล์: คลาส XSSFCell และ HSSFCell ใช้อินเทอร์เฟซนี้
- XSSFCเซลล์: เป็นคลาสที่แสดงเซลล์ในแถวของไฟล์ XLSX
- HSSFCเซลล์: เป็นคลาสที่แสดงเซลล์ในแถวของไฟล์ 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); } }
การจัดการ Excel โดยใช้ JXL API
JXL เป็นอีกหนึ่งขวดที่มีชื่อเสียงในการอ่านไฟล์ Excel Java และการเขียนไฟล์ ปัจจุบัน POI ถูกใช้ในโครงการส่วนใหญ่ แต่ก่อน POI JXL เป็นเพียงเท่านั้น Java API สำหรับการจัดการ Excel เป็น API ขนาดเล็กและเรียบง่ายสำหรับการอ่าน Excel 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 ไม่รองรับไฟล์ Excel รูปแบบ XLSX
- มันสร้างสเปรดชีตในรูปแบบ Excel 2000
สรุป
- สามารถอ่านไฟล์ Excel ได้ Java การทำงานของ IO เราต้องใช้ โถ Apache POI.
- เวิร์กบุ๊กในไฟล์ Excel มีสองประเภท XLSX และ xls ไฟล์
- POI มีสมุดงานอินเทอร์เฟซ แผ่นงาน แถว และเซลล์ที่แตกต่างกัน
- อินเทอร์เฟซเหล่านี้ถูกนำมาใช้โดยสอดคล้องกัน xls (HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell) and XLSX (XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell) คลาสการจัดการไฟล์
- JXL เป็นอีกหนึ่ง API สำหรับการจัดการ Excel Selenium.
- JXL ไม่สามารถทำงานกับรูปแบบ XLSX ของ Excel ได้