1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
Public Class ProductsByCategory
Inherits DbBase
Public Sub New(ByVal path As String)
MyBase.New(path)
End Sub
Public Function CreateProductsByCategory() As Workbook
DBInit()
Dim designerFile As String = path + "\\Designer\\Northwind.xls"
Dim workbook As workbook = New workbook()
workbook.Open(designerFile)
Me.oleDbDataAdapter1.SelectCommand.CommandText = "SELECT Categories.CategoryName, Products.ProductName, Products.QuantityPerUnit, Products.UnitsInStock, Products.Discontinued, Categories.CategoryID, Products.ProductID FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID WHERE (Products.Discontinued <> Yes) ORDER BY Categories.CategoryName, Products.ProductName"
Me.oleDbDataAdapter1.Fill(Me.dataTable1)
Dim sheet As Worksheet = workbook.Worksheets("Sheet7")
sheet.Name = "Products By Category"
Dim cells As cells = sheet.Cells
Dim vPageBreaks As VerticalPageBreakCollection = sheet.VPageBreaks
cells.SetRowHeight(4, 20.25)
cells.SetRowHeight(5, 18.75)
Dim currentRow As Integer = 4
Dim currentColumn As Byte = 0
Dim lastCategory As String = ""
Dim thisCategory As String, nextCategory As String
Dim productsCount As Integer = 0
SetProductsByCategoryStyles(workbook)
Dim i As Integer
For i = 0 To Me.dataTable1.Rows.Count - 1
thisCategory = CType(Me.dataTable1.Rows(i)("CategoryName"), String)
If thisCategory <> lastCategory Then
currentRow = 4
If i <> 0 Then
currentColumn += 4
End If
CreateProductsByCategoryHeader(workbook, cells, currentRow, currentColumn, thisCategory)
lastCategory = thisCategory
currentRow += 2
End If
cells(currentRow, currentColumn).PutValue(CType(Me.dataTable1.Rows(i)("ProductName"), String))
cells(currentRow, CType((currentColumn + 1), Byte)).PutValue(CType(Me.dataTable1.Rows(i)("UnitsInStock"), Short))
If i <> Me.dataTable1.Rows.Count - 1 Then
nextCategory = CType(Me.dataTable1.Rows(i + 1)("CategoryName"), String)
If thisCategory <> nextCategory Then
Dim style As Style = workbook.Styles("ProductsCount")
cells(currentRow + 1, currentColumn).PutValue("Number of Products:")
cells(currentRow + 1, currentColumn).Style = style
style = workbook.Styles("CountNumber")
cells(currentRow + 1, CType((currentColumn + 1), Byte)).PutValue(productsCount + 1)
cells(currentRow + 1, CType((currentColumn + 1), Byte)).Style = style
currentRow = currentRow + 1
productsCount = 0
vPageBreaks.Add(0, currentColumn + 1)
Else
productsCount = productsCount + 1
End If
Else
Dim style As Style = workbook.Styles("ProductsCount")
cells(currentRow + 1, currentColumn).PutValue("Number of Products:")
cells(currentRow + 1, currentColumn).Style = style
style = workbook.Styles("CountNumber")
cells(currentRow + 1, CType((currentColumn + 1), Byte)).PutValue(productsCount + 1)
cells(currentRow + 1, CType((currentColumn + 1), Byte)).Style = style
End If
currentRow = currentRow + 1
Next
For i = 0 To workbook.Worksheets.Count - 1
If (i >= workbook.Worksheets.Count) Then
Exit For
End If
sheet = workbook.Worksheets(i)
If sheet.Name <> "Products By Category" Then
workbook.Worksheets.RemoveAt(i)
i = i - 1
End If
Next
Return workbook
End Function
Private Sub SetProductsByCategoryStyles(ByVal workbook As Workbook)
Dim styleIndex As Integer = workbook.Styles.Add()
Dim style As style = workbook.Styles(styleIndex)
style.Font.IsItalic = True
style.Font.IsBold = True
style.Font.Size = 16
style.HorizontalAlignment = TextAlignmentType.Right
style.Name = "Category"
styleIndex = workbook.Styles.Add()
style = workbook.Styles(styleIndex)
style.Font.Size = 16
style.Font.IsBold = True
style.HorizontalAlignment = TextAlignmentType.Left
style.Name = "CategoryName"
styleIndex = workbook.Styles.Add()
style = workbook.Styles(styleIndex)
style.Font.Size = 14
style.Font.IsBold = True
style.Font.IsItalic = True
style.HorizontalAlignment = TextAlignmentType.Left
style.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Medium
style.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Medium
style.Name = "ProductName"
styleIndex = workbook.Styles.Add()
style = workbook.Styles(styleIndex)
style.Font.Size = 14
style.Font.IsBold = True
style.Font.IsItalic = True
style.HorizontalAlignment = TextAlignmentType.Right
style.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Medium
style.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Medium
style.Name = "UnitsInStock"
styleIndex = workbook.Styles.Add()
style = workbook.Styles(styleIndex)
style.Font.IsBold = True
style.Font.IsItalic = True
style.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Thin
style.Name = "ProductsCount"
styleIndex = workbook.Styles.Add()
style = workbook.Styles(styleIndex)
style.HorizontalAlignment = TextAlignmentType.Left
style.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Thin
style.Name = "CountNumber"
End Sub
Private Sub CreateProductsByCategoryHeader(ByVal workbook As Workbook, ByVal Cells As Cells, ByVal startRow As Integer, ByVal startColumn As Byte, ByVal categoryName As String)
Dim style As style = workbook.Styles("Category")
Cells(startRow, startColumn).PutValue("Category:")
Cells(startRow, startColumn).Style = style
style = workbook.Styles("CategoryName")
Cells(startRow, CType((startColumn + 1), Byte)).PutValue(categoryName)
Cells(startRow, CType((startColumn + 1), Byte)).Style = style
style = workbook.Styles("ProductName")
Cells(startRow + 1, startColumn).PutValue("Product Name")
Cells(startRow + 1, startColumn).Style = style
style = workbook.Styles("UnitsInStock")
Cells(startRow + 1, CType((startColumn + 1), Byte)).PutValue("Units In Stock:")
Cells(startRow + 1, CType((startColumn + 1), Byte)).Style = style
End Sub
End Class
|