winfrom匯入excel檔案

2022-07-26 01:00:23 字數 2325 閱讀 8216

private dataset xsldata()            //定義乙個dataset並把excel資料讀入dataset

else

return null;

}//string tablename = string.empty;

//動態匯入很多excel**的時候,他們的表名可能不一樣,總不能每次都輸入一次表名吧,其它不需要的只要用下面的方法就可以得到表名

//tablename = conn.getoledbschematable(oledbschemaguid.tables, null).rows[0][2].tostring().trim();

特別注意

extended properties='excel 8.0;hdr=yes;imex=1'

a:hdr ( header row )設定

若指定值為yes,代表 excel 檔中的工作表第一行是欄位名稱

若指定值為 no,代表 excel 檔中的工作表第一行就是資料了,沒有欄位名稱

b:imex ( import export mode )設定

imex 有三種模式,各自引起的讀寫行為也不同,容後再述:

0 is export mode

1 is import mode

2 is linked mode (full update capabilities)

這裡特別要說明的就是 imex 引數了,因為不同的模式代表著不同的讀寫行為:

當 imex=0 時為「匯出模式」,這個模式開啟的 excel 檔案只能用來做「寫入」用途。

當 imex=1 時為「匯入模式」,這個模式開啟的 excel 檔案只能用來做「讀取」用途。

當 imex=2 時為「鏈結模式」,這個模式開啟的 excel 檔案可同時支援「讀取」與「寫入」用途。

//匯入excel     

private void toolstripbutton1_click(object sender, eventargs e)

不執行else}}

catch (exception ex)

匯入csv 檔案 繫結:

方式一:

string mystring = @"provider=microsoft.jet.oledb.4.0;data source=" + directory.getparent(filepath) + ";extended properties='text;fmt=delimited;hdr=yes;imex=1;'";          

//string funame = fuexcel.filename;            

string funame = filepath.substring(filepath.lastindexof("\\") + 1);            

string query = "select * from [" + funame + "]";

oledbconnection cnnxls = new oledbconnection();       

cnnxls.connectionstring = mystring;            

cnnxls.open();//開啟連線            

oledbdataadapter oleadapter = new oledbdataadapter(query, cnnxls);       

dataset ds = new dataset();           

oleadapter.fill(ds);         

dtresult = dtreaderebay(ds);           /讀取物件

dgbom.datasource = dtresult;            

dgbom.databind();            

ds.dispose();           

cnnxls.close(); 

方式二:

using (streamreader sr = new streamreader(filepath, system.text.encoding.getencoding("gb2312")))

//dvtable = dtreaderebay(db);

dtebay.rows.removeat(0);

dgbom.datasource = dtebay;

dgbom.databind();

}   

///

/// 檔案上傳

///

///

前台:

Excel資料匯入

excel資料匯入 你hold住麼 一 在本篇將帶領大家一同了解 匯入excel流程和問題解決方案 需要注意的是在server端的web config 中新增這幾行 ps 中value 是根據自己的專案中的資料夾的命名而定,可參考以下截圖 由於在server的配置檔案的檔名不一致。找不excel的x...

excel匯入匯出

匯出 yii框架中 header content disposition attachment filename 123.xls arr connection createcommand select from recruit queryall array this db get recruit r...

Excel資料匯入

private void button3 click object sender,eventargs e 連線excel的連線字串,excel2007版本以上的 hdr yes代表第一行是列名 string strconn provider microsoft.ace.oledb.12.0 data...