HTML JPG PDF XML EXCEL
  Product Family
SqlServer

Convert SqlServer to EXCEL in C#

High-speed C# library for converting SqlServer to EXCEL. This is a professional software solution to import and export EXCEL, SqlServer, and many other formats on .NET Framework, .NET Core or Mono Platforms.

Convert SqlServer to EXCEL Using C#

How do I convert SqlServer to EXCEL? With Aspose.Cells for .NET library, you can easily convert SqlServer to EXCEL programmatically with a few lines of code. Aspose.Cells for .NET is capable of building cross-platform applications with the ability to generate, modify, convert, render and print all Excel files. .NET Excel API not only convert between spreadsheet formats, it can also render Excel files as images, PDF, HTML, ODS, CSV, SVG, JSON, WORD, PPT and more, thus making it a perfect choice to exchange documents in industry-standard formats. Open NuGet package manager, search for Aspose.Cells and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


PM> Install-Package Aspose.Cells

How to Convert SqlServer to EXCEL via C#

Need to convert SqlServer data to EXCEL programmatically? .NET developers can easily load & convert SqlServer to EXCEL in just a few lines of code.

  1. Install ‘Aspose.Cells for .NET’.
  2. Add a library reference (import the library) to your C# project.
  3. Query data from the SqlServer database to obtain a DataTable object.
  4. Create a new Workbook and import data from a DataTable object.
  5. Save data to xlsx format by calling Workbook.Save method.
 

Sample Code to Convert SqlServer to EXCEL - C#

var connectionString = "Server=localhost;Database=SqlServerTestDataBase;User ID=root;Password=admin;Trusted_Connection=False;";
var tableName = "countrylanguage";

string query = $"SELECT * FROM {tableName}";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlCommand cmd = new SqlCommand(query, connection);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    DataTable dataTable = new DataTable();
    adapter.Fill(dataTable);

    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Cells.ImportData(dataTable, 0, 0, new ImportTableOptions() { InsertRows = true });
    workbook.Save("SQLServerData.xlsx");
}
 

C# library to convert SqlServer to EXCEL

There are two alternative options to install “Aspose.Cells for .NET” onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:

  1. Install a NuGet Package . See Documentation
  2. Install the library using Package Manager Console within Visual Studio IDE

System Requirements

Before running the .NET conversion example code, make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET, .NET Core, Windows Azure or Mono Platforms..
  • Development environment like Microsoft Visual Studio.
  • Add reference to the Aspose.Cells for .NET DLL in your project.