asp.net如何读取Excel文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Text;
/// <summary>
??????? /// 讀取Excel文件
??????? /// </summary>
??????? /// <param name="sender"></param>
??????? /// <param name="e"></param>
??????? protected void btnImport_Click(object sender, EventArgs e)
??????? {
string filename = DateTime.Now.ToString("yyyyMMdd") + ".xls";
??????????? string fileSavePath = Server.MapPath("~/Exc/") + filename;
??????????? TxtFileDown.SaveAs(fileSavePath);
??????????? string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileSavePath + ";Extended Properties=Excel 8.0";
??????????? OleDbConnection cnnxls = new OleDbConnection(strConn);
??????????? OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select [工號] as UserNo,[時間] as ClockTime,'{0}' as fromfile,[地點] as ClockAdds from [Sheet1$]", filename), cnnxls);
??????????? DataSet ds = new DataSet();
??????????? try
??????????? {
??????????????? oda.Fill(ds, "Sheet1$");
??????????????? cnnxls.Close();
??????????????? int i = ds.Tables["Sheet1$"].Rows.Count;
??????????? }
??????????? catch(Exception ex)
??????????? {
??????????????? //throw ex;
??????????????? Message("請將待導入數據的Excel表格的Sheet名稱命名為Sheet1", true);
??????????????? cnnxls.Close();
??????????????? return;
??????????? }
}
?
轉載于:https://www.cnblogs.com/JETSh/p/5275188.html
總結
以上是生活随笔為你收集整理的asp.net如何读取Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多个流,简短的读和写
- 下一篇: C++指针与内存泄露