我正在做一些涉及许多表格的项目..在每个表格中我必须使用下面的代码将excel导入数据网格..我不想复制每个表格的代码..计划创建模块以便我可以调用每个表单的功能将 excel 数据导入 datagrid 。
Try
With Form1.OpenFileDialog1
.Title = "Please open the STM_Ticket_Template"
.Filter = "Excel Files | *.xlsx"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fn1 As String = .FileName.ToString
Dim oledbCon As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fn1 + ";Extended Properties=Excel 12.0;"
conDB = New OleDb.OleDbConnection(oledbCon)
adap = New OleDb.OleDbDataAdapter("Select * From [SAM_TICKETS$]", conDB)
adap.TableMappings.Add("Table", "Excel")
dSet = New DataSet
adap.Fill(dSet)
Me.DataGridView1.DataSource = dSet.Tables(0)
End If
End With
Dim msg As String = MsgBox("Template successfully loaded", MsgBoxStyle.Information, "Creation Template")
Catch ex As Exception
MsgBox("Load Error..." + Environment.NewLine + ex.ToString)
End Try
有什么办法可以做到这一点?
任何建议表示赞赏:)