c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中
更新時(shí)間:2017年03月24日 08:47:25 作者:Resources
本文主要介紹了c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
話不多說(shuō),請(qǐng)看代碼:
public FileResult GetExcelFile()
{
if (Session["beginDate"] != null)
{
string bdate = Session["beginDate"].ToString();
DateTime ld = Convert.ToDateTime(Session["lastDate"].ToString());
DateTime ldate = ld.AddDays(1);
string lldate = ldate.ToString("yyyy-MM-dd");
int ptkey = Convert.ToInt32(Session["Process_PTR"]);
string proceSql = "select * from Assay_Process where RKEY="+ptkey;
DataTable proceDt = SqlHelper.QueryTable(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 = SqlHelper.RunProceduresByParameter("pro_GetAssalyInfoByExportPrint", iDataTitle);
DataTable dt = dtTitle.Tables[0];
string outStr = ExcelExport.outExcel(dt);
byte[] fileContents = Encoding.UTF8.GetBytes(outStr.ToString());
return File(fileContents, "application/ms-excel", "" + proceDt.Rows[0]["ProcessName"].ToString().Trim() + "化驗(yàn)單據(jù)(" + bdate.Trim() + "-" + lldate.Trim() + ").xls");
}
else
{
string qua_no = Session["quano"].ToString();
IDataParameter[] iDataTitle = new SqlParameter[1];
iDataTitle[0] = new SqlParameter("@Qua_no", qua_no);
DataSet dtTitle = SqlHelper.RunProceduresByParameter("pro_GetAssalyInfoByQua_No", iDataTitle);
DataTable dt = dtTitle.Tables[0];
string outStr = ExcelExport.outExcel(dt);
byte[] fileContents = Encoding.UTF8.GetBytes(outStr.ToString());
return File(fileContents, "application/ms-excel", "化驗(yàn)單據(jù)(" + qua_no.Trim()+ ").xls");
}
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
C#中通過(guò)使用Connection類來(lái)實(shí)現(xiàn)打開/關(guān)閉數(shù)據(jù)庫(kù)的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于C#中通過(guò)使用Connection類來(lái)實(shí)現(xiàn)打開/關(guān)閉數(shù)據(jù)庫(kù)的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
C#?將數(shù)據(jù)庫(kù)SqlServer數(shù)據(jù)綁定到類中的過(guò)程詳解
本文講述的是讀取數(shù)據(jù)庫(kù)中數(shù)據(jù)的常用做法,即將數(shù)據(jù)庫(kù)中的數(shù)據(jù)綁定到創(chuàng)建的類中,再將類綁定到DataGridView的數(shù)據(jù)源中的做法,對(duì)C#將SqlServer數(shù)據(jù)綁定到類中感興趣的朋友一起看看吧2022-06-06
如何在C#9 中使用頂級(jí)程序 (top-level)
這篇文章主要介紹了如何在C#9 中使用頂級(jí)程序 (top-level),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

