Home > Forum > ASP.NET > Export to PDF

Export to PDF

by abhishek pandey   on Dec 12, 2013   Category: ASP.NET  |  Views: 2116    |  Points: 5   |  Starter 
  Reply
How, we can export data from grid view to PDF.


private DataTable GetData()
{
string str = "SELECT [EmpFirstName], [EmpLastName], [EmpAddress], [EmpPhoneNo], [EmpEmail] FROM [Employeetest]";
string s = System.Configuration.ConfigurationManager.ConnectionStrings["Abhishek_testConnectionString"].ToString();
SqlConnection con = new SqlConnection(s);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(str, con);
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}

protected void btnexportexcel_Click(object sender, EventArgs e)
{

string strFilename = "Employeedetail.pdf";
UploadDataTableToPDF(GetData(), strFilename);

}


protected void UploadDataTableToPDF(DataTable dtEmp, string filename)
{
string attachment = "attachment; filename=" + filename;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
string tab = string.Empty;
foreach (DataColumn dtcol in dtEmp.Columns)
{
Response.Write(tab + dtcol.ColumnName);
tab = "\t";
}
Response.Write("\n");
foreach (DataRow dr in dtEmp.Rows)
{
tab = "";
for (int j = 0; j < dtEmp.Columns.Count; j++)
{
Response.Write(tab + Convert.ToString(dr[j]));
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :Export to PDF   
by Thamil
on Dec 15 2013 1:26AM
Points : 10
Gold 
Hello Abhishek pandey,

Actually there is no built in base class library in .net to generate pdf files, need to use third party librarry -iTextSharp which is free and will help you to convert grdivew data to pdf files. Please see the below URL for reference.

http://www.c-sharpcorner.com/UploadFile/raj1979/export-grid-view-data-to-pdf-using-itextsharp/
http://www.dotnetfox.com/articles/how-to-export-gridview-data-to-pdf-document-in-Asp-Net-with-C-Sharp-using-itextsharp-1028.aspx
http://www.aspsnippets.com/Articles/Export-Paging-enabled-Gridview-to-PDF-with-Formatting-using-iTextSharp-in-ASPNet.aspx

I hope above link will help you on this.
  Re :Export to PDF   
by Leo.D
on Apr 17 2014 7:41PM
Points : 10
Starter 
Just another way to do that.
Step 1, Load Data Information
Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which is about to be exported out.
private void btnLoad_Click(object sender, EventArgs e)
{
using (OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.tbCS.Text;
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);
dataGridView.DataSource = dt;
}
}
}

Step 2, Set Export into PDF
private void btnRun_Click(object sender, EventArgs e)
{
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("20110223.pdf");

}


More details:http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Program-Guide/How-to-Export-DataTable-to-PDF.html

regards
  Re :Export to PDF   
by Leo.D
on Apr 17 2014 7:44PM
Points : 10
Starter 
Just another way to do that.
Step 1, Load Data Information
Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which is about to be exported out.
private void btnLoad_Click(object sender, EventArgs e)
{
using (OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.tbCS.Text;
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);
dataGridView.DataSource = dt;
}
}
}

Step 2, Set Export into PDF
private void btnRun_Click(object sender, EventArgs e)
{
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("20110223.pdf");

}


More details:http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Program-Guide/How-to-Export-DataTable-to-PDF.html

regards

Recent Post

Latest Posts