国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

ASP.Net動態讀取Excel文件最簡方法

2020-01-24 00:18:14
字體:
來源:轉載
供稿:網友

注意:頁面分別拖拽一個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及以下除外),只能通過上傳到服務器再進行數據加載,然后再刪除

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 犍为县| 兰溪市| 华亭县| 麻江县| 宜城市| 安吉县| 永泰县| 股票| 子长县| 莲花县| 酒泉市| 盐城市| 绥宁县| 武胜县| 武山县| 依安县| 浑源县| 二连浩特市| 平昌县| 岳池县| 江源县| 萨迦县| 秭归县| 交城县| 浦县| 英吉沙县| 百色市| 甘孜县| 湾仔区| 承德县| 宁陵县| 济源市| 三门县| 武冈市| 小金县| 闻喜县| 河池市| 昭苏县| 阿巴嘎旗| 建昌县| 樟树市|