Click here to Skip to main content
16,006,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Sir,

This is Satya.
I am working with Microsoft technologies and I have a problem on converting data DataGridView in web page to PDF file.

Please help me?
Posted
Updated 29-Sep-11 1:39am
v2
Comments
RaisKazi 29-Sep-11 7:49am    
Share your code.

1 solution

protected void Page_Load(object sender, EventArgs e)
{
//Grab the same data as the datagrid [report view] on the reporting page
//Then set the "ContentType" to "application/vnd.ms-excel" which will generate the .XSL file

//---Retrieve the Report from SQL, drop into DataSet, then Bind() it to a DataGrid
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
SqlDataAdapter cmd1 = new SqlDataAdapter("EXEC [dbo].[spStatReport] @CompanyID=" + Session["CompanyID"] + ", @StatReportID=" + Request.QueryString["ReportID"].ToString() + ", @StartDate='" + Request.QueryString["StartDate"].Replace("-", "/").ToString() + "', @EndDate='" + Request.QueryString["EndDate"].Replace("-", "/").ToString() + "';", conn);
cmd1.SelectCommand.CommandType = CommandType.Text;
DataSet dsReports = new DataSet("tblReporting");
cmd1.Fill(dsReports);
conn.Close();

DataGrid dtaFinal = new DataGrid();
dtaFinal.DataSource = dsReports.Tables[0];
dtaFinal.DataBind();

dtaFinal.HeaderStyle.ForeColor = System.Drawing.Color.White;
dtaFinal.HeaderStyle.BackColor = System.Drawing.Color.DarkGray;
dtaFinal.ItemStyle.BackColor = System.Drawing.Color.White;
dtaFinal.AlternatingItemStyle.BackColor = System.Drawing.Color.AliceBlue;


//---Create the File---------
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();

//---For PDF uncomment the following lines----------
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");

//---For MS Excel uncomment the following lines----------
//Response.ContentType = "application/vnd.ms-excel";
//Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

//---For MS Word uncomment the following lines----------
//Response.ContentType = "application/vnd.word";
//Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");

//---For CSV uncomment the following lines----------
//Response.ContentType = "text/csv";
//Response.AddHeader("content-disposition", "attachment;filename=FileName.csv");

//---For TXT uncomment the following lines----------
//Response.ContentType = "text/plain";
//Response.AddHeader("content-disposition", "attachment;filename=FileName.txt");


EnableViewState = false;

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

//---Renders the DataGrid and then dumps it into the HtmlTextWriter Control
dtaFinal.RenderControl(hw);

//---Utilize the Response Object to write the StringWriter to the page
Response.Write(sw.ToString());
Response.Flush();
Response.Close();
Response.End();
}
 
Share this answer
 
Comments
styanarayana pilla 30-Sep-11 1:20am    
hi sir,
Thanks for your Solution,but i am not satisfied that solution.In case of pdf
format ,file was created but data was not getting. It Show the following error
"Adobe Reader could not open Product.pdf' besause it is either not a supported file type or because file has been damaged "
Please help me..
Advance thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900