服务器之家

服务器之家 > 正文

ASP.Net动态读取Excel文件最简方法

时间:2022-02-23 14:45     来源/作者:彬菌

注意:页面分别拖拽一个FileUpload、Button1、Label1、GridView控件,并新建一个UploadedExcel文件夹

Default.aspx.cs代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部