基於OleDb的Excel資料訪問

2022-07-21 06:21:09 字數 1350 閱讀 9373

比較以下兩個連線字串:

1)string connectionstr = string.format("provider=microsoft.jet.oledb.4.0;data source=;extended properties='excel 8.0;hdr=no;imex=1'", filepath);

2)string connectionstr = string.format("provider=microsoft.jet.oledb.4.0;data source=;extended properties=excel 8.0;", filepath);

預設情況下,系統認為 excel 資料來源的第一行包含可用作欄位名的列標題。如果不是這種情況,則必須

將該設定關閉,否則,第一行資料將會「消失」,而被用作欄位名稱。這可通過向連線字串的擴充套件屬性

新增可選的 hdr= 設定來完成。預設情況下(無需指定)是 hdr=yes。如果沒有列標題,則需要指定

hdr=no;提供程式將字段命名為 f1、f2 等等。因為擴充套件屬性字串現在包含了多個值,所以必須用引號

單獨包起來(注意字串中標為紅色的部分)。

**如下:

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.text;

using system.windows.forms;

using system.data.oledb;

namespace oledbexcel

private void form1_load(object sender, eventargs e)

;extended properties='excel 8.0;hdr=no;imex=1'", filepath);

string commandtext = "select * from [sheet1$]";

oledbconnection con = new oledbconnection(connectionstr);

oledbcommand cmd = new oledbcommand(commandtext, con);

datatable dt = new datatable("excelinfo");

trycatch(exception ex)

finally

this.datagridview.datasource = dt;

this.datagridview.update();}}

}參考:

OLEDB 連線 Excel 丟失資料問題

在工作發現用oledb去連線excel時會發現有些行的字段會丟失尾部的資料。上網查詢,發現原因是因為oledb缺省會以前8行的資料為基礎判斷,所以如果前面的資料長度不夠長,會引起後面的資料會丟失。解決方法 現在只能以修改登錄檔的方法解決。64位系統 hkey local machine softwa...

oledb讀取Excel資料丟失原因

產生這種問題的根源與excel isam 3 indexed sequential access method,即索引順序訪問方法 驅動程式的限制有關,excel isam 驅動程式通過檢查前幾行中實際值確定乙個 excel 列的型別,然後選擇能夠代表其樣本中大部分值的資料型別 4 也即excel ...

Oledb的方式匯出Excel

連線字串 string connectionstring provider microsoft.jet.oledb.4.0 data source excel檔案路徑及檔案全名 extended properties excel 8.0 hdr yes imex 2 定義連線物件 oledbconn...