Bekijk de onderstaande video om te zien hoe je onze site als een web app op je startscherm installeert.
Opmerking: Deze functie is mogelijk niet beschikbaar in sommige browsers.
Imports Microsoft.Office.Interop
Public Sub Import(ByVal fileName as String, ByRef table as DataTable)
Dim connectionInfo As String = String.Format(CultureInfo.InvariantCulture, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;ReadOnly=0;IMEX=1;'", _
fileName)
Dim conn As New OleDbConnection()
conn.ConnectionString = connectionInfo
conn.Open()
table = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
conn.Close()
End
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim sFilePath, sFileName As String
Dim sSlash As Single
With OpenFileDialog1
.Title = "Import Excel file"
.InitialDirectory = "C:\Documents and Settings\htran\My Documents\vbnet\Welcome"
.Filter = "File (*.xls;*.csv;*.txt)|*.xls;*.csv;*.txt|All files (*.*)|*.*"
.ShowDialog()
sSlash = InStrRev(.FileName, "\")
sFilePath = Mid(.FileName, 1, sSlash)
sFileName = Mid(.FileName, sSlash + 1, Len(.FileName))
End With
Dim strConnection As System.Data.OleDb.OleDbConnection
Dim myPath As String = sFilePath & sFileName
Try
Dim objDataSet As System.Data.DataSet
Dim objAdapter As System.Data.OleDb.OleDbDataAdapter
strConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & myPath & " '; " & "Extended Properties=Excel 8.0;")
objAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Artikel1$]", strConnection)
objDataSet = New System.Data.DataSet
objAdapter.Fill(objDataSet, "Table")
ArtikelenDataGridView.DataSource = objDataSet.Tables(0).DefaultView
strConnection.Close()
Catch ex As Exception
' Dim er As String
'strConnection.Close()
End Try
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim connect As System.Data.OleDb.OleDbConnection
Dim adapter As System.Data.OleDb.OleDbDataAdapter
Dim dataset As New System.Data.DataSet()
Dim opendlg As New OpenFileDialog
opendlg.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
If opendlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim pathname As String = opendlg.FileName
'MsgBox(pathname)
connect = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & "data source=" & pathname & ";Extended Properties=Excel 8.0;")
adapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Artikelen1$]", connect)
connect.Open()
adapter.Fill(dataset)
Me.ArtikelenDataGridView.DataSource = dataset.Tables(0)
adapter.Fill(dataset.Tables(0))
connect.Close()
MsgBox(CType(dataset.Tables(0).Rows(0).Item(0), Object).ToString)
End If
End Sub
Private Sub importFile(ByVal fileName As String)
Dim connectionInfo As String = String.Format(CultureInfo.InvariantCulture, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;ReadOnly=0;IMEX=1;'", _
fileName)
Dim conn As New OleDbConnection()
conn.ConnectionString = connectionInfo
conn.Open()
Dim dtWorksheets As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim sheetName As String = String.Empty
For Each row As DataRow In dtWorksheets.Rows
sheetName = row("TABLE_NAME").ToString()
Exit For
Next
Dim importTable As New DataTable()
importTable.Locale = CultureInfo.InvariantCulture
Dim strSQL As String = String.Format(CultureInfo.InvariantCulture, "SELECT * FROM [{0}]", sheetName)
Dim da As New OleDbDataAdapter(strSQL, conn)
da.Fill(importTable)
conn.Close()
conn.Dispose()
End
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Exporteer_naarExcel()
End Sub
Private Sub LoadArtikel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadArtikel.Click
Load_Atrikelen()
End Sub
Private Sub Load_Atrikelen()
Dim opendlg As New OpenFileDialog
opendlg.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
If opendlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim pathandfile As String = opendlg.FileName
Dim connection As OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & "data source=" & pathandfile & ";Extended Properties=Excel 8.0;")
Dim Command = New OleDb.OleDbCommand("select * from [Artikel1$]", connection)
connection.Open()
Dim dr As Data.IDataReader = Command.ExecuteReader
Dim bulkCopy As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ArtikelSparklz.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
bulkCopy.DestinationTableName = "Artikelen"
bulkCopy.WriteToServer(dr)
Dim adapter As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Artikel1$]", connection)
adapter.Fill(Me.ArtikelimportDataSet1.Artikelen)
connection.Close()
End If
End Sub
Private Sub Exporteer_naarExcel()
Try
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
'Start Excel and get Application object.
oXL = New Excel.Application
' Get a new workbook.
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oXL.Visible = True
Dim r As Integer
Dim c As Integer
For r = 0 To Me.ArtikelenDataGridView1.Rows.Count - 1
For c = 0 To Me.ArtikelenDataGridView1.Rows(r).Cells.Count - 1
Dim s As String = Me.ArtikelenDataGridView1.Rows(r).Cells(c).Value
oSheet.Cells(r + 1, c + 1).Value = s
Next
Next
'Make sure Excel is visible and give the user control
'of Microsoft Excel's lifetime.
oXL.Visible = True
oXL.UserControl = True
'Make sure you release object references.
oRng = Nothing
oSheet = Nothing
oWB = Nothing
oXL = Nothing
Catch ex As Exception
MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
End Try
End Sub
We gebruiken essentiële cookies om deze site te laten werken, en optionele cookies om de ervaring te verbeteren.