使用OleDb,將Excel匯入DataSet

2021-08-22 01:25:02 字數 924 閱讀 3257

本方法,將傳入的excel檔案內所有的sheet內的資料都填充入dataset中。這是乙個簡單快捷的方法,不足之處是不適合帶有格式複雜的excel檔案。(比如:有合併單元格的)

public class excelutil ';extended properties=excel 8.0;"; //if (firstrowisheader) // strconn += "hdr=yes;"; strconn = string.format(strconn, filename); using (var conn = new oledbconnection(strconn)) ]"; var adapter = new oledbdataadapter(); adapter.selectcommand = conn.createcommand(); if (string.isnullorempty(sheetname)) ); foreach (datarow row in excelschema.rows) } else quot;); adapter.selectcommand.commandtext = sql; adapter.fill(ds, sheetname); } conn.close(); } return ds; } }

上面**中46行的 "quot;" 應為 "$"

excel查詢的時候,加上範圍也可以。這樣就能指定範圍獲得資料。比如:

(1) string sql = "select * from [a:c]";

(2) string sql = "select * from [a1:c100]";

使用:private void btnexcel2data_click(object sender, eventargs e) }

使用OleDb,將Excel匯入DataSet

本方法,將傳入的excel檔案內所有的sheet內的資料都填充入dataset中。這是乙個簡單快捷的方法,不足之處是不適合帶有格式複雜的excel檔案。using system.data using system.data.oledb using system.data.odbc public da...

利用OleDb匯入Excel資料丟失問題及解決方法

在開發的應用中,對於excel資料匯入,一直都利用oledb,以前都收到使用者反饋有啥問題。但這幾天搞乙個專案,也用到了這個東西,還在測試階段就發現出了問題 經常會出現某一字段不允許有空值。按常規的認識,肯定是該行中某列的資料沒有填寫,但檢測excel表,並未發現有此情況。然後就是各種折騰,最終發現...

使用OLEDB將資料庫的資料匯入Excel檔案

datatable中的資料匯入到excel,使用oledb,資料來源table excel檔案的完整路徑 excel sheet的名稱 匯入執行的結果 public bool datatabletoexcel datatable dt,string excelpath,string sheetnam...