Làm cách nào để lấy dữ liệu từ Excel trong Java?
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 Show
Apache POI trong SeleniumApache 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 ExcelCá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à
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 POICác lớp và giao diện trong Apache POISau đâ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-
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 ExcelHoà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 ExcelHoà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 JXLJXL 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 Làm cách nào để lấy dữ liệu từ tệp XLSX trong Java?getLastRowNum(); . newRows) { // Tạo một Row mới trong sheet XLSX hiện có Row row = mySheet. createRow(rownum++); . lấy (chìa khóa); . objArr) { Ô ô = hàng. createCell(cellnum++);
Làm cách nào để đọc và ghi dữ liệu từ trang tính Excel trong Java?chúng ta cần tạo một đối tượng WritableWorkbook và đặt đường dẫn tệp trong Workbook. phương thức createWorkbook. Tiếp theo, chúng ta gọi phương thức createSheet để tạo một sheet mới và đặt nhãn. Cuối cùng, chúng ta cần thêm ô cho điều này, chúng ta gọi phương thức addCell và truyền đối tượng nhãn với dữ liệu mà chúng ta muốn đưa vào tệp excel
Chúng ta có thể đọc tệp Excel trong Java không?Trong Java, việc đọc tệp Excel không giống như đọc tệp Word do các ô trong tệp Excel. JDK không cung cấp API trực tiếp để đọc dữ liệu từ các tệp Excel mà chúng tôi phải chuyển sang thư viện của bên thứ ba là Apache POI .
Làm cách nào để đọc tệp Excel động trong Java?Cách đọc tệp Excel trong Java . Nhận tham chiếu của trang tính trong một đối tượng Worksheet Lấy số lượng hàng và cột dữ liệu trong trang tính Bắt đầu một vòng lặp cho các hàng Bắt đầu một vòng lặp lồng nhau cho các cột Đọc dữ liệu từ mỗi ô bằng Worksheet. getCell(). lấy (hàng Index, cột Index). phương thức getValue() |