我有一个实际计算贷款的excel工作表......如果我们输入金额而不是。多年来,它为我们提供了未来几年将要制造的所有 EMI 的清单。
我的老板想将该 excel 工作表集成到 vb.net 表单中......我该怎么做?请帮我....
我有一个实际计算贷款的excel工作表......如果我们输入金额而不是。多年来,它为我们提供了未来几年将要制造的所有 EMI 的清单。
我的老板想将该 excel 工作表集成到 vb.net 表单中......我该怎么做?请帮我....
在您的表单中添加 aDataGrid "GridControl1"
和 a button "BtnImport_Click
:
在您的按钮中BtnImport_Click
添加此代码
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
Dim dialog As New OpenFileDialog()
dialog.Filter = "Excel files |*.xls;*.xlsx"
dialog.InitialDirectory = "C:\"
dialog.Title = "Veuillez sélectionner le fichier à importer"
'Encrypt the selected file. I'll do this later. :)
If dialog.ShowDialog() = DialogResult.OK Then
Dim dt As DataTable
dt = ImportExceltoDatatable(dialog.FileName)
GridControl1.DataSource = dt
GridControl1.Visible = True
MsgBox(" done ! ", MsgBoxStyle.Information)
End If
End Sub
并将其添加function
到您的form
:
Public Shared Function ImportExceltoDatatable(filepath As String) As DataTable
' string sqlquery= "Select * From [SheetName$] Where YourCondition";
Dim dt As New DataTable
Try
Dim ds As New DataSet()
Dim constring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filepath & ";Extended Properties=""Excel 12.0;HDR=YES;"""
Dim con As New OleDbConnection(constring & "")
con.Open()
Dim myTableName = con.GetSchema("Tables").Rows(0)("TABLE_NAME")
Dim sqlquery As String = String.Format("SELECT * FROM [{0}]", myTableName) ' "Select * From " & myTableName
Dim da As New OleDbDataAdapter(sqlquery, con)
da.Fill(ds)
dt = ds.Tables(0)
Return dt
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical)
Return dt
End Try
End Function
对你有帮助的跳