read data from excel file in java spring boot

Read data from excel file using apache poi dependency

Download required dependencies – from here

Java code to read files

package guru.duplicateremover;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadDataFromExcel {

// create main method
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
    // your logic inside main method

    // excel file path
    final String file_path = "your_filepath";

    // excel file name
    final String file_name = "demo.xlsx".trim();

    // create object of input stream
    FileInputStream fileInputStream = new FileInputStream(file_path + file_name);

    XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);

    XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);

    System.out.println("first_name" + " " + "last_name" + " " + "email");
    Row row;
    for (int i = 1; i <= xssfSheet.getLastRowNum(); i++) {
        row = xssfSheet.getRow(i);

        System.err.println(row.getCell(0) + " " + " " + row.getCell(1) + " " + row.getCell(2));

    }

    xssfWorkbook.close();

}

}

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *