以前一直在想,excel的格式和我們的資料庫**式是很相似的,我能不能就像讀資料庫表一樣的讀取excel表裡面的內容。今天上網搜了一下,在次總結一下,方便以後的學習。
首先,我們要知道如何去讀excel。。
**如下:
其中引數 ***celfile 引數是excel表的實體地址。readexcel方法返回的是乙個dataset物件,也就是說吧excel對映成dataset,就像我們對資料庫表操作是一樣。接著 我在想,如果我需要的只是excel中的幾列值,我應該怎麼去實現,開始想法是在對excel對映是直接對它進行限制:select * from [sheet1$],把*好換成我需要的欄位名的集合。但是結果不行,好像是沒有這麼用的。。所以換了一種思維方正excel表已經對映成dataset了。我直接對dataset進行操作,就ok了。class readexcel
}
**如下;
其中學號,姓名、學分加權平均分是excel中的幾個列的資料。readexcel re = new readexcel();
dataset ds = new dataset();
ds = re.readexcel(spath);
datatable dt = new datatable();
dt = ds.tables["book1"];
datatable mydt = new datatable();
mydt.columns.add("學號", typeof(string));
mydt.columns.add("姓名", typeof(string));
mydt.columns.add("學分加權平均分", typeof(string));
for (int i = 0; i < dt.rows.count; i++)
datagridview1.datasource = mydt;
實現效果:
完整顯示excel表資料
**如下:
效果圖如下:readexcel re = new readexcel();
dataset ds = new dataset();
ds = re.readexcel(spath);
datatable dt = new datatable();
dt = ds.tables["book1"];
datagridview1.datasource = dt;
獲取datable中某行某列的資料
假設該datatable有5行和兩個字段 name phone 我如何訪問第3行的 phone 欄位的值。datatable.rows 2 1 tostring datatable.rows i j tostring i是行,j是列 索引都從0開始 使用datarow!datatable.rows ...
獲取某行某列的值
獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...
獲取某行某列的值
獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...