Hi,
See this code if could help...
public enum ExportTypes
{
Excel, PDF, TextFile
}
public class ExportManager
{
public static bool Downloader(GridViewReport grdReport, ExportTypes type)
{
HttpContext.Current.Response.ClearContent();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string selectedType = string.Empty;
switch (type)
{
case ExportTypes.Excel:
selectedType = "xls";
grdReport.ExportType = ExportTypes.Excel;
grdReport.FileName = grdReport.FileName + ".xls";
grdReport.RenderControl(htw);
HttpContext.Current.Response.Write(grdReport.ReportHeader +
sw.ToString());
break;
case ExportTypes.PDF:
selectedType = "pdf";
grdReport.RenderControl(htw);
HttpContext.Current.Response.ContentType = "application/pdf";
string fileName2Save = grdReport.FileName.Replace(" ", "").
Replace("/", "-") + ".pdf";
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment; filename=" + fileName2Save);
HttpContext.Current.Response.Cache.
SetCacheability(HttpCacheability.NoCache);
break;
}
if (selectedType == "pdf")
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.
NoCache);
StringReader sr = new StringReader(grdReport.
ReportHeader + "\r\n" + sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 4f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.ContentType =
grdReport.ExportType.ToString();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" + grdReport.FileName);
grdReport.Dispose();
HttpContext.Current.Response.End();
}
return true;
}
}
public class GridViewReport : GridView
{
public string ReportHeader { get; set; }
public string FileName { get; set; }
public ExportTypes ExportType { get; set; }
public GridViewReport()
{
RowStyle.VerticalAlign = VerticalAlign.Top;
CssClass = "clsReportStyle";
}
}
Please remember to vote if could help so that others may consider as an answer...
Regards,