SoFunction
Updated on 2025-03-10

DataTable exports Excel custom column names

1. Add a quote

2. Add the header of the cs file

using ;
using ;
using ;

3. The code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
public partial class WorkManage_SMT_SMTMaintain : 
{
 SQLHelper sql = new SQLHelper(["LocalConnectionString"].ToString());
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 }
 }
 protected void btnReport_Click(object sender, EventArgs e)
 {
 string strSql = (@" select ,,pcbapn,PrdType,cycle,cast((12*3600/cycle) as int) as 'rate',onlineMan,offlineMan,reserve3,ptype_desc,minsqg,maxsqg from smtmdl left join smtpicsmdl on = where pcbapn = '{0}' order by  asc, asc,PrdType asc", ());
 DataTable dt = (strSql);
 string strFileName = "SMT model information" + ("yyyyMMddHHmmss");
 ExportExcel(dt, strFileName, "SMT model information");
 }
 /// <summary>
 /// DataTable export Excel /// </summary>
 /// <param name="dt">datatable data source</param> /// <param name="strFileName">File name</param> /// <param name="strSheetName">Workbook name</param> public void ExportExcel(DataTable dt, string strFileName, string strSheetName)
 {
 HSSFWorkbook book = new HSSFWorkbook();
 ISheet sheet = (strSheetName);
 
 IRow headerrow = (0);
 ICellStyle style = ();
  = ;
  = ;
 
 HSSFRow dataRow = (HSSFRow)(0);
 string strColumns = "Host type, sub-machine type, 5E material number, production line type, CT (S), rate/12H, online manpower, outside of line manpower, total manpower, surface type, scraper lower limit, scraper upper limit";
 string[] strArry = (',');
 for (int i = 0; i &lt; ; i++)
 {
  (i).SetCellValue(strArry[i]);
  (i).CellStyle = style;
 }
 for (int i = 0; i &lt; ; i++)
 {
  dataRow = (HSSFRow)(i + 1);
  for (int j = 0; j &lt; ; j++)
  {
  string ValueType = "";
  string Value = "";
  if ([i][j].ToString() != null)
  {
   ValueType = [i][j].GetType().ToString();
   Value = [i][j].ToString();
  }
  switch (ValueType)
  {
   case ""://String type   (j).SetCellValue(Value);
   break;
   case ""://Date Type    dateV;
   (Value, out dateV);
   (j).SetCellValue(dateV);
   break;
   case ""://Boolean   bool boolV = false;
   (Value, out boolV);
   (j).SetCellValue(boolV);
   break;
   case "System.Int16"://Integral   case "System.Int32":
   case "System.Int64":
   case "":
   int intV = 0;
   (Value, out intV);
   (j).SetCellValue(intV);
   break;
   case ""://Floating point type   case "":
   double doubV = 0;
   (Value, out doubV);
   (j).SetCellValue(doubV);
   break;
   case ""://Null value processing   (j).SetCellValue("");
   break;
   default:
   (j).SetCellValue("");
   break;
  }
  (j).CellStyle = style;
  //Set width  (j, ( + 10) * 256);
  }
 }
 MemoryStream ms = new MemoryStream();
 (ms);
 ("Content-Disposition", ("attachment; filename={0}.xls", (strFileName, .UTF8)));
 (());
 ();
 book = null;
 ();
 ();
 }
}

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!