In this Article, we have Export the DataTable as a PDF/Excel/Word Format. I hope the below code is useful to us.

Aspx.cs File

Common.Report objReport = new Common.Report();
DataTable objReportTable = (DataTable)Session["ReportTable"];
if (objReportTable != null)
objReport.ShowReport(Common.ReportType.Excel, objReportTable, "TestExcel", objReportTable.Rows.Count, true);

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Net;
using System.Web.UI;
using System.Drawing;
using HOV.Common.Export;


/// <summary>
/// Common Initialization / Functionality
/// </summary>

namespace Common
{

public enum ReportType
{
    PDF,
    Word,
    Excel
}

/// <summary>
/// Create the Generic Reports
/// </summary>
public class Report
{

public void ShowReport(ReportType rtExportType, DataTable dtReportTable, string TitleLabel, int iColumnsCount, bool bLandscape)
{
try
{
if (dtReportTable != null)
{
    if (dtReportTable.Rows.Count == 0)
    {
        DataRow drw = dtReportTable.NewRow();
        foreach (DataColumn clmn in dtReportTable.Columns)
        {
            try
            {
                drw[clmn] = " ";
            }
            catch (Exception)
            {
                drw[clmn] = 0;
            }
        }
        dtReportTable.Rows.Add(drw);
    }
    DataTable correctedDS = dtReportTable;
    Export(rtExportType, correctedDS.DefaultView, TitleLabel, iColumnsCount, bLandscape);
}
}
catch (Exception ex)
{
throw ex;
}
}

private string GetTitleLabel(string strTitleLabel)
{
//Regex.Replace(strTitleLabel, @"[^~!@#$%^&*()+=:;<>/\\|'\{}[]`]", "");
//return strTitleLabel.Trim();
return strTitleLabel.Replace(" ", "").Replace("~", "").Replace("!", "").Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "").Replace("&", "").Replace("*", "").Replace("(", "").Replace(")", "").Replace("+", "").Replace("=", "").Replace(":", "").Replace(";", "").Replace("<", "").Replace(">", "").Replace("/", "").Replace("\\", "").Replace("|", "").Replace("'", "").Replace("\"", "").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "").Replace("`", "");
}

private void Export(ReportType rtExportType, DataView dvExportTable, string TitleLabel, int iColumnsCount, bool bLandscape)
{
try
{
string _sFileName = string.Empty;
HttpContext.Current.Response.Clear();
switch (rtExportType)
{
    case ReportType.PDF:
        HttpContext.Current.Response.ContentType = "application/pdf";
        _sFileName = GetTitleLabel(TitleLabel) + ".pdf";
        break;
    case ReportType.Excel:
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        _sFileName = GetTitleLabel(TitleLabel) + ".xls";
        break;
    case ReportType.Word:
        HttpContext.Current.Response.ContentType = "application/vnd.doc";
        _sFileName = GetTitleLabel(TitleLabel) + ".doc";
        break;
}
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + _sFileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.Charset = "";
switch (rtExportType)
{
    case ReportType.PDF: //Pdf Report

GridView gvRender = new GridView();
gvRender.HeaderStyle.CssClass = "reportHeader";
gvRender.HeaderStyle.Font.Bold = true;
gvRender.HeaderStyle.BackColor = Color.FromArgb(192, 202, 227);
gvRender.AlternatingRowStyle.CssClass = "arial8pt";
gvRender.AlternatingRowStyle.BackColor = ColorTranslator.FromHtml("#BAC4DD");
gvRender.RowStyle.BackColor = Color.White;
gvRender.RowStyle.ForeColor = Color.Black;
gvRender.RowStyle.Font.Bold = true;
gvRender.BorderColor = Color.Black;
gvRender.GridLines = GridLines.Both;

gvRender.DataSource = dvExportTable;
gvRender.DataBind();

      System.IO.MemoryStream PDFDocumentStream = (System.IO.MemoryStream)new ExportToPDFLib().ExportGridViewToPDF(gvRender, TitleLabel, iColumnsCount, "A4", bLandscape);
        HttpContext.Current.Response.BinaryWrite(PDFDocumentStream.ToArray());
HttpContext.Current.Response.End();
        break;
    default: //Word or Excel report

        DataGrid dg = new DataGrid();

        dg.HeaderStyle.CssClass = "reportHeader";
        dg.HeaderStyle.Font.Bold = true;
        dg.HeaderStyle.BackColor = Color.FromArgb(192, 202, 227);

        //font-family: Arial, Helvetica, sans-serif; font-size: 8pt; font-weight: bold; font-variant: normal ; color: black; background-color: #BAC4DD; text-align: left
        dg.AlternatingItemStyle.CssClass = "arial8pt";

        dg.DataSource = dvExportTable;
        dg.DataBind();

        System.IO.StringWriter swRenderDataGrid = new System.IO.StringWriter();
        HtmlTextWriter hwDataGrid = new HtmlTextWriter(swRenderDataGrid);
        dg.RenderControl(hwDataGrid);

        System.IO.StringWriter swFile = new System.IO.StringWriter();
        string HostName = HttpContext.Current.Request.Url.Host;
        swFile.Write("<img height='50px' width='200px' src=http://" + HostName + HttpContext.Current.Request.ApplicationPath + "/Images/newpchlogo-modified.jpg />");
        HttpContext.Current.Response.Write(swFile.ToString() + "<BR><BR><BR><BR><FONT size=\"4\"><B>" + TitleLabel + "</B></FONT><BR>" + swRenderDataGrid.ToString() + "<BR><BR>" + "<FONT size=\"4\"><B> PCH-Portal </B></FONT><BR><FONT size=\"4\"><B> Powered By : Hovservices </B></FONT><BR><FONT size=\"4\"><B>" + DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") + "</B></FONT><BR>");
        HttpContext.Current.Response.End();
        swRenderDataGrid.Close();
        swFile.Close();
        break;
}

}
catch (Exception ex)
{
//throw ex;
}

}
}

}

Here i cant able to give the PDF Dll. Instead of using ExportToPDFLib().ExportGridViewToPDF, we may download the shareware PDF dll and make a change and use of it. sorry for this Inconvenient code.

Comments (0)