'DataGridView 列枚举型定义
Private Enum DGV_ENUM
COL_ROW
COL_NAME
COL_AGE
End Enum
'Excel 列枚举型定义
Private Enum XLS_COL_ENUM
COL_ROW
COL_NAME
COL_AGE
End Enum
Private Sub BTN_LOAD_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles BTN_LOAD.Click
Dim xlAppExcelFile As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim intColNum As Integer
Dim intRowNum As Integer
Dim blnAddRow As Boolean
'生成新的instance
xlAppExcelFile = New Excel.Application
'在已有的Excel,指定文件路径,打开Book
xlBook = xlAppExcelFile.Workbooks.Open("文件路径")
'不表示Excel
xlAppExcelFile.Visible = False
'禁止显示对话框和警告消息
xlAppExcelFile.DisplayAlerts = False
'取消任务栏中的窗口选项
xlAppExcelFile.ShowWindowsInTaskbar = False
xlSheet = xlBook.Sheets(1)
intRowNum = xlSheet.UsedRange.Rows.Count
intColNum = xlSheet.UsedRange.Columns.Count
For intXlsI As Integer = 1 To intRowNum
'Excel中名字空的话、… … 很出。
If Trim$(xlSheet.Cells(intXlsI,XLS_COL_ENUM.COL_NAME).Value).Equals(String.Empty) Then
… …
Exit For'退出整个循环(intXlsI)。
'Continue For 退出当前循环。
End If
'将Excel读取放到DataGridView
''RemoveHandler DGV.Invalidated,AddressOf DGV_Invalidated
Me.DGV.AllowUserToAddRows = False
Me.DGV.AllowUserToDeleteRows = False
Dim intRowCnt As Integer = Me.DGV_EDIT.Rows.Add()
DGV.Value(DGV_ENUM.COL_ROW,intRowCnt) = Trim$(xlSheet.Cells(intXlsI,XLS_COL_ENUM.COL_ROW).Value)
'如果DataGridView中有Combo Box控件,不能将Combo Box的Code值直接赋值,是整个值。
'例: A:小王 B:小张 不能将Code A或B,赋到Grid中的Combo Box。用[A:小王]-> OK
CMB_HIDE_NAME_LIST.SetSelectCondition("Combo Box的Code值")
DGV.Value(DGV_ENUM.COL_NAME,intRowCnt) = CMB_HIDE_NAME_LIST.SelectedItem
DGV.Value(DGV_ENUM.COL_AGE,XLS_COL_ENUM.COL_AGE).Value)
Me.DGV.AllowUserToAddRows = True
Me.DGV.AllowUserToDeleteRows = True
''AddHandler DGV.Invalidated,AddressOf DGV_Invalidated
Next intXlsI
'Excel 终了(关闭会有一定的延时)
xlSheet = Nothing
xlbook.Close()
xlBook = Nothing
xlAppExcelFile.Quit()
xlAppExcelFile = Nothing
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(xlAppExcelFile)
End Sub
'@******************************************************************************
'@ 名 称: DGV_EDIT_KeyDown
'@ 内 容: 用DEL键,删除GridView当前行
'@ 备 考:
'@******************************************************************************
'Me.DGV_EDIT.AllowUserToAddRows = True(自动添加行)
If DGV_EDIT.CurrentRow.Index <> DGV_EDIT.Rows.Count - 1 Then
If e.KeyCode = Keys.Delete Then
DGV_EDIT.Rows.RemoveAt(DGV_EDIT.CurrentRow.Index)
DGV_EDIT.Refresh()
End If
End If
End Sub 原文链接:https://www.f2er.com/vb/259954.html