本文實(shí)例講述了ASP.NET實(shí)現(xiàn)讀取Excel內(nèi)容并在Web上顯示的方法,是非常實(shí)用的一個(gè)功能,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
點(diǎn)擊事件代碼.cs代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string strPath = "d:/test.xls";
string mystring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = '" + strPath + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
//"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
DataGrid1.DataSource = myDs.Tables[0];
DataGrid1.DataBind();
}
注意:
如果使用經(jīng)典的"Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + strPath + "';Extended Properties=Excel 8.0"會(huì)報(bào)錯(cuò):外部表不是預(yù)期的格式
這是因?yàn)椋篗icrosoft.Jet.OLEDB.4.0是Microsoft Jet引擎,這適用于2003版本(2003之前的沒有測(cè)試過,所以也不知道能向下適應(yīng)到哪個(gè)版本),而在2007中,微軟對(duì)其旗下 Access 與 Excel 的主要文件格式進(jìn)行修改,并且重命名為 .accdb(Access 2007 數(shù)據(jù)庫文件)與 .xlsx(Excel 2007 文件),因此未被 Microsoft Jet 引擎所支持,不過微軟也很快的提出了 Microsoft Office 2007 Desktop Drivers: Data Connectivity Components 來支持。
因此,解決方法就是把連接字符串中的數(shù)據(jù)提供者改為 Microsoft.ACE.OLEDB.12.0即可。
您可能感興趣的文章:- asp.net導(dǎo)出Excel亂碼的原因及解決方法
- asp.net讀取excel文件的三種方法示例
- asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例
- 三步將Asp.Net頁面輸出到EXCEL里
- ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例
- asp中把數(shù)據(jù)導(dǎo)出為excel的2種方法
- asp導(dǎo)出excel文件最簡(jiǎn)單方便的方法
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- Asp.net導(dǎo)出Excel/Csv文本格式數(shù)據(jù)的方法
- asp.net導(dǎo)出excel數(shù)據(jù)的常見方法匯總
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- asp.net中EXCEL數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的方法
- asp.net中Table生成Excel表格的方法
- 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
- asp.net頁面中如何獲取Excel表的內(nèi)容
- asp實(shí)現(xiàn)excel中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫
- 利用ASP輸出excel文件實(shí)例講解