Import data from Excel to database and export data from database to Excel are common tasks for developers. In this article, I will introduce how to import Excel to database and export database to excel in Java. To read and update database, I use JDBC (MySQL database is used in this article), to read and generate Excel file, I use Spire.XLS for Java API. This solution works for both the old 97-2003 and the new 2007 or above Excel formats.
First, you need to add needed dependencies for MySQL JDBC Driver and Spire.XLS for Java API in your maven project’s pom.xml file.
Export Database to Excel
Assume I have the following data table called “employees” in MYSQL database “pcb”.
In the following code example, you will see how to retrieve the data from the data table and insert into an Excel file using Spire.XLS for Java API:
Import Excel to Database
In the following code example, I will import data from the above generated Excel file back to the data table “employees” in MYSQL database.
I hope you learned how to import database to excel and export excel to database in Java from my article. Apart from interacting with database, the Spire.XLS for Java API also supports importing data from other data sources such as arrays and array lists to Excel, you can give it a try if you’re interested. Happy Coding!