Load Excel From SQL Database

IronXL makes it easy to convert SQL query results into Excel workbooks, allowing developers to add data export functionality to their .NET applications quickly. Developers can skip manual data formatting and focus on creating clear and user-friendly reports by loading a DataSet directly into a WorkBook.

This feature is ideal for automating tasks like generating reports or sharing database insights. It supports popular file formats like XLS and XLSX, ensuring compatibility with most spreadsheet applications. Technically, it reduces the amount of code needed and uses IronXL’s powerful API to handle large datasets efficiently, making it a practical choice for data-heavy applications.

This snippet demonstrates how to export SQL query results to an Excel file using IronXL. First, an SqlDataAdapter is initialized with a query (SELECT * FROM Products) and a connection string pointing to the database. The SqlDataAdapter fetches data and populates a DataSet object, a flexible in-memory representation of the query results.

The WorkBook.Load method is then used to directly convert the DataSet into an Excel workbook. This method efficiently maps the tabular structure of the DataSet to a worksheet, preserving rows and columns. Finally, the SaveAs method is called to save the workbook as an .xlsx file.

This approach eliminates the need for manually iterating through rows or handling complex Excel formatting, making it a highly efficient solution for exporting database content. IronXL ensures that the generated Excel file is compatible with various spreadsheet applications while maintaining robust performance and ease of use.

// Step 1: Create an instance of SqlDataAdapter with the SQL query and connection string.
// replace "yourConnectionString" with your actual database connection string.
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Products", @"yourConnectionString");

// Step 2: Create a DataSet object to hold the query results in memory.
DataSet ds = new DataSet();

// Step 3: Fill the DataSet with data fetched by the SqlDataAdapter.
adapter.Fill(ds);

// Step 4: Load the DataSet into an IronXL WorkBook to convert the data into an Excel format.
WorkBook workBook = WorkBook.Load(ds);

// Step 5: Save the WorkBook as an Excel file named "ExportedData.xlsx".
workBook.SaveAs("ExportedData.xlsx");
// Step 1: Create an instance of SqlDataAdapter with the SQL query and connection string.
// replace "yourConnectionString" with your actual database connection string.
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Products", @"yourConnectionString");

// Step 2: Create a DataSet object to hold the query results in memory.
DataSet ds = new DataSet();

// Step 3: Fill the DataSet with data fetched by the SqlDataAdapter.
adapter.Fill(ds);

// Step 4: Load the DataSet into an IronXL WorkBook to convert the data into an Excel format.
WorkBook workBook = WorkBook.Load(ds);

// Step 5: Save the WorkBook as an Excel file named "ExportedData.xlsx".
workBook.SaveAs("ExportedData.xlsx");
' Step 1: Create an instance of SqlDataAdapter with the SQL query and connection string.
' replace "yourConnectionString" with your actual database connection string.
Dim adapter As New SqlDataAdapter("SELECT * FROM Products", "yourConnectionString")

' Step 2: Create a DataSet object to hold the query results in memory.
Dim ds As New DataSet()

' Step 3: Fill the DataSet with data fetched by the SqlDataAdapter.
adapter.Fill(ds)

' Step 4: Load the DataSet into an IronXL WorkBook to convert the data into an Excel format.
Dim workBook As WorkBook = WorkBook.Load(ds)

' Step 5: Save the WorkBook as an Excel file named "ExportedData.xlsx".
workBook.SaveAs("ExportedData.xlsx")
$vbLabelText   $csharpLabel

Click here to view the How-to Guide, including examples, sample code, and files