Hello Raghav Pal. I just watched your video "How to read excel in Java" and I'm overwhelmed. Malheureusement je suis française et cela a été très dur pour moi de vous suivre. Mais encore une fois Merci Mille fois Merci
Hi Raghav Thanks for the video.. As usual explained step by step.. How Can we read/write excel available on onedrive in selenium.. Please help me with that
Hi Monill, Yes, you can use Java or Python to read and write Excel files on OneDrive. For java you can do by using the Microsoft Graph API and OpenXML library. Here are the steps to do this: Authenticate your OneDrive account: You can use the OAuth 2.0 protocol to authenticate your OneDrive account. You can use the MSAL Java library to implement the authentication process. Access the OneDrive API: Once you are authenticated, you can use the Microsoft Graph API to access the OneDrive API. You can use the Microsoft Graph Java SDK to interact with the API. Use the OpenXML library: To read and write Excel files in Java, you can use the OpenXML library. This library provides a simple interface to work with Excel files and can be used to read and write data to Excel files. Here's a sample code in Java to read an Excel file from OneDrive using Selenium and OpenXML: import com.microsoft.aad.msal4j.ConfidentialClientApplication; import com.microsoft.aad.msal4j.IAuthenticationResult; import com.microsoft.graph.authentication.TokenCredentialAuthProvider; import com.microsoft.graph.models.DriveItem; import com.microsoft.graph.models.Workbook; import com.microsoft.graph.models.WorkbookRange; import com.microsoft.graph.requests.DriveItemContentRequestBuilder; import com.microsoft.graph.requests.WorkbookRequestBuilder; import com.microsoft.graph.requests.WorkbookWorksheetCollectionRequestBuilder; import com.microsoft.graph.requests.WorkbookWorksheetRangeRequestBuilder; import com.microsoft.graph.requests.WorkbookWorksheetRequestBuilder; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 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.WorkbookFactory; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class OneDriveExcelReader { public static void main(String[] args) { // Define the OneDrive credentials String clientId = "your_client_id"; String clientSecret = "your_client_secret"; String tenantId = "your_tenant_id"; String authority = "login.microsoftonline.com/" + tenantId; String[] scopes = {"graph.microsoft.com/.default"}; // Define the Excel file details String fileUrl = "graph.microsoft.com/v1.0/me/drive/root:/Documents/Excel/file.xlsx:/content"; try { // Authenticate the OneDrive account ConfidentialClientApplication app = ConfidentialClientApplication.builder(clientId, clientSecret) .authority(authority) .build(); IAuthenticationResult result = app.acquireTokenSilent(scopes).join(); if (result == null) { result = app.acquireToken(scopes).join(); } // Authenticate the Graph SDK with the access token TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(result.accessToken()); // Access the OneDrive API and get the Excel file DriveItemContentRequestBuilder contentRequestBuilder = new DriveItemContentRequestBuilder(fileUrl, null); InputStream excelInputStream = contentRequestBuilder.buildRequest(authProvider).get(); // Load the Excel file using OpenXML org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(excelInputStream); Sheet sheet = workbook.getSheetAt(0); // Print the contents of the Excel file for (Row row : sheet) { for (Cell cell : row) { System.out.print(cell.toString() + "\t"); } System.out.println(); } } catch (Exception ex) { ex.printStackTrace(); } } }
@@RaghavPal Hi Raghav thank you for quick response.. I tried this earlier before posting but it was difficult.. Yesterday after your message I tried to implement this but it gives me compile time error for acquireTokenSilent as required type SilentParameters provided string[] and many more compile time errors.. Can you suggest or create a video which shows this line by line I tried some videos and microsoft docs to setup Microsoft auth
Nice job, i just want to know if there's a documentation for this method. because i'm writing my end of study memory about massive data import with Java. Thanks in advance
Hi Petya Yes, you can use the CSV format to import data from Excel into a DTO. Here are the steps on how to do it: 1. Open the CSV file in a text editor. 2. Identify the column names in the CSV file. 3. Create a DTO class with the same column names as the CSV file. 4. Use the `CSVReader` class to read the data from the CSV file into a List of objects of the DTO class. 5. Use the `List` of objects to populate the DTO object in your application. Here is an example of how to import data from CSV file into a DTO in Java: ```java import java.io.FileReader; import java.io.IOException; import java.util.List; import com.opencsv.CSVReader; public class CSVImporter { public static void main(String[] args) throws IOException { // Create a CSVReader object. CSVReader reader = new CSVReader(new FileReader("data.csv")); // Skip header. reader.readNext(); // Read data from CSV file into a List of objects. List products = reader.readAll(); // Iterate over the List of products and print them to the console. for (Product product : products) { System.out.println(product); } } } class Product { private String id; private String name; private double price; public Product(String id, String name, double price) { this.id = id; this.name = name; this.price = price; } public String getId() { return id; } public String getName() { return name; } public double getPrice() { return price; } } ``` I hope this helps
Hi Raghav, This video is very informative but I am facing problem when I have mix values of integer and string. Here only string type values can be read but when I have a column of int type then it is giving error.
Hi Jerin, you can get the row count and then use loops like for or while loop for the iterations equal to the row count. This is an example. You can use loops based on your need
Hi Raghav , Thank you for this excellent tutorial . Just a question - there are two types of jars 1) poi , 2) poi-ooxml . And in which case we should use what. Also to read from .csv file is there any java library like poi ??
Hi Kundan, Apache POI contains HSSF implementation for Excel ’97(-2007) file format i.e XLS. Apache POI XSSF implementation should be used for Excel 2007 OOXML (.xlsx) file format. stackoverflow.com/questions/60217698/what-is-the-difference-between-poi-and-poi-ooxml For CSV you can use openCSV api stackabuse.com/libraries-for-reading-and-writing-csvs-in-java/
Hii bro, ur class is very nice. Well explained but I need to implement in gradle instead of maven could u pls provided me vedio link for gradle impkentation.
@@RaghavPal hii bro have one doubt I am able to read the data from Excel file in stand alone but I need to push the code in git pipeline once we pushed the code into git. How it will take excel path bro since our excel path is system dependent right? How I need to give excel path for that bro?
hi Raghav ...ur videos are so helpfull as its turning me from non tech to tech .... Upon trying to create in Excel in Java ..I am having a prob ...Please can you help me.... My Question is when I m adding dependencies poi-ooxml is coming in Red underline .... not sure Y ...I ignored it and moved ahead but when I am entering 'XSSFW' and pressing Ctrl+Spacebar .... I am not able to see drop down list ...its showing me "NO default Proposal" .... Please can you help me.
Hi raghav sir Please let me know that A row is having some empty cells, how to read those cells any idea you suggest me to read the same empty cell and store the same thing in the database I hope you have answer Waiting for your answer
Hi raghav sir Please let me know Example we are having a serial number column in Excel sheet, which is first column .now we are having one serial number which takes it and updates the same thing, how to do without a serial number need to store the serial number in the database
@@RaghavPal will need to create code for this and how to ignore some cells in the Excel sheet and please create a video for reading the CSV file and storing in the db
Hi, You will need to check a java lib for that, I believe Apache POI does not support it yet stackoverflow.com/questions/14282164/exception-reading-xlsb-file-apache-poi-java-io-charconversionexception
@@RaghavPal thanks Raghav.. That helped. I am able to read xlsb by using the given example shared on that forum. But still struggling with writing on xlsb. Converting xlsb to other option like xlsx is not valid in my case .. as xlsb is having some macro inside.. it would get lost if i do any file conversion. Any suggestion how i can write in xlsb file in java?
Hi Raghav, I definitely appreciate your awesome work. Your explanation is very nice. are you planning to upload video for writing on the same excel file where it reads from? Also, if there is a scenario like - I read data from excel and use it on an web application, then it generate a case number, then write the case number in the same excel file , and read that case number again from the excel file to do next step in the web application. Please advice. Thanks
Hi Mario, testing is a wide field. I will suggest you can learn the tools and automation testing as per needs, CI, CD DevOps, RPA so many options are there and future is good
At 3:40 you say you'll "Correct the identation?" I think? What does that mean? Why are we doing it? When I click Ctrl+A and Ctrl+I it doesn't seem to do anything. More importantly, are you just clicking Ctrl+S to save? Because that also does not work for me. When I save either from the File drop-down or by clicking Ctrl+S, it does not give me Maven dependencies on the left.
I don't know if you're still reading this.... but when I try to run the ExcelUtils class for the first time, I get the following error: Error: Main class utils.ExcelUtils could not be found or loaded. Cause: java.lang.ClassNotFoundException: utils.ExcelUtils how can i fix this?
Hi Raghav, I am not able to add the POI dependency, it is throwing an error saying no such dependency found. Could you please suggest me how to resolve this issue?
Hi Gayatri, pls check if you have added the right dependency in pom.xml and have saved your project after that, You can check if its available in Maven Dependencies folder
Hi raghav I have question Scenario like this: 1. Your Business Analyst has asked to write an automation script which can search for iPhones having a maximum price of INR 40,000 on Flipkart website. 2. The automation script should be capable of retrieving the device model with storage capacity and customer ratings. 3. The information retrieved should be saved in a CSV file where data is sorted by Price in ascending order. Plz provide solution. I am waiting for it.
@@RaghavPal I tried many times now I need help not getting clearity about ₹40,000 Used split and then if condition And I can not compaire price But I face difficulties Please do video on this and explain as early as possible I am waiting
Hi Sai, pls check if you see Caused By section in your logs. A diff version of lib can work, Check community.smartbear.com/t5/SoapUI-Open-Source/ERROR-org-apache-poi-POIXMLException-java-lang-reflect/m-p/143361#M24280
Not sure if I got you completely, In simple words, You can keep a folder within your project to keep all your excel files and in the code provide a relative path to this folder. So it will work even if you take this on a new system. You have to ensure that the excel files should be in that folder
I am having doubt but not on this topic. How to trim double quotes string that should print on excel in java. Not looking for console output. Example String s="aacfvbbgvv" ; s = s. replaceAll("[^a-zA-Z0-9]", "") ; System. Out. Println(s) ; This is coming on console. Let me know to print in excel from properties file
Hi Saleha, pls check this stackoverflow.com/questions/2608665/how-can-i-trim-beginning-and-ending-double-quotes-from-a-string www.codegrepper.com/code-examples/css/java+remove+double+quotes+from+string
@16.40 I tried the same thing and it works with the try-catch method. But as soon as I try to remove the try-catch block, I am getting an IO exception error on the line: XSSFWorkbook workbook = new XSSFWorkbook(excelpath); Any ideas why this could be happening
Hello Raghav, I am trying to do validation. My execution inputs first statement and enters 'for' loop and 'if' loop but does not perform any action. What am I missing ? Any suggestion please public void getOrganizationName() throws Exception { elementActions.doSendKeysUsingByLocator(OrganizationName, readExcelData.getCellDataString("Sheet1", 0, 0)); for (int i = 0; i
Hi Sai, Try to print, what you get fore entering for loop. Also print the values inside for loop, Remove break and check again Can check with break points and debugging.
@@RaghavPal I tried inserting print statements but it is failing to load 2nd row from excel sheet. public void getOrganizationName() throws IOException { JSUtil.flashby(OrganizationName, driver, 16); String data = readExcelData.getCellDataString("Organizations", 0, 0); System.out.println(data); //PRINTS FIRST ROW elementActions.doSendKeysUsingByLocator(OrganizationName, readExcelData.getCellDataString("Organizations", 0, 0)); for (int i = 1; i
You missed one more thing in the ExcelUtils class That is nothing but Return type of getRowCount() Return type of getCellData() You could have made return type as directly int and Object
As I run on local I have no issues but when I upload to Azure DevOps and run remotely, build failed with below errors. I was wondering why Azure DevOps failing to download the packages? Any suggestion please! [INFO] Compiling 32 source files to d:\a\1\s\ExceleRATE\target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[8,35] package org.apache.poi.ss.usermodel does not exist [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[9,37] package org.apache.poi.xssf.usermodel does not exist [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[10,37] package org.apache.poi.xssf.usermodel does not exist [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[18,9] cannot find symbol symbol: class XSSFWorkbook location: class com.qa.ExceleRATE.util.ReadExcelData [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[20,9] cannot find symbol symbol: class XSSFSheet location: class com.qa.ExceleRATE.util.ReadExcelData [ERROR] /e:/a/1/s/ExceleRATE/src/main/java/com/qa/ExceleRATE/util/ReadExcelData.java:[31,34] cannot find symbol symbol: class XSSFWorkbook location: class com.qa.ExceleRATE.util.ReadExcelData [INFO] 6 errors
@@RaghavPal Hey, im trying to edit a cell. this is my code and it doesnt work. do you know why? String path = "./data/test.xlsx"; XSSFWorkbook workbook = new XSSFWorkbook(path); XSSFSheet sheet = workbook.getSheet("one"); Row theRow = sheet.getRow(i); theRow.getCell(j).setCellValue("YES"); i and j are numbers
I am getting error please help me to resolve it "Could not find or load main class utils.Excelutils Caused by: java. lang.ClassNotfoundException: utils.Excelutils I am stuck on this error & unable to compile & run the code . Please someone help quickly. 🙏🏻🙏🏻
Hi Gopal, looks like the right libraries are not added. Just check after adding the dependencies in your pom.xml, save project and check in Maven Dependencies folder if you find the jars
@@RaghavPal okk sir thank you for reply . I was detect the problem and resolve that . The problem was you say to use new version of dependencies , I use 5.1.0 it can't work . Then , I see in your video you use 4.2.1 version of dependencies , it try it and works successfully.
Hey hope you are doing alright just I wanna say that GOD loved the world so much he sent his only begotten son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply trusting in Jesus, confessing that GOD raised him from the dead, turning away from your sins and forming a relationship with GOD.