How to read data from Excel using Java?

#java #springboot #springframework #javafullstackdeveloper #javabackenddeveloper #javajobs #javadevelopment #javaprogramming #javabackenddeveloper #javacommunity #javafullstack

Hello everyone!!!

I just published my new article about Reading Excel data and converting it to a Java object and then transferring it to a database. As I developer we need to know efficient way to handle excel data and dump into database. As my 2 years experience, I try to handle about 1 lack excel line data and dump into database, if you have more than efficient code then please let me know and shared the idea I appreciate to share the knowledge.

Here in my article I will not deal with spring batch concept, I only deal with multithreading thread concept available in Java.

There are two concepts of Java code running

1) Synchronous: Sync is single-thread, so only one operation or program will run at a time.

2) Asynchronous: Async is multi-thread, which means operations or programs can run in parallel. Async is non-blocking, which means it will send multiple requests to a server.

First I will write a code without for loop to iterate 1 lack data

With synchronous simple thread and make some observation of time reading excels data.

Let start with code and only read one by one code, I will provide whole source code link of my gitHub account at the end of the article

I assume that you are familiar with the basic spring boot project

Here is our spring boot project

you can make project using

Starting with Spring Initializr

https://start.spring.io/

No alt text provided for this image

mainly we need 3 important dependency

No alt text provided for this image

now we modify our application properties

we make configuration database and auto table creation ,

i make here create-drop for each testing for production env you should be as

none or update

spring.jpa.hibernate.ddl-auto= create-drop


No alt text provided for this image

first start with controller I made 3 endpoint for testing and i will explain one by one

FIRST START WITH

1) /withoutAsync

@RestControlle
@RequestMapping("/api/v1/users")
@AllArgsConstructor
public class UserAccountController {


	private final UserAccountService userAccountService;


	@PostMapping("/withoutAsync")
	public ResponseEntity<?> batchInsertWithoutAsync(@RequestParam("file") MultipartFile multipartFile) {
		userAccountService.batchInsertWithoutAsync(multipartFile);
		return ResponseEntity.ok("File Reading complete without async");
	}r        

then we need to write UserAccountService.java for processing business logic

No alt text provided for this image

Here I make generic code for reading excel cell

No alt text provided for this image

now we ready to test our first test case for simple sync way

i am using postman for testing and make one excel for contain 1 lack row data

here is my sample header excel file

No alt text provided for this image

now try to hit api using postman

No alt text provided for this image
No alt text provided for this image

Observation 1:

1)time taken for excel read and convert to java object is 11.67 sec

2) 100000 [ 1 lack ] data saving for 43.39 sec

in postman you can see total request process time is= 55.23 sec

ours first testing is completed with sync thread.

also check in database 1 lack data is inserted

No alt text provided for this image

2) /Async

here is our second end point

batch Insert Without Async Without Object split

	@PostMapping("/Async"
	public ResponseEntity<?> batchInsertWithoutAsyncWithoutObjectsplit(
			@RequestParam("file") MultipartFile multipartFile) {
		userAccountService.batchInsertWithoutAsyncWithoutObjectsplit(multipartFile);
		return ResponseEntity.ok("File Reading complete with async");
	})        


here in service layer i make code for async executions'

No alt text provided for this image

only i have make save data in repo async

What is CompletableFuture? A CompltableFuture is used for asynchronous programming. Asynchronous programming means writing non-blocking code. It runs a task on a separate thread than the main application thread and notifies the main thread about its progress, completion or failure.

No alt text provided for this image
No alt text provided for this image

Observation 2:

1) time taken for excel read and convert to java object is 13.20 sec

2) 100000 [ 1 lack ] data saving for 0.002 sec because background inserting data asynchronous.

3) in postman you can see total request process time is= 13.93 sec

ours secod testing is completed with Async thread.

also check in database 1 lack data is inserted

No alt text provided for this image

3)Async/split/object

here is our third end point

why we need splitting object and save the data because some time excel data over the 10 lack or something then for inserting data we need to split object list and save and clear the object list.

batch Insert Without Async With Object split


@PostMapping("/Async/split/object"
	public ResponseEntity<?> batchInsertWithoutAsyncWithObjectsplit(@RequestParam("file") MultipartFile multipartFile) {
		userAccountService.batchInsertWithoutAsyncWithObjectsplit(multipartFile);
		return ResponseEntity.ok("File Reading complete with async object split");
	})        

now in service layer i am writing splitting object code

No alt text provided for this image
No alt text provided for this image

here now last end testing with postman

No alt text provided for this image

o/p

batch insert start
Execution time for excel read is = 13.78200 seconds
total user Accounts size=100000
sub partitions=25
executeBatch=0
executeBatch=1
Current Thread Name: pool-1-thread-1
executeBatch=2
executeBatch=3
executeBatch=4
executeBatch=5
executeBatch=6
executeBatch=7
executeBatch=8
executeBatch=9
executeBatch=10
executeBatch=11
executeBatch=12
executeBatch=13
executeBatch=14
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-3
executeBatch=15
executeBatch=16
executeBatch=17
executeBatch=18
executeBatch=19
executeBatch=20
executeBatch=21
executeBatch=22
executeBatch=23
executeBatch=24
execute Batch done
Execution time is for all save data=0.01100 seconds
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3
Current Thread Name: pool-1-thread-2
Current Thread Name: pool-1-thread-1
Current Thread Name: pool-1-thread-3t        


Observation 3:

1) time taken for excel read and convert to java object is 13.79 sec

2) 100000 [ 1 lack ] data saving for 0.01 sec because background inserting data asynchronous.

3) in postman you can see total request process time is= 12.69 sec

ours end testing is completed with Async thread.

also check in database 1 lack data is inserted

No alt text provided for this image

This article covered the basics of spring boot + Excel data reading in efficient way, complete source code for this article can be found over on https://github.com/bhushan0109/Spring-Boot-Excel-Reader-.git

That’s it for today.

@Author Bhushan Patil


To view or add a comment, sign in

More articles by Bhushan Patil

  • JMeter

    I choose this article because as developers we need to know this tool for api testing. As a developer, when we test our…

  • Spring Security with JWT for REST API

    #javadeveloper #javaprogramming #springsecurity #jwt #java #springboot #springframework #javafullstackdeveloper…

  • Postgres insert or update trigger WHEN condition

    Postgres Professional#javadeveloper #javaprogramming #java #springboot #springframework #javafullstackdeveloper…

  • Efficiently Read Large Excel

    #java #springboot #springframework #javafullstackdeveloper #javabackenddeveloper #javajobs #javadevelopment…

  • How to Integrate Apache Kafka in Your Spring Boot Application

    #java #springboot #springframework #javafullstackdeveloper #javabackenddeveloper #javajobs #javadevelopment #rabbitmq…

  • Spring Boot RabbitMQ Producer and Consumer Example

    #java #springboot #springframework #javafullstackdeveloper #javabackenddeveloper #javajobs #javadevelopment #rabbitmq…

  • Spring Boot Scheduler

    Hi Java LinkedIn family, I am sharing a new concept. In this article, you will learn how to schedule tasks in Spring…

  • The best way to externalise Spring Boot application.properties

    The best way to externalise Spring Boot application.properties First Create a Spring Boot application By default your…

  • Retrieving a POJO DTO projection with Spring Data JPA

    Hi Java LinkedIn family, I am sharing a new concept. I have noticed that many Java developers are doing the bad…

  • important java springboot concept

    Hi java LinkedIn family, I share a new concept. if you are creating multiple services in Spring Boot, JPA, Hibernate…

Others also viewed

Explore content categories