How to read excel file with restful in spring boot

1. Getting Apache POI library

2. Program to Read an excel file using Apache POI

import com.example.test.readfileexcel.service.Service;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@author Mhosein-abbasi 11/9/21
public class Controller {
private final Service service;

public Controller(Service service) {
this.service = service;

public String upload(
@RequestParam MultipartFile file,
@RequestParam Integer numberOfSheet)
throws Exception {
return service.upload(file, numberOfSheet);
package com.example.test.readfileexcel.service;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.util.Iterator;

@author Mhosein-abbasi 11/9/21
public class Service {
public String upload(MultipartFile file, Integer numberOfSheet) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
if (numberOfSheet == null || numberOfSheet < 0) {
numberOfSheet = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheet; i++) {
// Getting the Sheet at index i
Sheet sheet = workbook.getSheetAt(i);
System.out.println("=> " + sheet.getSheetName());
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// 1. You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("Iterating over Rows and Columns using Iterator");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row =;
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell =;
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
return "OK";

3. Download postman (send Request)

4. Create new excel(xlsx, xls) file


5. run application and send request by postman


Java developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Goals That Must be Achieved by The End Of Zuri Internship

Deploying Spring Boot applications on Heroku

Deciding Which Software Projects to Begin as a Self-Thought Software Developer

A story to tell my grandchildren

Introduction to MPSA13 NPN Darlington Transistor Working, Pinout, Features — The Engineering…

Flutter Firebase Messaging: Where everyone makes mistakes!

Picking optimal portfolios using Quantum Computing

Evolving a Homelab

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
M.hosein abbasi

M.hosein abbasi

Java developer

More from Medium

Connect to Different Types of Databases in Spring Boot

Java8 | Exception Handling

Spring Boot, OpenApi 3, Spring Security: how I handled 404 error

Mock static methods with Mockito — Java Unit Testing