Dim style6 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("CellBord")
style6.Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlThin
xlWorkSheet.Cells(ro, 1) = itemView1.Items(sd).Text
xlWorkSheet.Cells(ro, 1).Style = "CellBord"
xlWorkSheet.Cells(ro, 2) = itemView1.Items(sd).SubItems(1).Text
xlWorkSheet.Cells(ro, 2).Style = "CellBord"
xlWorkSheet.Cells(ro, 3) = CDbl(itemView1.Items(sd).SubItems(2).Text)
xlWorkSheet.Cells(ro, 3).Style = "CellBord"
xlWorkSheet.Cells(ro, 4) = CDbl(itemView1.Items(sd).SubItems(3).Text)
xlWorkSheet.Cells(ro, 4).Style = "CellBord"
xlWorkSheet.Cells(ro, 5) = CDbl(itemView1.Items(sd).SubItems(4).Text)
xlWorkSheet.Cells(ro, 5).Style = "CellBord"
xlWorkSheet.Cells(ro, 6) = CDbl(itemView1.Items(sd).SubItems(5).Text)
xlWorkSheet.Cells(ro, 6).Style = "CellBord"
xlWorkSheet.Cells(ro, 7) = CDbl(itemView1.Items(sd).SubItems(6).Text)
xlWorkSheet.Cells(ro, 7).Style = "CellBord"
xlWorkSheet.Cells(ro, 8).Style = "FailText"
xlWorkSheet.Cells(ro, 8) = itemView1.Items(i).SubItems(7).Text
xlWorkSheet.Cells(ro, 8).Style = "CellBord"
xlWorkSheet.SaveAs(fileName)
xlWorkBook.Close()
xlApp.Quit()
Dim sDir As String = GetMyCustomDesktopFolderPath()
Dim fileName As String = sDir & "\" & txtExcelName.Text
Dim CountRows As Integer = 0
Dim maxNum As Integer = 33
Dim ro As Integer = 4
' If the workbooks already exist, prompt to delete.
Dim answer As MsgBoxResult
If File.Exists(fileName) = True Then
answer = MsgBox("Delete existing workbook (" & fileName & ")?", MsgBoxStyle.YesNo)
If answer = MsgBoxResult.Yes Then
If Dir(fileName) <> "" Then Kill(fileName)
Else
Exit Sub
End If
End If
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
'Setup the Excel File
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Name = "Page 1"
xlWorkSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
xlWorkSheet.PageSetup.LeftMargin = 0.1
xlWorkSheet.PageSetup.RightMargin = 0.1
xlWorkSheet.PageSetup.TopMargin = 0.5
'Set Column Width
xlWorkSheet.Columns("A").ColumnWidth = 25
xlWorkSheet.Columns("B").ColumnWidth = 35
xlWorkSheet.Columns("D").ColumnWidth = 10
xlWorkSheet.Columns("E").ColumnWidth = 10
xlWorkSheet.Columns("F").ColumnWidth = 10
xlWorkSheet.Columns("G").ColumnWidth = 10
xlWorkSheet.Columns("H").ColumnWidth = 10
'Style for Vendor Information
Dim style2 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("VendorStyle")
style2.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.WhiteSmoke)
style2.Font.Bold = True
'Style for Products that do not have sufficient quantity on hand or on order
Dim style3 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("FailText")
style3.Font.Color = Color.Red
style3.Font.Bold = True
style3.HorizontalAlignment = Excel.Constants.xlCenter
'Style for Products that do not have sufficient quantity on hand or on order
Dim style7 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("PassText")
style7.Font.Color = Color.Green
style7.Font.Bold = True
style7.HorizontalAlignment = Excel.Constants.xlCenter
'Style for Report
Dim style4 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("ReportTitle")
style4.Font.Color = Color.DarkGray
style4.Font.Size = 16
style4.Font.Bold = True
style4.HorizontalAlignment = Excel.Constants.xlLeft
'Not used at the moment
Dim style5 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("BGRead")
style5.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.WhiteSmoke)
'Style for Headers
Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
style.Font.Bold = True
style.Font.Color = Color.White
style.HorizontalAlignment = Excel.Constants.xlCenter
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray)
'Styles for Borders - only XLEdgeLEFT Works.
Dim style6 As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("CellBord")
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlThin
'Set Report & Column Titles
xlWorkSheet.Cells(1, 1).Style = "ReportTitle"
xlWorkSheet.Cells(1, 1) = "Item Summery Report Pass or Fail"
xlWorkSheet.Cells(2, 1) = "Report Date: " & myStartDate
xlWorkSheet.Cells(3, 1) = "Sku"
xlWorkSheet.Cells(3, 2) = "Name"
xlWorkSheet.Cells(3, 3) = "Sold"
xlWorkSheet.Cells(3, 4) = "On SO"
xlWorkSheet.Cells(3, 5) = "On Hnd"
xlWorkSheet.Cells(3, 6) = "On Ord"
xlWorkSheet.Cells(3, 7) = "So/Mo"
xlWorkSheet.Cells(3, 8) = "30d Fail"
xlWorkSheet.Range("A3", "H3").RowHeight = 16
xlWorkSheet.Range("A3", "H3").Style = "NewStyle"
Dim lv As Integer = itemView1.Items.Count - 1
Dim i, sd As Integer
sd = 0
Dim useSQL2 As Integer = 0
For i = 0 To lv
useSQL2 = 0
CountRows += 1
'Check if this is a header
If itemView1.Items(i).SubItems(4).Equals(System.DBNull.Value) Or itemView1.Items(sd).SubItems(4).Text = "" Then
itemView1.Items(i).SubItems(4).Text = ""
useSQL2 = 1
End If
If useSQL2 = 1 Then
'Secondary Header for Vendor Name
xlWorkSheet.Cells(ro, 1) = itemView1.Items(sd).SubItems(1).Text
xlWorkSheet.Cells(ro, 3) = itemView1.Items(sd).SubItems(2).Text
xlWorkSheet.Cells(ro, 1).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 3).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 2).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 4).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 5).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 6).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 7).Style = "VendorStyle"
xlWorkSheet.Cells(ro, 8).Style = "VendorStyle"
Else
'Actual Product Row
xlWorkSheet.Cells(ro, 1) = itemView1.Items(sd).Text
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 2) = itemView1.Items(sd).SubItems(1).Text
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 3) = CDbl(itemView1.Items(sd).SubItems(2).Text)
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 4) = CDbl(itemView1.Items(sd).SubItems(3).Text)
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 5) = CDbl(itemView1.Items(sd).SubItems(4).Text)
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 6) = CDbl(itemView1.Items(sd).SubItems(5).Text)
xlWorkSheet.Cells(ro, 1).style = "CellBord"
xlWorkSheet.Cells(ro, 7) = CDbl(itemView1.Items(sd).SubItems(6).Text)
xlWorkSheet.Cells(ro, 1).style = "CellBord"
If itemView1.Items(sd).SubItems(7).Text = "Fail" Then
xlWorkSheet.Cells(ro, 8).Style = "FailText"
Else
xlWorkSheet.Cells(ro, 8).Style = "PassText"
End If
xlWorkSheet.Cells(ro, 8) = itemView1.Items(sd).SubItems(7).Text
xlWorkSheet.Cells(ro, 1).style = "CellBord"
End If
'Check if we need to make a nother header for a second page...
If CountRows > maxNum Then
xlWorkSheet.HPageBreaks.Add(xlWorkSheet.Range("A" & (ro + 1), "H" & (ro + 1)))
xlWorkSheet.Cells(ro + 1, 1) = "Sku"
xlWorkSheet.Cells(ro + 1, 2) = "Name"
xlWorkSheet.Cells(ro + 1, 3) = "Sold"
xlWorkSheet.Cells(ro + 1, 4) = "On Sales Order"
xlWorkSheet.Cells(ro + 1, 5) = "On Hand"
xlWorkSheet.Cells(ro + 1, 6) = "On Order"
xlWorkSheet.Cells(ro + 1, 7) = "Sold / Month"
xlWorkSheet.Cells(ro + 1, 8) = "30 Day Fail"
xlWorkSheet.Range("A" & (ro + 1), "H" & (ro + 1)).RowHeight = 16
xlWorkSheet.Range("A" & (ro + 1), "H" & (ro + 1)).Style = "NewStyle"
CountRows = 0
ro += 2
Else
ro += 1
End If
sd += 1
Next
'Save here
xlWorkSheet.SaveAs(fileName)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
excelApp = GetObject(, "Excel.Application")
which connects to an existing Excel instance. However, you also need error handling to deal with the possibility that Excel isn't already open. If you'd like to go this route, I can post the full code section.style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous
style6.Borders(Excel.XlBordersIndex.xlEdgeTop).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeLeft).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlThin
style6.Borders(Excel.XlBordersIndex.xlEdgeRight).Weight = Excel.XlBorderWeight.xlThin
Imports System.Data.Odbc
Imports System.IO
Imports System.Text
Imports System
Imports System.Net
Imports System.Drawing
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data.OleDb
Imports System.Threading
itemView1.Items(sd)
and
itemView1.Items(i)