SoFunction
Updated on 2025-03-01

C# Export Excel using Datatable

This example shares the specific code of C# using Datatable to export Excel for your reference. The specific content is as follows

using ;
using ;
using System;
using ;
using ;
using ;
using ;
using ;
namespace 
{
 /// <summary>
 ///title call information /// </summary>
 public class ChartTitle
 {
  /// <summary>
  /// Call name  /// </summary>
  public string CallName { get; set; }
  /// <summary>
  /// Header information  /// </summary>
  public List<ChartName> SetNameList { get; set; }
 }
 public class ChartName
 {
  /// <summary>
  /// Level 1 header  /// </summary>
  public string Name { get; set; }
  /// <summary>
  /// Secondary header  /// </summary>
  public List<string> ChartChildNameList { get; set; }

 }

 public class ExcelHelper
 {

  //
  /// <summary>
  /// Datatable exports Excel  /// </summary>
  /// <param name="dt">Data</param>  /// <param name="list">Table header</param>  /// <param name="counts">Total number of entries</param>  /// <param name="filePath">Save address</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static MemoryStream WriteExcel(DataTable dt, List&lt;ChartTitle&gt; objlist, string filePath)
  {
   var list = (a =&gt;  == "ccc").SetNameList;
   if (!(filePath) &amp;&amp; null != dt &amp;&amp;  &gt; 0)
   {
    XSSFWorkbook book = new XSSFWorkbook();
     sheet = ();
    ICell cell = null;
    ICellStyle style = ();
    IFont font = ();//Create font style    IFont fonts = ();//Create font style    IFont fontss = ();//Create font style     = ;//Set font color    ICellStyle styles = (); //red     = ;
    ICellStyle styless = (); //green     = ;
    ICellStyle stylesss = (); //blue    (fonts);
    (font);
    (fontss);
     = ;
     row1 = (0);
     row2 = (1);
    var row = row1;
    int num = 0;//Record column    var d = true;//Is it loaded to the secondary form for the first time    #region Create a table header    //Two rows and single columns    for (int i = 0; i &lt; ; i++)
    {
     if (!d)
     {
      row = row1;
      d = true;
     }
     if (list[i].ChartChildNameList == null)
     {
      cell = (num);
      (num, 15 * 256);
      (list[i].Name);
       = style;
      (new (0, 1, num, num));
      num++;
      continue;
     }
     //Two rows and multiple columns     var count = list[i].; //Number of columns     cell = (num);
     (list[i].Name);
      = style;
     (new (0, 0, num, count + num - 1));
     if (d)
     {
      d = false;
      row = row2;
     }
     var t = 0;
     var rang = (1);
     for (int j = 0; j &lt; count; j++)
     {
      (num + t, 5 * 256);
      cell = (num + t);
      (list[i].ChartChildNameList[j]);
       = style;
      t++;
     }
     num = num + count;
    }
    #endregion Create a table header    #region Write data    int customs = 0;//Record column count    int clouns = 0;//Record number of rows


    filePath = (filePath, "XXX");
    #region Export Operation    var data1 = &lt;List&lt;Class Name&gt;&gt;([0][0].ToString());
    foreach (var item in data1)
    {
      rows = (clouns + 2);
     customs = 0;
     (customs).SetCellValue(()); customs++;
     (customs).SetCellValue(()); customs++;
     for (int i = 0; i &lt; ; i++)
     {
      if ((',')[0] == [i].ToString())
      {
       cell = (customs);
       ([i]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([i]); customs++;
      }
     }
     for (int j = 0; j &lt; ; j++)
     {
      if ((',')[1] == [j].ToString())
      {
       cell = (customs);
       ([j]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([j]); customs++;
      }
     }
     for (int k = 0; k &lt; ; k++)
     {
      if ((',')[2] == [k].ToString())
      {
       cell = (customs);
       ([k]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([k]); customs++;
      }
     }
     for (int l = 0; l &lt; ; l++)
     {
      if ((',')[3] == ([l].ToString()))
      {
       cell = (customs);
       ([l]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([l]); customs++;
      }
     }
     for (int m = 0; m &lt; ; m++)
     {
      if ((',')[4] == ([m].ToString()))
      {
       cell = (customs);
       ([m]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([m]); customs++;
      }
     }
     for (int n = 0; n &lt; ; n++)
     {
      if (([n].ToString()))
      {
       cell = (customs);
       ([n]);
        = styles;
       customs++;
      }
      else
      {
       (customs).SetCellValue([n]); customs++;
      }

     }
     clouns++;
    }

    #endregion


    #endregion Write data    // Write to the client    using ( ms = new ())
    {
     (ms);
     using (FileStream fs = new FileStream(filePath, , ))
     {
      byte[] data = ();
      (data, 0, );
      ();
     }
     book = null;
     return ms;
    }
   }
   return null;
  }



 }
}

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.