SoFunction
Updated on 2025-03-01

c# Export Datatable data into Excel table

Without further ado, please see the code:

public FileResult GetExcelFile()
    {
      if (Session["beginDate"] != null)
      {
        string bdate = Session["beginDate"].ToString();
        DateTime ld = (Session["lastDate"].ToString());
        DateTime ldate = (1);
        string lldate = ("yyyy-MM-dd");
        int ptkey = Convert.ToInt32(Session["Process_PTR"]);
        string proceSql = "select * from Assay_Process where RKEY="+ptkey;
        DataTable proceDt = (proceSql);
        IDataParameter[] iDataTitle = new SqlParameter[3];
        iDataTitle[0] = new SqlParameter("@BeginDate", bdate);
        iDataTitle[1] = new SqlParameter("@LastDate", lldate);
        iDataTitle[2] = new SqlParameter("@RKEY", ptkey);
        DataSet dtTitle = ("pro_GetAssalyInfoByExportPrint", iDataTitle);
        DataTable dt = [0];
        string outStr = (dt);
        byte[] fileContents = Encoding.(());
        return File(fileContents, "application/ms-excel", "" + [0]["ProcessName"].ToString().Trim() + "Test documents(" + () + "-" + () + ").xls"); 
      }
      else
      {
        string qua_no = Session["quano"].ToString();
        IDataParameter[] iDataTitle = new SqlParameter[1];
        iDataTitle[0] = new SqlParameter("@Qua_no", qua_no);
        DataSet dtTitle = ("pro_GetAssalyInfoByQua_No", iDataTitle);
        DataTable dt = [0];
        string outStr = (dt);
        byte[] fileContents = Encoding.(());
        return File(fileContents, "application/ms-excel", "Test documents(" + qua_no.Trim()+ ").xls"); 
      }
    }

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!