Home » Java » How to read an excel in java sent from frontend in the web application?-Exceptionshub

How to read an excel in java sent from frontend in the web application?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I wanted to read cells which will be uploaded as excel in the web application.The below code works fine as I am fetching excel sheet from local .

       String filepath = "C:\Users\Abilash.J\Downloads\movies.xlsx";
       try {
           myExcelBook = new XSSFWorkbook(new FileInputStream(filepath));
       } 
       catch(Exception e)
       {
           System.err.println(e);
       }
           XSSFSheet sheet = myExcelBook.getSheetAt(0);

For getting excel from frontend i can’t specify filepath there … What Should be done if i need to read an excel sent from the frontend. In which format the frontend need to send?

How to&Answers:

Regarding frontend, you need to create a File Upload form. You will receive that file in the backend, save it to disk and then do the processing (the code snippet from your question)

Answer:

Check the XSSFWorkbook constructors.
For example using the one accepting a File object as parameter : Build a File object with the file once your application receives it. Then create XSSFWorkbook using the File object you created.