ASP.Net動(dòng)態(tài)讀取Excel文件最簡(jiǎn)方法
注意:頁(yè)面分別拖拽一個(gè)FileUpload、Button1、Label1、GridView控件,并新建一個(gè)UploadedExcel文件夾
Default.aspx.cs代碼:
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { delete(); } protected void Button1_Click(object sender, EventArgs e) { OleDbConnection conn = new OleDbConnection(); OleDbCommand cmd = new OleDbCommand(); OleDbDataAdapter da = new OleDbDataAdapter(); DataSet ds = new DataSet(); string query = null; string connString = ""; string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss"); //string strFileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName); string strFileType = Path.GetExtension(FileUpload1.FileName).ToString().ToLower(); if (strFileType == ".xls" || strFileType == ".xlsx") { FileUpload1.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType)); } else { return; } string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType); if (strFileType.Trim() == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } else if (strFileType.Trim() == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } query = "SELECT * FROM [Sheet1$]"; conn = new OleDbConnection(connString); if (conn.State == ConnectionState.Closed) { conn.Open(); } try { cmd = new OleDbCommand(query, conn); da = new OleDbDataAdapter(cmd); ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); Label1.Text = "讀取成功"; } catch (Exception ex) { Label1.Text = "讀取失敗"; Response.Write(ex); } finally { da.Dispose(); conn.Close(); conn.Dispose(); } } //定時(shí)任務(wù) private void delete() { DirectoryInfo di = new DirectoryInfo(Server.MapPath("/UploadedExcel/")); FileInfo[] fi = di.GetFiles("*." + "*"); DateTime dtNow = DateTime.Now; foreach (FileInfo tmpfi in fi) { TimeSpan ts = dtNow.Subtract(tmpfi.LastWriteTime); if (ts.Milliseconds > 100) { tmpfi.Attributes = FileAttributes.Normal; tmpfi.Delete(); } } } } }
注意:FileUpload控件并不能直接獲取到文件的絕對(duì)路徑(IE6及以下除外),只能通過上傳到服務(wù)器再進(jìn)行數(shù)據(jù)加載,然后再刪除
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問題的解決
這篇文章主要給大家介紹了關(guān)于C#(.net)中按字節(jié)數(shù)截取字符串最后出現(xiàn)亂碼問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06C#連接數(shù)據(jù)庫(kù)和更新數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#連接數(shù)據(jù)庫(kù)和更新數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2015-08-08Visual C#.Net 網(wǎng)絡(luò)程序開發(fā)-Socket篇
Visual C#.Net 網(wǎng)絡(luò)程序開發(fā)-Socket篇...2007-03-03C#結(jié)合JavaScript實(shí)現(xiàn)多文件上傳功能
在許多應(yīng)用場(chǎng)景里,多文件上傳是一項(xiàng)比較實(shí)用的功能,本文主要為大家詳細(xì)介紹了C#如何結(jié)合JavaScript實(shí)現(xiàn)多文件上傳功能,感興趣的小伙伴可以了解下2023-12-12Unity Shader實(shí)現(xiàn)徑向模糊效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)徑向模糊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng),分為前景、場(chǎng)景、背景等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09