In our work, sometimes we need to save data from SQL Server into an Excel file, maybe most people immediately think of Microsoft.Office.Introp.Excel Object, but it has the disadvantage that you need to install Microsoft Excel or Office on the machine. In this article we will see how to save data into the Excel and convert the Excel to HTML in a pretty easy method to use two free libraries, which doesn't need to install Microsoft Excel or Office.
To demonstrate how to save data from SQL to Excel file, I have created a sample SQL table and here is how it looks like.
Now, the next step is to read the SQL data into a Datatable.
- private static DataTable exportedData()
- {
- DataTable dt = new DataTable();
- using (SqlConnection connection = new SqlConnection("Data Source=MICROSOF- 8A1237\\SQLEXPRESS;Initial Catalog=test;Integrated Security=SSPI;"))
- {
- using (SqlCommand command = new SqlCommand("select * from PivotTable"))
- {
- using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command.CommandText, connection))
- {
- dataAdapter.Fill(dt);
- }
- }
-
- }
- return dt;
- }
Next is to export the Datatable to the Excel file with the third-party library
Epplus; the namespace to be used:
The following is the actual code:
- DataTable dt = exportedData();
- ExcelPackage elPackage = new ExcelPackage();
- ExcelWorksheet worksheet = elPackage.Workbook.Worksheets.Add("SqlData");
- worksheet.Cells["A1"].LoadFromDataTable(dt, true);
As you can see, it is pretty easy that you don't need to create a new Excel and there is only LoadFromDataTable method called.
Next is to convert the Excel file to HTML, Epplus doesn't support the feature, so you need to use the another component: Spire.xls, which seems to be a community sponsored version of Epplus.
The following is the namespace to be used:
The following is the actual code:
- Workbook book = new Workbook();
- book.LoadFromStream(ms,ExcelVersion.Version2010);
- Worksheet sheet = book.Worksheets["SqlData"];
- sheet.SaveToHtml("result.html");
It is also quite easy. To learn more about the component that also supports conversion of the Excel file to a PDF, image and so on.
The following is the entire code.
- class Program
- {
- static void Main(string[] args)
- {
- DataTable dt = exportedData();
- ExcelPackage elPackage = new ExcelPackage();
- ExcelWorksheet worksheet = elPackage.Workbook.Worksheets.Add("SqlData");
- worksheet.Cells["A1"].LoadFromDataTable(dt, true);
- using (MemoryStream ms = new MemoryStream())
- {
- elPackage.SaveAs(ms);
- Workbook book = new Workbook();
- book.LoadFromStream(ms, ExcelVersion.Version2010);
- Worksheet sheet = book.Worksheets["SqlData"];
- sheet.SaveToHtml("result.html");
- }
- System.Diagnostics.Process.Start("result.html");
- }
- private static DataTable exportedData()
- {
- DataTable dt = new DataTable();
- using (SqlConnection connection = new SqlConnection("Data Source=MICROSOF- 8A1237\\SQLEXPRESS;Initial Catalog=test;Integrated Sec urity=SSPI;"))
- {
- using (SqlCommand command = new SqlCommand("select * from PivotTable"))
- {
- using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command.CommandText, connection))
- {
- dataAdapter.Fill(dt);
- }
- }
- }
- return dt;
- }
- }