|
|
#2
fairy42008-03-11 09:11
2種方法,
1:將excel當作一個數據庫,再對它經行操作,有一定的難度,但是絕對可以實現,我用就做出來過,網上這樣的資料不多!但是感覺起來舒服些,代碼效率也高 2:打開EXCEL,判斷使用了多少行和列,加載到數組,再轉換成表,將數據源設置為該表!這方法比較笨,但是實現較容易!效率不怎樣 以下是方法2部分代碼: dim tblList As DataTable Dim ArrayCells(,) As Object Dim xlRange As Excel.Range Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet 打開excel。。。 xlRange = xlSheet.UsedRange ReDim ArrayCells(xlRange.Rows.Count, xlRange.Columns.Count) intX = xlRange.Columns.Count 關閉excel。。。。 Call CreateColumnList(intX) For r As Integer = 0 To UBound(ArrayCells, 1) - 1 rw01 = tblList.NewRow For c As Integer = 0 To UBound(ArrayCells, 2) - 1 rw01.Item("列" & c + 1) = CStr(ArrayCells(r + 1, c + 1)) '.ToString() Next tblList.Rows.Add(rw01) Next DataGrid0.DataSource = tblList 函數: Public Sub CreateColumnList(ByVal cols As Integer) Dim newTempCol As DataColumn tblList = New DataTable For x As Integer = 0 To cols - 1 newTempCol = New DataColumn newTempCol.AllowDBNull = True newTempCol.ColumnName = "列" & x + 1 newTempCol.DataType = System.Type.GetType("System.String") tblList.Columns.Add(newTempCol) Next tblList.TableName = "DataList" End Sub |
格式是这样的
aa aa aa aa aa aa
bc bz bf
hh kk ll
pp oo uu
如何一次性复制EXCEL上面的数据粘贴到DATAGRIDVIEW中?