Home > Articles > .Net Framework Articles > Exporting data to multiple formats in C#

Exporting data to multiple formats in C#

by Bhavik Patel   on Dec 09, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 934    |  Points: 100   
Like this article? Bookmark and Share:
If you are working on enterprise application for your client, you shall be facing one common problem in every meeting, your client always asks for the multiple format data export functionality for data grids and reports. Only a few libraries are available in the market to fulfill our requirements.

Introduction

This article is intended to demonstrate and review the features provided by the Spire.DataExport for exporting data into multiple formats. Spire.DataExport is developed by e-icecube which are expert in developing office components and has the ability to export data into MS Word, Excel, RTF, Access, PDF, XPS, HTML, XML, Text, CSV, DBF, SYLK, SQL Script, DIF, Clipboard etc. It's the support to export data from Command, ListView and DataTable components faster than others.

Let's begin,

Create a console application and name the solution DataExport

.
data-export

Download Spire.DataExport

Spire.DataExport available in free and premium version. You can download it from the official website or
from nuget

PM> Install-Package Spire.DataExport

Once you add a package to Spire.DataExport, it will add reference of below libraries in the project.

  • Spire.DataExport

  • Spire.DataExport.ResourceMgr

  • Spire.License

To implement this example I have used the Northwind database (Download and restore it on your local machine).

Using ADO.NET we will fetch and store the product data into a DataTable.

string connectionString = @"Data Source=.\SQLEXPRESS2014;Initial Catalog=NORTHWND;Integrated Security=True"; ;
using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand("select * from products", con))
    {
        try
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);
            dt.TableName = "Export";
            return dt;
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
        finally
        {
            con.Close();
        }
    }
}

Now, Let's validate the Spire.DataExport functionality for few document formats like excel, word, html, xml and PDF.

To Excel

To Export the data we need to initialize the Spire.DataExport's CellExport and WorkSheet. Below code is useful to create and save excel file in local machine.

Code
Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport();
Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet();
worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
worksheet1.DataTable = dt;
worksheet1.StartDataCol = ((System.Byte)(0));
cellExport.Sheets.Add(worksheet1);
cellExport.SaveToFile("SpireExport.xls");
Result

Based on implementation I concluded that Spire.DataExport is around 53% faster than the CloseXML. But we have one problem here, if you are using the community edition of Spire.DataExport then only allowed 40 rows to export and add extra sheet with name Evolution copy.

Let's try other formats also.



To PDF

Code
Spire.DataExport.PDF.PDFExport PDFExport = new Spire.DataExport.PDF.PDFExport();
PDFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
PDFExport.DataTable = dt;
PDFExport.SaveToFile("SpireExport.pdf");
Result


To Word

Code
RTFExport rtfExport = new RTFExport();
rtfExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
rtfExport.DataTable = dt;
RTFStyle rtfStyle = new RTFStyle();
rtfStyle.FontColor = Color.Blue;
rtfStyle.BackgroundColor = Color.LightGreen;
rtfExport.RTFOptions.DataStyle = rtfStyle;
rtfExport.FileName = @"..\..\SpireExport.doc";
rtfExport.SaveToFile();
Result


To HTML

Code
Spire.DataExport.HTML.HTMLExport HTMLExport = new Spire.DataExport.HTML.HTMLExport();
HTMLExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
HTMLExport.DataTable = dt;
HTMLExport.SaveToFile("SpireExport.html");
Result


To CSV

Code 

TXTExport CSVExport = new TXTExport();
CSVExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
CSVExport.DataTable = dt;
CSVExport.FileName = "SpireExport.csv";
CSVExport.SaveToFile();
Result

Hope this article will help you alot

Conclusion

I am very impressed with the speed of exporting data into an excel sheet, which is 50% faster than other open source libraries. Spire.DataExport is very useful and efficient library to boost up data export performance. I will recommend the Spire.DataExport rather than other costly libraries available in the market.



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts