Developers always wish to quickly export
data to PDF, HTML, Excel or Word Document. Here shares a fast and smooth
solution for .NET developer/programmer. This solution was based on and supported
by a free data export component.
The following picture is a screenshot of the design form of this project:

Please download Spire.Dataexport and follow my steps to export data to PDF,HTML, Excel or Word.
In order to export data to the target files, we have to load data information first. The following codes is an easy alternative to help us load data information.
private void btnLoad_Click(object sender, EventArgs e)
{
using (OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.tbCS.Text;
System.Data.OleDb.OleDbCommand oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = this.tbCT.Text;
oleDbCommand.Connection = oleDbConnection;
using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
After we successfully load data information we need, next we can set export to PDF, Html, Excel or Word document.
1. Export data to PDF
{
Spire.DataExport.PDF.PDFExport PDFExport = new Spire.DataExport.PDF.PDFExport();
PDFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
PDFExport.DataTable = this.dataGridView.DataSource as DataTable;
PDFExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
PDFExport.SaveToFile("result.pdf");
}
2. Export Data to HTML
private void btnRun_Click(object sender, EventArgs e)
{
Spire.DataExport.HTML.HTMLExport HTMLExport = new Spire.DataExport.HTML.HTMLExport();
HTMLExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
HTMLExport.DataTable = this.dataGridView1.DataSource as DataTable;
HTMLExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
HTMLExport.SaveToFile("result.html");
} 3. Export Data to Excel
private void btnRun_Click(object sender, EventArgs e)
{
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 = this.dataGridView1.DataSource as DataTable;
worksheet1.StartDataCol = ((System.Byte)(0));
cellExport.Sheets.Add(worksheet1);
cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
cellExport.SaveToFile("result.xls");
}
4. Export Data to Word
private void btnRun_Click(object sender, EventArgs e)
{
Spire.DataExport.RTF.RTFExport RTFExport = new Spire.DataExport.RTF.RTFExport();
RTFExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
RTFExport.DataTable = this.dataGridView1.DataSource as DataTable;
RTFExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
RTFExport.FileName = "RTF0722.rtf";
RTFExport.SaveToFile();
}About Free Dataexport
Free Data Export Component is a Community Edition of Spire.DataExport, which is a commercial/personal use Full Free .net component. As a C#/VB.NET free component, it also offers a lot of powerful functions. This free Data Export Component can easily help you export data from SQL/DataTable to XLS, PDF and MS Word, HTML, MS clipboard ,XML, PDF, DBF, SQL Script, SYLK, DIF, CSV. Free Data Export Component is totally independant and not necessary to install Microsoft Excel or Adobe Acrobat or Microsoft Access on your machine. You also can use this free component to create Excel spreadsheet, PDF document or Access database.