c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中
更新時間:2017年03月24日 08:47:25 作者:Resources
本文主要介紹了c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中的方法。具有很好的參考價值。下面跟著小編一起來看下吧
話不多說,請看代碼:
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() + "化驗單據(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", "化驗單據(jù)(" + qua_no.Trim()+ ").xls"); } }
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
C#中通過使用Connection類來實現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實例
今天小編就為大家分享一篇關(guān)于C#中通過使用Connection類來實現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10C#?將數(shù)據(jù)庫SqlServer數(shù)據(jù)綁定到類中的過程詳解
本文講述的是讀取數(shù)據(jù)庫中數(shù)據(jù)的常用做法,即將數(shù)據(jù)庫中的數(shù)據(jù)綁定到創(chuàng)建的類中,再將類綁定到DataGridView的數(shù)據(jù)源中的做法,對C#將SqlServer數(shù)據(jù)綁定到類中感興趣的朋友一起看看吧2022-06-06