Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Me.CboSection.SelectedIndex = -1 Then Exit Sub
Windows.Forms.Cursor.Current = Cursors.WaitCursor
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel File|*.xls"
saveFileDialog1.Title = "Save an Excel File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
FillExcel(saveFileDialog1.FileName)
End If
Windows.Forms.Cursor.Current = Cursors.Default
End Sub
Private Sub FillExcel(ByVal FileName As String)
Dim str, filename1, sql1 As String
Dim col, row As Integer
Dim j, m As Integer
Dim cnt As Integer
Dim ad1 As SqlClient.SqlDataAdapter
Dim tb2 As System.Data.DataTable
str = "SELECT * from Table1"
Dim Tb As System.Data.DataTable
sql="write sql "
tb2 = New DataTable
tb2 = GetDataset(sql)
Dim columncount As Integer = tb2.Columns.Count
Dim Excel As Object = CreateObject("Excel.Application")
If Excel Is Nothing Then
MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical)
Return
End If
Try
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
Dim i As Integer = 1
For col = 0 To tb2.Columns.Count - 1
.cells(1, i).value = tb2.Columns(col).ColumnName
.cells(1, i).EntireRow.Font.Bold = True
i += 1
Next
i = 2
Dim k As Integer = 1
For col = 0 To tb2.Columns.Count - 1
i = 2
For row = 0 To tb2.Rows.Count - 1
Dim OI
.Cells(i, k).Value = tb2.Rows(row).ItemArray(col)
i += 1
Next
k += 1
Next
.ActiveCell.Worksheet.SaveAs(FileName)
End With
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
Excel = Nothing
MsgBox("Data's are exported to Excel Succesfully in '" & FileName & "'", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
For Each i As Process In pro
i.Kill()
Next
End Sub