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及以下除外),只能通過(guò)上傳到服務(wù)器再進(jìn)行數(shù)據(jù)加載,然后再刪除
相關(guān)文章
在C#中獲取端口號(hào)與系統(tǒng)信息的高效實(shí)踐
在現(xiàn)代軟件開(kāi)發(fā)中,尤其是系統(tǒng)管理、運(yùn)維、監(jiān)控和性能優(yōu)化等場(chǎng)景中,了解計(jì)算機(jī)硬件和網(wǎng)絡(luò)的狀態(tài)至關(guān)重要,C# 作為一種廣泛應(yīng)用的編程語(yǔ)言,提供了豐富的 API 來(lái)幫助開(kāi)發(fā)者獲取計(jì)算機(jī)的硬件信息和網(wǎng)絡(luò)狀態(tài),本篇博客將帶你深入探索如何在 C# 中高效獲取端口號(hào)和系統(tǒng)信息2025-01-01
C#基礎(chǔ)教程之IComparable用法,實(shí)現(xiàn)List<T>.sort()排序
這篇文章主要介紹了C#的一些基礎(chǔ)知識(shí),主要是IComparable用法,實(shí)現(xiàn)List<T>.sort()排序,非常的實(shí)用,這里推薦給大家。2015-02-02
C#單例模式Singleton的實(shí)現(xiàn)詳解
單例模式(Singleton?Pattern)是日常開(kāi)發(fā)中最簡(jiǎn)單的設(shè)計(jì)模式之一,它提供了一種創(chuàng)建對(duì)象的最佳方式,本文主要為大家介紹的是C#單例模式的實(shí)現(xiàn)方法,需要的可以參考一下2023-05-05
C# WinForm中Panel實(shí)現(xiàn)用鼠標(biāo)操作滾動(dòng)條的實(shí)例方法
由于在WinForm中Panel不能直接響應(yīng)鼠標(biāo)的滾動(dòng)事件,只好采用捕獲窗體的滾動(dòng)事件。2013-03-03
C#?DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹(shù)狀模型數(shù)據(jù)
NineRays.WebControls.FlyTreeView?是?9rays.net?推出的一款功能強(qiáng)大的樹(shù)狀模型數(shù)據(jù)顯示控件,本文主要介紹了如何使用其并結(jié)合?DataSet對(duì)象進(jìn)行數(shù)據(jù)顯示,感興趣的可以了解下2024-04-04
C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB詳解
在本篇文章里小編給大家整理的是關(guān)于C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-08-08
C#?System.Linq提供類似SQL語(yǔ)法的高效查詢操作
System.Linq是C#的一個(gè)命名空間,提供了LINQ(語(yǔ)言集成查詢)功能,允許開(kāi)發(fā)者使用一致的查詢語(yǔ)法來(lái)處理不同類型的數(shù)據(jù)源,如數(shù)組、集合、數(shù)據(jù)庫(kù)和XML等,本文介紹C#?System.Linq提供類似SQL語(yǔ)法的高效查詢操作,感興趣的朋友一起看看吧2024-09-09
C#實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
c# Newtonsoft 六個(gè)值得使用的特性(上)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
小白2分鐘學(xué)會(huì)Visual Studio如何將引用包打包到NuGet上
這篇文章主要介紹了小白2分鐘學(xué)會(huì)Visual Studio如何將引用包打包到NuGet上,只需兩步完成打包上傳操作,需要的朋友可以參考下2021-09-09

