ASP.NET用DataSet導(dǎo)出到Excel的方法
/讀取臨時(shí)文件
GYYW.DA.Common.Base_SqlDataBase daBZDM = new GYYW.DA.Common.Base_SqlDataBase();
DataSet dsBZDM = daBZDM.GetDataSetBySql("select QCDM,MC,GG from WG_BZDM where QCDM like '02%'");
//同時(shí)將虛擬目錄下的Data作為臨時(shí)文件目錄。
string urlPath = HttpContext.Current.Request.ApplicationPath + "/Data/";
string physicPath = HttpContext.Current.Server.MapPath(urlPath);
//string fileName = Guid.NewGuid() + ".Xls";
string fileName ="DownLoad.Xls";
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + physicPath + fileName +";Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(connString);
OleDbCommand objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.Connection.Open();
//建立表結(jié)構(gòu)
objCmd.CommandText = @"CREATE TABLE Sheet1(器材代碼 varchar,名稱 varchar, 規(guī)格 varchar)";
objCmd.ExecuteNonQuery();
//建立插入動作的Command
objCmd.CommandText = "INSERT INTO Sheet1(器材代碼, 名稱,規(guī)格) VALUES (@QCDM, @MC, @GG)";
objCmd.Parameters.Clear();
objCmd.Parameters.Add(new OleDbParameter("@QCDM", OleDbType.VarChar));
objCmd.Parameters.Add(new OleDbParameter("@MC", OleDbType.VarChar));
objCmd.Parameters.Add(new OleDbParameter("@GG",OleDbType.VarChar));
//遍歷DataSet將數(shù)據(jù)插入新建的Excel文件中
foreach (DataRow row in dsBZDM.Tables[0].Rows)
{
for (int i=0; i<objCmd.Parameters.Count; i++)
{
objCmd.Parameters[i].Value = row[i];
}
objCmd.ExecuteNonQuery();
}
objCmd.Connection.Close();
//提供下載
//清除臨時(shí)文件
HttpResponse response = HttpContext.Current.Response;
response.Clear();
//為輸出作準(zhǔn)備
response.WriteFile(urlPath + fileName);
string httpHeader="attachment;filename=KCMX.Xls";
response.AppendHeader("Content-Disposition", httpHeader);
response.Flush();
//輸出完畢后清除臨時(shí)文件
string strSaveDir = "../Data/";
string strFile = Server.MapPath(strSaveDir + fileName).ToString();
//string sss = urlPath + fileName;
System.IO.File.Delete(strFile);//刪除臨時(shí)文件
response.End();
- .NET6導(dǎo)入和導(dǎo)出EXCEL
- Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出功能的實(shí)現(xiàn)方法
- ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
- asp.net DataTable導(dǎo)出Excel自定義列名的方法
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net導(dǎo)出excel的簡單方法實(shí)例
- asp.net導(dǎo)出Excel類庫代碼分享
- ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- asp.net GridView導(dǎo)出到Excel代碼
- ASP.NET MVC把表格導(dǎo)出到Excel
相關(guān)文章
asp.net(c#)實(shí)現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
有一個(gè)員工表Employee,需要保存員工照片(Photo)到數(shù)據(jù)庫(sql server)上。員工照片對應(yīng)的字段是varbinary(max),也就是要存成二進(jìn)制文件類型(這和以前討巧地存圖片文件路徑就不相同了),默認(rèn)可以為空。2011-09-09Asp.Net Core輕量級Aop解決方案:AspectCore
這篇文章主要介紹了Asp.Net Core輕量級Aop解決方案:AspectCore,需要的朋友可以參考下2017-06-06Asp.net中DataTable導(dǎo)出到Excel的方法介紹
本篇文章介紹了,Asp.net中DataTable導(dǎo)出到Excel的方法。需要的朋友參考下2013-05-05將Access數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入到SQL Server中的詳細(xì)方法實(shí)例
將Access數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入到SQL Server中的詳細(xì)方法實(shí)例,需要的朋友可以參考一下2013-03-03.Net?core?Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器的原理解析
我們經(jīng)常遠(yuǎn)程連接服務(wù)器去查看日志,比較麻煩,如果直接訪問項(xiàng)目的某個(gè)頁面就能實(shí)時(shí)查看日志就比較奈斯了,結(jié)合blazor實(shí)現(xiàn)了基本效果,這篇文章主要介紹了.Net?core?Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器,需要的朋友可以參考下2022-10-10