Many clients provide their data in the form of excel sheet and request developer to save it in your database as it is. If you ever have implemented ‘How to Upload excel data into Database using Spring Boot’, it’s good. If not, no worries!, we will learn the same in this article. In the end, you will find it very easy & fast way to get it done. In fact, once you go through the complete article you will observe that you only need to provide the location of your excel file in application.properties file. You will get the data uploaded into database by making small changes in your own entity classes only.
We will not promote any hard-coding in our implementation. Even if you are working first time in Spring Boot application, you will easily implement the functionality by following the steps mentioned here. Further to escape hard-coding we will take some of the values from properties file so that we don’t need to modify the java file in case we have any change request in future. Also, we will use the most popular Apache POI: ‘The Java API for Microsoft Documents’ in our implementation. Now let’s get into the topic “How to Upload Excel data into Database Using Spring Boot”.
What all functionality/features will you get from this article?
1) How to save data into the database if input data is in the form of excel sheet.
2) How to upload any file (apart from excel) using a web browser.
3) Above two functionalities are not interdependent. We can execute each functionality separately.
4) Apache POI Latest version (currently 4.1.2) has been used to implement the project.
5) Additionally, code is successfully tested on JDK8, JDK9 and even JDK14 versions.
6) How to read data from excel sheet and insert into database table in spring boot?
What will you learn after implementing this application?
1) How to create a Spring Boot web application that incorporates industry level project design?
2) How to design a Java web application, including all layers using Controller, Service, Repository, UI etc. as in real time project designs?
3) Equally important, Where and how to use Annotations like @Value, @Autowired, @Controller, @Service, @Repository, @Entity, @Id, @GeneratedValue, @GetMapping, @PostMapping etc.
4) Also, Working with Spring Boot Data JPA repository interface.
5) Implementation of the most popular Apache POI (Java API for Microsoft Documents)
6) Then, How to work with application.properties file?
7) How to upload data as a batch into database using Spring Boot?
8) Additionally, How to write modular & reusable code?
9) How to implement dynamic code with minimal changes, keeping future change requests in mind?
10) How to write code without hard-coding the values using Spring Boot?
11) Last but not the least, you will learn “How to Upload Excel data into Database Using Spring Boot”.
12) Particularly, How to read data from excel sheet and insert into database table in spring boot.
What all implementations can you do yourself after going through this example?
1) How to read data from excel sheet and insert into database table in spring mvc?
2) How to upload excel file to database using spring boot?
3) spring boot excel file upload example
4) How to read data from excel sheet and insert into database table in spring boot?
5) How to import data from excel to mysql using spring boot?
6) How to upload excel file into database using java?
7) How to read excel file in spring boot example?
8) How to read data from excel and store in the datatable in java?
9) How to upload and read excel file in spring boot?
10) How to write java code to read excel file and insert into database?
11) How to import excel data into database using java?
12) How to upload Excel file into database using Apache POI and Spring Framework?
Software Used in this project
♦ STS (Spring Tool Suite): Version-> 4.7.1. RELEASE
⇒ Dependent Starters : Spring Web, Spring Data JPA, MySql Driver
♦ MySQL Database : Version ->8.0.19 MySQL Community Server
♦ JDK8 or later versions (Extremely tested on JDK8, JDK9 and JDK14)
We will add following apache ‘poi-ooxml’ dependency in pom.xml as it is an external jar to get the features of Microsoft Excel.
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
You should have a database ready to store data from excel file. If not, create a MySQL database to save the data. Still, if you just want to test the functionality you can use our configurations as database name ‘exceldata’ and table name ‘invoice’ accordingly.
Coding Steps on ‘How to Upload Excel data into Database Using Spring Boot ?’
— Create Project in STS
If you are new in Spring Boot go to Internal Link to create a sample project in spring boot. While creating a project in STS, add 3 starters ‘MySql Driver’, ‘Spring Data JPA‘ and ‘Spring Web’. You can also add ‘Spring Boot DevTools’ optionally.
Where to place JSP files ?
In order to accommodate JSP files,
1) create folder ‘webapps’ under src/main/
2) under ‘webapps’ create folder ‘WEB-INF’
3) Under ‘WEB-INF’ create folder ‘pages’ to accommodate your JSP files as view part.
4) Now your Folder structure hierarchy should look like “src/main/webapps/WEB-INF/pages”.
— Writing Java classes, JSPs & Updating application.properties
|com.dev.springboot.service||IExcelDataService.java||create||Service Interface for DB operations|
|com.dev.springboot.service||IFileUploaderService.java||create||Service Interface to upload any file|
|com.dev.springboot.service.impl||ExcelDataServiceImpl.java||create||Service implementation class for DB operations|
|com.dev.springboot.service.impl||FileUploaderServiceImpl.java||create||Service implementation class to upload any file|
|src/main/resources||application.properties||update||properties file to declare common properties in the project|
|src/main/webapp/WEB-INF/pages||uploadPage.jsp||create||UI page to upload a file|
|src/main/webapp/WEB-INF/pages||success.jsp||create||UI page to success message when data is saved into database|
Below are the codes for each file
#DB Connection Properties spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/exceldata spring.datasource.username=root spring.datasource.password=devs # Data JPA Properties spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update # MVC view properties spring.mvc.view.prefix=/WEB-INF/pages/ spring.mvc.view.suffix=.jsp # upload file app.upload.dir=D:/ExcelUploadRepo # Excel file as input for storing it's data app.upload.file=D:/ExcelUploadRepo/InvoiceDetailsSheet.xlsx
Once all above files created in STS, your project structure should look something like below screenshot:
In order to run the application, right click on Project, then select Run As >> Spring Boot App.
Finally, in order to test the results paste url in your browser : http://localhost:8080/
Accordingly, you should see the below page :
Furthermore, in order to upload an excel file, click on ‘Choose File’ which will browse to your file system, then select an excel file to be uploaded and click ‘OK/Open’. Now click on upload in order to upload the file at the location you provided in application.properties accordingly. Note: Besides excel files, this functionality will also work for any file having other extensions. Consequently, once the file is uploaded, you will see the success message with file name you uploaded as below.
In order to upload excel records into database click on ‘Yes’ link. Consequently, you will see the success page immediately with number of records uploaded into database. In contrast, if you click no, nothing will happen, it will just redirect to the same page.
Furthermore, you can check the saved records into database as below:
♥ Note: If you provide the input file name as a value of ‘app.upload.file’ in application.properties and put the excel file in the same folder, You can directly save the excel records into database just by clicking on the ‘Yes’ link. In this case, Even You don’t have to upload the file.
How to do our own implementation from this Example with minimal changes?
In fact, you need to change values of properties included in the application.properties file
1) Update the values of first 4 properties according to your database
2) Also update the file storage directory path in the value of property ‘app.upload.dir’
3) Then update the upload file path with file name in the value of property ‘app.upload.file’
Moreover, Below are the changes in java files :
4) To store excel records into database you need to create new Entity class, new Repository class in place of ‘Invoice.java’ & ‘InvoiceRepository.java’ accordingly as per your requirements.
5) Consequently, you need to change all the occurrences of entity class name in ‘IExcelDataService.java’ and ‘ExcelDataServiceImpl.java’. It’s ‘Invoice’ in our case.
6) In addition, you need to update entity class method names in createList() method of ‘ExcelDataServiceImpl.java’.
Can we use this implementation in a real project ?
Of course, you can use this implementation in a real project with modification described above as per your requirement.
What are the changes we need to do if we want to use this functionality in our real project?
You can go through the minimal modifications suggested in the above FAQ section ‘How to do our own implementation from this Example with minimal changes?’ of this article accordingly.
After going through all the points mentioned in the article, You should be able to work on ‘How to read data from excel sheet and insert into database table in spring boot ?’. However, If you follow all the steps as mentioned in the article, you will implement it successfully in one go without facing any issue. Additionally, you will also be able to work on ‘How to upload any file (apart from excel) using web browser’ confidently. Now, we can expect from you to apply this knowledge in your real time project whenever you have such kind of requirement. Furthermore, if you face any issue on running this project, don’t hesitate to put your comments below.
♥ Also, if you want to learn how to generate PDF report using Spring Boot, kindly visit our separate article on ‘How to generate dynamic PDF report using Spring Boot‘. Additionally, if you want to learn more on Spring Boot trending topics kindly visit our Spring Boot Section.