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
|
Public Class SalesByYearSubreport
Inherits DbBase
Public Sub New(ByVal path As String)
MyBase.New(path)
End Sub
Public Function CreateSalesByYearSubreport() As Workbook
DBInit()
Dim designerFile As String = path + "\\Designer\\Northwind.xls"
Dim workbook As workbook = New workbook()
workbook.Open(designerFile)
Me.oleDbSelectCommand1.CommandText = "SELECT DISTINCTROW COUNT(Orders.OrderID) AS Orders, " + _
" SUM([Order Subtotals].Subtotal) AS Sales, FORMAT(ORDERS.SHIPPEDDATE, 'yyyy/Q') AS Quarter" + _
" FROM Orders INNER JOIN [Order Subtotals] ON" + _
" Orders.OrderID = [Order Subtotals].OrderID WHERE " + _
" (orders.shippeddate IS NOT NULL) GROUP BY FORMAT(ORDERS.SHIPPEDDATE , 'yyyy/Q')"
Me.oleDbDataAdapter1.Fill(Me.dataTable1)
Dim sheet As Worksheet = workbook.Worksheets("Sheet11")
sheet.Name = "Sales By Year Subreport"
Dim cells As cells = sheet.Cells
Dim currentRow As Integer = 0
Dim totalOrders As Integer = 0
Dim totalSales As Decimal = 0.0
Dim thisYear As String = ""
SetSalesByYearSubreportStyles(workbook)
Dim i As Integer
For i = 0 To Me.dataTable1.Rows.Count - 1
If i = 0 Then
thisYear = Me.dataTable1.Rows(0)("Quarter").ToString().Substring(0, 4)
CreateSalesByYearSubreportHeader(workbook, cells, 0, thisYear)
CreateData(cells, 2, 0)
totalOrders += CType(Me.dataTable1.Rows(0)("Orders"), Integer)
totalSales += CType(Me.dataTable1.Rows(0)("Sales"), Decimal)
currentRow = 3
Else
If thisYear = Me.dataTable1.Rows(i)("Quarter").ToString().Substring(0, 4) Then
CreateData(cells, currentRow, i)
totalOrders += CType(Me.dataTable1.Rows(i)("Orders"), Integer)
totalSales += CType(Me.dataTable1.Rows(i)("Sales"), Decimal)
currentRow = currentRow + 1
If i = Me.dataTable1.Rows.Count - 1 Then
CreateFooter(workbook, cells, currentRow, totalOrders, totalSales)
End If
Else
CreateFooter(workbook, cells, currentRow, totalOrders, totalSales)
totalOrders = 0
totalSales = 0.0
currentRow = currentRow + 1
thisYear = Me.dataTable1.Rows(i)("Quarter").ToString().Substring(0, 4)
If i <> Me.dataTable1.Rows.Count - 1 Then
CreateSalesByYearSubreportHeader(workbook, cells, currentRow, thisYear)
currentRow += 2
CreateData(cells, currentRow, i)
totalOrders += CType(Me.dataTable1.Rows(i)("Orders"), Integer)
totalSales += CType(Me.dataTable1.Rows(i)("Sales"), Decimal)
currentRow = currentRow + 1
End If
End If
End If
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 <> "Sales By Year Subreport" Then
workbook.Worksheets.RemoveAt(i)
i = i - 1
End If
Next
Return workbook
End Function
Private Sub CreateFooter(ByVal workbook As Workbook, ByVal Cells As Cells, ByVal startRow As Integer, ByVal totalOrders As Integer, ByVal totalSales As Decimal)
Dim style As style = workbook.Styles("Bold")
Cells(startRow, 1).PutValue("Totals:")
Cells(startRow, 1).Style = style
Cells(startRow, 2).PutValue(totalOrders)
Cells(startRow, 3).PutValue(CType(totalSales, Double))
End Sub
Private Sub CreateData(ByVal Cells As Cells, ByVal startRow As Integer, ByVal index As Integer)
Cells(startRow, 1).PutValue(Integer.Parse(Me.dataTable1.Rows(index)("Quarter").ToString().Substring(5)))
Cells(startRow, 2).PutValue(CType(Me.dataTable1.Rows(index)("Orders"), Integer))
Cells(startRow, 3).PutValue(CType(CType(Me.dataTable1.Rows(index)("Sales"), Decimal), Double))
End Sub
Private Sub SetSalesByYearSubreportStyles(ByVal workbook As Workbook)
Dim styleIndex As Integer = workbook.Styles.Add()
Dim style As style = workbook.Styles(styleIndex)
style.Font.IsBold = True
style.Name = "Bold"
End Sub
Private Sub CreateSalesByYearSubreportHeader(ByVal workbook As Workbook, ByVal Cells As Cells, ByVal startRow As Integer, ByVal year As String)
Dim style As style = workbook.Styles("Bold")
Cells(startRow, 0).PutValue(year + " Summary")
Cells(startRow + 1, 1).PutValue("Quarter:")
Cells(startRow + 1, 2).PutValue("Orders Shipped:")
Cells(startRow + 1, 3).PutValue("Sales:")
Cells(startRow, 0).Style = style
Cells(startRow + 1, 1).Style = style
Cells(startRow + 1, 2).Style = style
Cells(startRow + 1, 3).Style = style
End Sub
End Class
|