How to read excel file in spring boot

How to read excel file in spring boot

  • How to read excel file in spring boot

Please click on following link to download sample project.

Read excel file in spring boot sample project (6 downloads)

How to read excel file in spring boot

Please follow the following steps to know how to read excel file in spring boot
1)First create a simple maven project and spring boot starter and apache poi dependency which is shown below
spring boot apache poi dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
		
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
		
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

2)Now create a restcontroller to read excel file which is shown below
How to read excel file in spring boot

package com.example.SpringBootExcelReader;

import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ExcelController {

	@PostMapping("excel")
	public String excelReader(@RequestParam("file") MultipartFile excel) {
		
		try {
			XSSFWorkbook workbook = new XSSFWorkbook(excel.getInputStream());
			XSSFSheet sheet = workbook.getSheetAt(0);
			
			for(int i=0; i<sheet.getPhysicalNumberOfRows();i++) {
				XSSFRow row = sheet.getRow(i);
				for(int j=0;j<row.getPhysicalNumberOfCells();j++) {
					System.out.print(row.getCell(j) +" ");
				}
				System.out.println("");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "Success";
	}
}

3)Now run your spring boot application

4)Now we will use following excel data for testing purpose
apache poi excel read spring boot

5)Now open postman and create post request which is shown below
Read excel file in spring boot

6)Once you click on Send you will see following kind of data on the console which is shown below
spring boot read excel file

Frequently Ask Questions

1)How to get large number without exponent using apache poi in spring boot

How to get large number without exponent using apache poi in spring boot
Answer :
DataFormatter dataformatter = new DataFormatter();
dataformatter.formatCellValue(<cellValue>);
apache poi get large number without exponent in spring boot

You may also like...