Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Cursor.Current = System.Windows.Forms.Curso
rs.WaitCur
sor
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String
Dim cnn1 As SqlConnection
Dim connectionString1 As String
Dim sql1 As String
Dim filename As String
Dim xlApp As New Microsoft.Office.Interop.E
xcel.Appli
cation
Dim xlWorkBook As Excel.Workbook
Dim misValue As Object = System.Reflection.Missing.
Value
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misVal
ue)
Dim Sheet1 As New Excel.Worksheet
Dim Sheet2 As New Excel.Worksheet
Dim Sheet3 As New Excel.Worksheet
xlApp.Workbooks.Add()
'=========================
==========
==========
========
' it creates the sheet1 containing Error Report Per File Type
'=========================
==========
==========
========
connectionString = "data source='" & ComboServers.Text & "'; Initial catalog=master;Integrated Security = True;"
cnn = New SqlConnection(connectionSt
ring)
cnn.Open()
sql = "Select [File Type],[Task],count(*) as [Error Count] from master.dbo.temp_Filter_by_
error group by [File Type],[task] order by [File Type],[task]"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
Dim Excel As Object = CreateObject("Excel.Applic
ation")
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
'Export to Excel process
Try
With Excel
.SheetsInNewWorkbook = 5
.Workbooks.Add()
.Worksheets(1).Select()
CType(Excel.Workbooks(1).W
orksheets(
"Sheet1"),
Microsoft.Office.Interop.E
xcel.Works
heet).Name
= "Error Report Per File Type"
Dim i As Integer = 1
For col = 0 To ds.Tables(0).Columns.Count
- 1
.cells(1, i).value = ds.Tables(0).Columns(col).
ColumnName
.cells(1, i).EntireRow.Font.Bold = True
i += 1
Next
i = 2
Dim k As Integer = 1
For col = 0 To ds.Tables(0).Columns.Count
- 1
i = 2
For row = 0 To ds.Tables(0).Rows.Count - 1
.Cells(i, k).Value = ds.Tables(0).Rows(row).Ite
mArray(col
)
i += 1
Next
k += 1
Next
'filename = "c:\Exception Report " & Now().Year & "-" & Now().Month & "-" & Now().Day & " " & Now().Hour & " " & Now().Minute & " " & Now().Second & ".xls"
'.ActiveCell.Worksheet.Sav
eAs(filena
me)
End With
'System.Runtime.InteropSer
vices.Mars
hal.Releas
eComObject
(Excel)
'Excel = Nothing
' MsgBox("Exception errors have been exported succesfully to Excel. The report can be found at '" & filename & "'", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
'=========================
==========
==========
========
' it creates the sheet2 containing Detailed Error Report
'=========================
==========
==========
========
connectionString1 = "data source='" & ComboServers.Text & "'; Initial catalog=master;Integrated Security = True;"
cnn1 = New SqlConnection(connectionSt
ring1)
cnn1.Open()
sql1 = "Select * from master.dbo.temp_Filter_by_
error order by [Fileset ID],[Doc ID]"
Dim dscmd1 As New SqlDataAdapter(sql1, cnn1)
Dim ds1 As New DataSet
dscmd1.Fill(ds1)
'Export to Excel process
Try
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
CType(Excel.Workbooks(1).W
orksheets(
"Sheet2"),
Microsoft.Office.Interop.E
xcel.Works
heet).Name
= "Detailed Error Report"
Dim i As Integer = 1
For col = 0 To ds1.Tables(0).Columns.Coun
t - 1
.cells(1, i).value = ds1.Tables(0).Columns(col)
.ColumnNam
e
.cells(1, i).EntireRow.Font.Bold = True
i += 1
Next
i = 2
Dim k As Integer = 1
For col = 0 To ds1.Tables(0).Columns.Coun
t - 1
i = 2
For row = 0 To ds1.Tables(0).Rows.Count - 1
.Cells(i, k).Value = ds1.Tables(0).Rows(row).It
emArray(co
l)
i += 1
Next
k += 1
Next
'filename = "c:\Exception Report " & Now().Year & "-" & Now().Month & "-" & Now().Day & " " & Now().Hour & " " & Now().Minute & " " & Now().Second & ".xls"
'.ActiveCell.Worksheet.Sav
eAs(filena
me)
End With
'System.Runtime.InteropSer
vices.Mars
hal.Releas
eComObject
(Excel)
'Excel = Nothing
' MsgBox("Exception errors have been exported succesfully to Excel. The report can be found at '" & filename & "'", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
' The excel is created and opened for insert value. We most close this excel using this system
'Dim pro() As Process = System.Diagnostics.Process
.GetProces
sesByName(
"EXCEL")
'For Each i As Process In pro
'i.Kill()
'Next
'=========================
==========
==========
========
' it creates the sheet3 containing Index Errors Report (if the user checks the checkbox
'=========================
==========
==========
========
If CheckBox1.CheckState = True Then
Dim cnn2 As SqlConnection
Dim connectionString2 As String
Dim sql2 As String
connectionString2 = "data source='" & ComboServers.Text & "'; Initial catalog=master;Integrated Security = True;"
cnn2 = New SqlConnection(connectionSt
ring2)
cnn2.Open()
sql2 = "Select * from master.dbo.temp_Filter_by_
error where Task = 'Index Files' order by [Fileset ID],[Doc ID]"
Dim dscmd2 As New SqlDataAdapter(sql2, cnn2)
Dim ds2 As New DataSet
dscmd2.Fill(ds2)
Try
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
CType(Excel.Workbooks(1).W
orksheets(
"Sheet3"),
Microsoft.Office.Interop.E
xcel.Works
heet).Name
= "Index Errors Report"
Dim i As Integer = 1
For col = 0 To ds2.Tables(0).Columns.Coun
t - 1
.cells(1, i).value = ds2.Tables(0).Columns(col)
.ColumnNam
e
.cells(1, i).EntireRow.Font.Bold = True
i += 1
Next
i = 2
Dim k As Integer = 1
For col = 0 To ds2.Tables(0).Columns.Coun
t - 1
i = 2
For row = 0 To ds2.Tables(0).Rows.Count - 1
.Cells(i, k).Value = ds2.Tables(0).Rows(row).It
emArray(co
l)
i += 1
Next
k += 1
Next
'filename = "c:\Exception Report " & Now().Year & "-" & Now().Month & "-" & Now().Day & " " & Now().Hour & " " & Now().Minute & " " & Now().Second & ".xls"
'.ActiveCell.Worksheet.Sav
eAs(filena
me)
End With
'System.Runtime.InteropSer
vices.Mars
hal.Releas
eComObject
(Excel)
'Excel = Nothing
' MsgBox("Exception errors have been exported succesfully to Excel. The report can be found at '" & filename & "'", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
filename = "c:\Exception Report " & Now().Year & "-" & Now().Month & "-" & Now().Day & " " & Now().Hour & " " & Now().Minute & " " & Now().Second & ".xls"
Excel.ActiveCell.Worksheet
.SaveAs(fi
lename)
System.Runtime.InteropServ
ices.Marsh
al.Release
ComObject(
Excel)
Excel = Nothing
MsgBox("Exception errors have been exported succesfully to Excel. The report can be found at '" & filename & "'", MsgBoxStyle.Information)
' The excel is created and opened for insert value. We most close this excel using this system
Dim pro() As Process = System.Diagnostics.Process
.GetProces
sesByName(
"EXCEL")
For Each i As Process In pro
i.Kill()
Next
End Sub
Your help will be greatly appreciated.
Thanks!
Tammy
Start Free Trial