SoFunction
Updated on 2025-03-07

C# implements GridView to export Excel instance code

Exporting Excel is often used in many projects. I introduced the C# implementation of GridView to export Excel instance code, and I also left myself a study note.

using ;
using ;
using ;
using ;
using ;
using ;

namespace 
{
 /// <summary>
 /// Summary description for GridViewExport
 /// </summary>
 public class GridViewExport
 {
  public GridViewExport()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  public static void Export(string fileName, GridView gv)
  {
   ();
   (
    "content-disposition", ("attachment; filename={0}", fileName));
    = "application/ms-excel";
   // = "utf-8";


   using (StringWriter sw = new StringWriter())
   {
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
     // Create a form to contain the grid
     Table table = new Table();
      = ; //Add solid lines between cells
     // add the header row to the table
     if ( != null)
     {
      PrepareControlForExport();
      ();
     }

     // add each of the data rows to the table
     foreach (GridViewRow row in )
     {
      PrepareControlForExport(row);
      (row);
     }

     // add the footer row to the table
     if ( != null)
     {
      PrepareControlForExport();
      ();
     }

     // render the table into the htmlwriter
     (htw);

     // render the htmlwriter into the response
     (());
     ();
    }
   }
  }

  /// <summary>
  /// Replace any of the contained controls with literals
  /// </summary>
  /// <param name="control"></param>
  private static void PrepareControlForExport(Control control)
  {
   for (int i = 0; i < ; i++)
   {
    Control current = [i];
    if (current is LinkButton)
    {
     (current);
     (i, new LiteralControl((current as LinkButton).Text));
    }
    else if (current is ImageButton)
    {
     (current);
     (i, new LiteralControl((current as ImageButton).AlternateText));
    }
    else if (current is HyperLink)
    {
     (current);
     (i, new LiteralControl((current as HyperLink).Text));
    }
    else if (current is DropDownList)
    {
     (current);
     (i, new LiteralControl((current as DropDownList).));
    }
    else if (current is CheckBox)
    {
     (current);
     (i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
    }

    if (())
    {
     PrepareControlForExport(current);
    }
   }
  }


  /// <summary>
  /// Export Grid data (all) to Excel  ///Available when all fields are of BoundField type  /// If the field is TemplateField template type, data cannot be retrieved  /// </summary>
  /// <param name="grid">grid ID</param>  /// <param name="dt">Data source</param>  /// <param name="excelFileName">The file name to export Excel</param>  public static void OutputExcel(GridView grid, DataTable dt, string excelFileName)
  {
   Page page = (Page);
   ();
   string fileName = (.(excelFileName));
   ("Content-Disposition", "attachment:filename=" + fileName + ".xls");
    = "application/-excel";
    = "utf-8";

   StringBuilder s = new StringBuilder();
   ("&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;" + fileName + "&lt;/TITLE&gt;&lt;META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"&gt;&lt;/head&gt;&lt;body&gt;");

   int count = ;

   ("&lt;table border=1&gt;");
   ("&lt;tr&gt;");
   for (int i = 0; i &lt; count; i++)
   {

    if ([i].GetType() == typeof(BoundField))
     ("&lt;td&gt;" + [i].HeaderText + "&lt;/td&gt;");

    //("&lt;td&gt;" + [i].HeaderText + "&lt;/td&gt;");

   }
   ("&lt;/tr&gt;");

   foreach (DataRow dr in )
   {
    ("&lt;tr&gt;");
    for (int n = 0; n &lt; count; n++)
    {
     if ([n].Visible &amp;&amp; [n].GetType() == typeof(BoundField))
      ("&lt;td&gt;" + dr[((BoundField)[n]).DataField].ToString() + "&lt;/td&gt;");

    }
    ("&lt;/tr&gt;");
   }

   ("&lt;/table&gt;");
   ("&lt;/body&gt;&lt;/html&gt;");

   (("utf-8").GetBytes(()));
   ();
  }

 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.