1. Re: EXPORT DATAGRID TO PDF in C#/Asp.Net
  2. //*************************************************
  3. //
  4. // Author:
  5. // Ryan Van Aken (vanakenr@msn.com)
  6. // (C) 2009 Ryan Van Aken
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. //*************************************************
  29.  
  30.  
  31.  
  32. //SQL Connection Settings -----------
  33. public string strConn = ConfigurationManager.ConnectionStrings["BLAH-Here"].ConnectionString;
  34. //-----------------------------------
  35.  
  36. protected void Page_Load(object sender, EventArgs e)
  37. {
  38. //Grab the same data as the datagrid [report view] on the reporting page
  39. //Then set the "ContentType" to "application/vnd.ms-excel" which will generate the .XSL file
  40.  
  41. //---Retrieve the Report from SQL, drop into DataSet, then Bind() it to a DataGrid
  42. SqlConnection conn = new SqlConnection(strConn);
  43. conn.Open();
  44. 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);
  45. cmd1.SelectCommand.CommandType = CommandType.Text;
  46. DataSet dsReports = new DataSet("tblReporting");
  47. cmd1.Fill(dsReports);
  48. conn.Close();
  49.  
  50. DataGrid dtaFinal = new DataGrid();
  51. dtaFinal.DataSource = dsReports.Tables[0];
  52. dtaFinal.DataBind();
  53.  
  54. dtaFinal.HeaderStyle.ForeColor = System.Drawing.Color.White;
  55. dtaFinal.HeaderStyle.BackColor = System.Drawing.Color.DarkGray;
  56. dtaFinal.ItemStyle.BackColor = System.Drawing.Color.White;
  57. dtaFinal.AlternatingItemStyle.BackColor = System.Drawing.Color.AliceBlue;
  58.  
  59.  
  60. //---Create the File---------
  61. Response.Buffer = true;
  62. Response.ClearContent();
  63. Response.ClearHeaders();
  64.  
  65. //---For PDF uncomment the following lines----------
  66. //Response.ContentType = "application/pdf";
  67. //Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
  68.  
  69. //---For MS Excel uncomment the following lines----------
  70. //Response.ContentType = "application/vnd.ms-excel";
  71. //Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
  72.  
  73. //---For MS Word uncomment the following lines----------
  74. //Response.ContentType = "application/vnd.word";
  75. //Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
  76.  
  77. //---For CSV uncomment the following lines----------
  78. //Response.ContentType = "text/csv";
  79. //Response.AddHeader("content-disposition", "attachment;filename=FileName.csv");
  80.  
  81. //---For TXT uncomment the following lines----------
  82. //Response.ContentType = "text/plain";
  83. //Response.AddHeader("content-disposition", "attachment;filename=FileName.txt");
  84.  
  85.  
  86. EnableViewState = false;
  87.  
  88. StringWriter sw = new StringWriter();
  89. HtmlTextWriter hw = new HtmlTextWriter(sw);
  90.  
  91. //---Renders the DataGrid and then dumps it into the HtmlTextWriter Control
  92. dtaFinal.RenderControl(hw);
  93.  
  94. //---Utilize the Response Object to write the StringWriter to the page
  95. Response.Write(sw.ToString());
  96. Response.Flush();
  97. Response.Close();
  98. Response.End();
  99. }