old Hippy
Gebruiker
- Lid geworden
- 24 mei 2008
- Berichten
- 911
Hi Allemaal
Ik exporteer mijn datagritview naar Excel.
maar het proces Excel stopt niet na het exporteren. waar door ik geen tweekeer de export kan doen.
iemand een idee hoe ik Excel afsluit na de export?????
Ik gebruik de volgen de code
Ik exporteer mijn datagritview naar Excel.
maar het proces Excel stopt niet na het exporteren. waar door ik geen tweekeer de export kan doen.
iemand een idee hoe ik Excel afsluit na de export?????
Ik gebruik de volgen de code

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Label1.Visible = True
Me.Label2.Visible = False
Me.Cursor = Cursors.WaitCursor
Export_artikelen()
End Sub
Private Sub Export_artikelen()
'verfying the datagridview having data or not
If ((ArtikelenDataGridView1.Columns.Count = 0) Or (ArtikelenDataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
'Creating dataset to export
Dim dset As New DataSet
'add table to dataset
dset.Tables.Add()
'add column to that table
For i As Integer = 0 To ArtikelenDataGridView1.ColumnCount - 1
dset.Tables(0).Columns.Add(ArtikelenDataGridView1.Columns(i).HeaderText)
Next
'add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To ArtikelenDataGridView1.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To ArtikelenDataGridView1.Columns.Count - 1
dr1(j) = ArtikelenDataGridView1.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
Dim row As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each ItemR In dt.Rows
Dim Columns As Integer = 0
For Each ItemC In dt.Columns
excel.Cells(row + 2, Columns + 1) = dt.Rows(row).Item(Columns).ToString
Columns = Columns + 1
Next
Columns = 0
row = row + 1
Me.ProgressBar1.Maximum = dt.Rows.Count
LabelCounting.Text = "Copied Data So Far :- " & row.ToString()
Me.ProgressBar1.PerformStep() 'increment your bar with each record
Next
wSheet.Columns.AutoFit()
Dim strFileName As String = "C:\ArtikelManager\Import-Atrikelen.xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName, FileFormat:=-4143)
excel.Workbooks.Open(strFileName)
excel.Visible = False
MsgBox("Export Artikelen gereed", MsgBoxStyle.Information, "Export Artikelen")
Me.ProgressBar1.Value = 0
LabelCounting.Text = "data"
Me.Cursor = Cursors.Default
End Sub