如何獲取excel 中的 某幾個列的值

2021-05-28 07:01:55 字數 1295 閱讀 5580

以前一直在想,excel的格式和我們的資料庫**式是很相似的,我能不能就像讀資料庫表一樣的讀取excel表裡面的內容。今天上網搜了一下,在次總結一下,方便以後的學習。

首先,我們要知道如何去讀excel。。

**如下:

class readexcel 

}

其中引數 ***celfile 引數是excel表的實體地址。readexcel方法返回的是乙個dataset物件,也就是說吧excel對映成dataset,就像我們對資料庫表操作是一樣。接著 我在想,如果我需要的只是excel中的幾列值,我應該怎麼去實現,開始想法是在對excel對映是直接對它進行限制:select * from [sheet1$],把*好換成我需要的欄位名的集合。但是結果不行,好像是沒有這麼用的。。所以換了一種思維方正excel表已經對映成dataset了。我直接對dataset進行操作,就ok了。

**如下;

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中的幾個列的資料。

實現效果:

完整顯示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 獲取某行...