วิธีการอ่าน/เขียนข้อมูลใน Excel Selenium การใช้งาน Apache POI
⚡ สรุปอย่างชาญฉลาด
ไฟล์ Excel ไดรฟ์ Selenium การทดสอบที่ขับเคลื่อนด้วยข้อมูล และ Apache POI คือ Java ไลบรารีที่อ่านและเขียนข้อมูลเหล่านี้ หัวข้อด้านล่างจะกล่าวถึงการพึ่งพา คลาสหลัก ตัวอย่างการอ่านและการเขียน และ JXL
File IO เป็นส่วนสำคัญของกระบวนการซอฟต์แวร์ใดๆ เราสร้างไฟล์ เปิดและอัปเดตบางอย่างหรือลบไฟล์ในคอมพิวเตอร์ของเราบ่อยครั้ง เช่นเดียวกับกรณีของ Selenium ระบบอัตโนมัติ เราจำเป็นต้องมีกระบวนการในการจัดการไฟล์ด้วย Selenium.
Java ให้คลาสที่แตกต่างกันสำหรับการจัดการไฟล์ด้วย Selenium- ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีการอ่านและเขียน Excel ไฟล์ด้วยความช่วยเหลือของ Java แพ็คเกจ IO และ อาปาเช่ ห้องสมุดจุดที่น่าสนใจ
อาปาเช่ POI ใน Selenium
การขอ อาปาเช่ POI ใน Selenium เป็น API ที่ใช้กันอย่างแพร่หลายสำหรับ Selenium การทดสอบที่ขับเคลื่อนด้วยข้อมูลเป็นไลบรารี POI ที่เขียนด้วยภาษา... Java ที่ให้ API แก่ผู้ใช้สำหรับการจัดการ Microsoft เอกสารประเภท .xls และ .xlsx ผู้ใช้สามารถสร้าง แก้ไข และอ่าน/เขียนไฟล์ Excel ได้อย่างง่ายดาย POI ย่อมาจาก “Poor Obfuscation Implementation” (การใช้งานการปกปิดข้อมูลที่แย่)
วิธีจัดการไฟล์ Excel โดยใช้ POI (Dependency ใน Maven POM)
POI เชื่อมโยงไปยัง Selenium ทดสอบกับแบบฝึกหัดด้านล่าง
หากต้องการอ่านและเขียนไฟล์ Excel ใน JavaApache มีไลบรารี POI ที่มีชื่อเสียงมาก ไลบรารีนี้มีความสามารถในการอ่านและเขียนไฟล์ Excel ทั้งรูปแบบ XLS และ XLSX ได้อย่างมีประสิทธิภาพ
ไลบรารี POI มีการใช้งาน HSSF สำหรับอ่านไฟล์ XLS
ในการอ่านไฟล์ XLSX นั้น การใช้งาน XSSF ของไลบรารี POI จะเป็นตัวเลือกที่เหมาะสม เรามาศึกษาการใช้งานเหล่านี้อย่างละเอียดกัน
หากคุณใช้ Maven ในโปรเจ็กต์ของคุณ การเพิ่ม dependency ของ Maven จะถูกเพิ่มลงในไฟล์ pom.xml ดังแสดงด้านล่าง
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.1</version> </dependency>
หมายเหตุเกี่ยวกับเวอร์ชัน: 4.1.1 คือตัวอย่างดั้งเดิม โครงการปัจจุบันประกาศว่า poi-ooxml 5.5.1 ซึ่งเป็นเวอร์ชันที่จำเป็นสำหรับไฟล์ XLSX
หรือคุณสามารถดาวน์โหลด POI jars เวอร์ชันล่าสุดได้จาก http://poi.apache.org/download.html & ดาวน์โหลดไฟล์ zip ล่าสุด
เมื่อคุณดาวน์โหลดไฟล์ zip สำหรับ Jar นี้ คุณจะต้องแตกไฟล์และเพิ่ม Jar ทั้งหมดเหล่านี้ลงในพาธคลาสของโปรเจ็กต์ของคุณ
คลาสและอินเทอร์เฟซใน POI
ต่อไปนี้เป็นรายการที่แตกต่างกัน Java อินเทอร์เฟซและคลาสใน POI สำหรับการอ่านไฟล์ XLS และ XLSX แสดงไว้ด้านล่าง
- สมุด: คลาส XSSFWorkbook และ HSSFWorkbook ใช้ส่วนต่อประสานนี้
- XSSFWorkbook: เป็นการแสดงไฟล์ XLSX ในรูปแบบคลาส
- แบบฝึกหัด HSSF: เป็นการแสดงไฟล์ XLS ในรูปแบบคลาส
- แผ่น: คลาส XSSFSheet และ HSSFSheet ใช้ส่วนต่อประสานนี้
- XSSFSheet: เป็นคลาสที่ใช้แทนชีตในไฟล์ XLSX
- HSSFSheet: เป็นคลาสที่ใช้แทนชีตในไฟล์ XLS
- แถว: คลาส XSSFRow และ HSSFRow ใช้การใช้งานอินเทอร์เฟซนี้
- XSSFRow: คลาสที่ใช้แทนแถวในชีตของไฟล์ XLSX
- HSSFRow: คลาสนี้ใช้แทนแถวในชีตของไฟล์ XLS
- มือถือ: คลาส XSSFCell และ HSSFCell ใช้การใช้งานอินเทอร์เฟซนี้
- XSSFCell: เป็นคลาสที่ใช้แทนเซลล์ในแถวของไฟล์ XLSX
- HSSFCเซลล์: เป็นคลาสที่แสดงเซลล์ในแถวของไฟล์ XLS
อ่านเขียน Operaการ
สำหรับตัวอย่างของเรา เราจะพิจารณารูปแบบไฟล์ 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"); } }
หมายเหตุ เราไม่ได้ใช้ TestNG กรอบที่นี่ รันชั้นเรียนเป็น 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 แล้ว โดยเวอร์ชันล่าสุดคือ 2.6.12 และยังขาดฟีเจอร์ต่างๆ เมื่อเทียบกับ POI API ด้วย
ดาวน์โหลด JXL:
หากคุณต้องการทำงานกับ JXL คุณสามารถดาวน์โหลดได้จากลิงค์นี้
https://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/
คุณยังสามารถดูตัวอย่างสาธิตภายในไฟล์ซิปนี้สำหรับ JXL ได้อีกด้วย เนื่องจากตัวอย่างดังกล่าวtracเนื้อหาด้านล่างนี้แสดงรายละเอียดเพิ่มเติม
คุณสมบัติบางอย่าง:
- JXL สามารถอ่านไฟล์ Excel ได้ Selenium สำหรับสมุดงาน 95, 97, 2000, XP, 2003
- เราสามารถทำงานร่วมกับภาษาอังกฤษ ฝรั่งเศส สเปน เยอรมัน
- สามารถคัดลอกแผนภูมิและการแทรกรูปภาพใน Excel ได้
ข้อเสียเปรียบ:
- เราสามารถเขียนได้เฉพาะ Excel 97 ขึ้นไปเท่านั้น (ไม่รองรับการเขียนใน Excel 95)
- JXL ไม่รองรับไฟล์ Excel รูปแบบ XLSX
- มันสร้างสเปรดชีตในรูปแบบ Excel 2000










