EasyXLS™ library allows you to export a ResultSet to an Excel file. The data in cells can be formatted using predefined formats or user-defined formats.
EasyXLS can be successfully used inclusively to export large Excel files having big volume of data with fast exporting time.
EasyXLS permits you to export Excel files without Excel installed or any other additional software installed.
// Create the database connection
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String sConnectionString =
"jdbc:microsoft:sqlserver://localhost:1433;databasename=Northwind;user=sa;password=;";
Connection sqlConnection = (Connection) DriverManager.getConnection(sConnectionString);
// Create the result set used to populate the sheet
String sQueryString = "SELECT TOP 100 CAST(Month(ord.OrderDate) AS varchar) + '/' + " +
"CAST(Day(ord.OrderDate) AS varchar) + '/' + " +
"CAST(year(ord.OrderDate) AS varchar) AS 'Order Date', " +
"P.ProductName AS 'Product Name', O.UnitPrice AS Price, O.Quantity, " +
"O.UnitPrice * O.Quantity AS Value " +
"FROM Orders AS ord, [Order Details] AS O, Products AS P " +
"WHERE O.ProductID = P.ProductID AND O.OrderID = ord.OrderID";
PreparedStatement pStatement = sqlConnection.prepareStatement(sQueryString,
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE);
ResultSet resultset = pStatement.executeQuery();
// Export resultset to Excel file
System.out.println("Writing file C:\\Samples\\ResultSet to Excel.xlsx.");
workbook.easy_WriteXLSXFile_FromResultSet("C:\\Samples\\ResultSet to Excel.xlsx",
resultset, new ExcelAutoFormat(Styles.AUTOFORMAT_EASYXLS1), "ResultSet");
Export ResultSet to Excel file with multiple sheets
EasyXLS offers the option to insert multiple ResultSets into one worksheet or into multiple worksheets. The ResultSet can be added into a specified worksheet. The column names of the ResultSet can be optionally included.
The below source code sample shows how to add a ResultSet with a nice formatting into an Excel file with two worksheets.
// Create an instance of the class that exports Excel files
ExcelDocument workbook = new ExcelDocument();
// Create a worksheet
workbook.easy_addWorksheet("First sheet");
// Create another worksheet
workbook.easy_addWorksheet("Second sheet");
...
// Populate the ResultSet
ResultSet resultset = pStatement.executeQuery();
// Get the second sheet
ExcelWorksheet worksheet = (ExcelWorksheet)workbook.easy_getSheet("Second sheet");
// Insert ResultSet with column names included
worksheet.easy_insertResultSet(resultset,
new ExcelAutoFormat(Styles.AUTOFORMAT_EASYXLS1), true);
// Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\ResultSet to Excel.xlsx");
Getting started with EasyXLS Excel library
To download the trial version of EasyXLS Excel Library, press the below button:
If you already own a license key, you may login and download EasyXLS from your account.
After installation, you will need to create a Java project and add EasyXLS.jar to external libraries.