ASP.Net动态读取Excel文件最简方法
作者:彬菌 时间:2022-12-24 16:38:57
注意:页面分别拖拽一个FileUpload、Button1、Label1、GridView控件,并新建一个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();
}
}
//定时任务
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控件并不能直接获取到文件的绝对路径(IE6及以下除外),只能通过上传到服务器再进行数据加载,然后再删除
来源:https://www.idaobin.com/archives/1207.html
标签:ASP.Net,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程获取网络连接方式及判断手机卡所属运营商的方法
2023-09-18 21:54:14
Kotlin协程launch原理详解
2023-05-19 09:36:01
用C语言实现五子棋小游戏
2023-01-19 12:56:58
![](https://img.aspxhome.com/file/2023/9/117779_0s.jpg)
android自定义imageview实现圆角图片
2023-05-12 07:12:07
![](https://img.aspxhome.com/file/2023/6/110756_0s.jpg)
Android编程实现AIDL(跨进程通信)的方法详解
2022-08-29 00:19:23
![](https://img.aspxhome.com/file/2023/0/130320_0s.jpg)
Java核心编程之文件随机读写类RandomAccessFile详解
2023-11-28 17:40:05
JAVA十大排序算法之桶排序详解
2022-11-08 01:07:47
![](https://img.aspxhome.com/file/2023/8/66678_0s.png)
Kotlin Flow数据流的3种使用场景详解
2021-06-03 09:06:18
github上的java项目怎么运行(面向小白)
2022-12-23 23:29:09
![](https://img.aspxhome.com/file/2023/8/94348_0s.png)
详解IntelliJ IDEA创建spark项目的两种方式
2023-12-05 05:48:21
![](https://img.aspxhome.com/file/2023/0/69660_0s.png)
Servlet3.0实现文件上传的方法
2023-08-15 00:52:44
![](https://img.aspxhome.com/file/2023/3/58143_0s.png)
java发送http请求并获取状态码的简单实例
2023-12-06 00:59:14
深入理解Kotlin的泛型系统
2023-09-09 00:47:30
![](https://img.aspxhome.com/file/2023/9/138679_0s.png)
Android控件之ListView用法实例详解
2023-09-11 00:39:26
![](https://img.aspxhome.com/file/2023/0/90130_0s.png)
C#中数组段用法实例分析
2022-06-11 23:08:05
android实现App活动定时自动跳转效果
2022-06-30 06:32:06
![](https://img.aspxhome.com/file/2023/7/109187_0s.png)
c语言中十六进制转二进制显示的实现方法
2023-12-17 23:21:44
Java虚拟机JVM性能优化(二):编译器
2023-12-03 21:21:57
![](https://img.aspxhome.com/file/2023/6/115916_0s.jpg)
Spring Cloud Gateway 默认的filter功能和执行顺序介绍
2021-07-01 21:31:46
![](https://img.aspxhome.com/file/2023/0/85550_0s.png)
Android Studio3.2中导出jar包的过程详解
2021-10-14 07:06:05
![](https://img.aspxhome.com/file/2023/4/137584_0s.jpg)