Skip to content

Commit 82cfd24

Browse files
committed
Add an example code for generating a report.
1 parent f8e2e8a commit 82cfd24

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

samples/report-generator.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
. "github.com/aspose-cells/aspose-cells-go-cpp/v25"
8+
)
9+
10+
type ReportGenerator struct {
11+
workbook *Workbook
12+
worksheet *Worksheet
13+
}
14+
15+
type SaleRecord struct {
16+
Date time.Time
17+
Product string
18+
Quantity int32
19+
Price float64
20+
}
21+
22+
func (reportGenerator *ReportGenerator) initialization() {
23+
reportGenerator.workbook, _ = NewWorkbook()
24+
worksheets, _ := reportGenerator.workbook.GetWorksheets()
25+
reportGenerator.worksheet, _ = worksheets.Get_Int(0)
26+
}
27+
func NewReportGenerator() *ReportGenerator {
28+
reportGenerator := &ReportGenerator{}
29+
reportGenerator.initialization()
30+
return reportGenerator
31+
}
32+
33+
func (reportGenerator *ReportGenerator) GenerateSaleReport(records []SaleRecord) {
34+
reportGenerator.setupReportHeader()
35+
reportGenerator.fillSalesData(records)
36+
reportGenerator.addSummaryStatistics(records)
37+
reportGenerator.createCharts(records)
38+
reportGenerator.formatReport()
39+
}
40+
func (reportGenerator *ReportGenerator) createHeaderStyle() *Style {
41+
style, _ := reportGenerator.workbook.CreateStyle()
42+
font, _ := style.GetFont()
43+
font.SetIsBold(true)
44+
font.SetSize(16)
45+
style.SetHorizontalAlignment(TextAlignmentType_Center)
46+
return style
47+
}
48+
func (reportGenerator *ReportGenerator) setupReportHeader() {
49+
cells, _ := reportGenerator.worksheet.GetCells()
50+
cell, _ := cells.Get_String("A1")
51+
cell.PutValue_String("Sales Report")
52+
cell.SetStyle_Style(reportGenerator.createHeaderStyle())
53+
cells.Merge_Int_Int_Int_Int(0, 0, 1, 5)
54+
}
55+
56+
func (reportGenerator *ReportGenerator) fillSalesData(saleRecords []SaleRecord) {
57+
headers := []string{"date", "product", "Quantity", "Unit Price", "Total Amount"}
58+
cells, _ := reportGenerator.worksheet.GetCells()
59+
rows, _ := cells.GetRows()
60+
row, _ := rows.Get(2)
61+
for i := 0; i < len(headers); i++ {
62+
cell, _ := row.Get(int32(i))
63+
cell.PutValue_String(headers[i])
64+
}
65+
66+
var rowIndex int32 = 3
67+
for i, record := range saleRecords {
68+
row, _ := rows.Get(rowIndex + int32(i))
69+
cell, _ := row.Get(0)
70+
cell.PutValue_Date(record.Date)
71+
cell, _ = row.Get(1)
72+
cell.PutValue_String(record.Product)
73+
cell, _ = row.Get(2)
74+
cell.PutValue_Int(record.Quantity)
75+
cell, _ = row.Get(3)
76+
cell.PutValue_Double(record.Price)
77+
cell, _ = row.Get(4)
78+
print(rowIndex + int32(i))
79+
cell.SetFormula_String(fmt.Sprintf("=C%d*D%d", rowIndex+int32(i)+1, rowIndex+int32(i)+1))
80+
}
81+
82+
}
83+
func (reportGenerator *ReportGenerator) addSummaryStatistics(records []SaleRecord) {
84+
dataRowCount := len(records)
85+
summaryRow := dataRowCount + 4
86+
87+
cells, _ := reportGenerator.worksheet.GetCells()
88+
cell, _ := cells.Get_Int_Int(int32(summaryRow), 0)
89+
cell.PutValue_String("Summary Statistics:")
90+
cell, _ = cells.Get_Int_Int(int32(summaryRow)+1, 0)
91+
cell.PutValue_String("Total Sales:")
92+
cell, _ = cells.Get_Int_Int(int32(summaryRow)+1, 1)
93+
cell.SetFormula_String(fmt.Sprintf("=SUM(E4:E%d)", dataRowCount+3))
94+
cell, _ = cells.Get_Int_Int(int32(summaryRow)+2, 0)
95+
cell.PutValue_String("Average Sales:")
96+
cell, _ = cells.Get_Int_Int(int32(summaryRow)+2, 1)
97+
cell.SetFormula_String(fmt.Sprintf("=AVERAGE(E4:E%d)", dataRowCount+3))
98+
}
99+
100+
func (reportGenerator *ReportGenerator) createCharts(records []SaleRecord) {
101+
chartRow := int32(len(records) + 8)
102+
charts, _ := reportGenerator.worksheet.GetCharts()
103+
chartIndex, _ := charts.Add_ChartType_Int_Int_Int_Int(ChartType_Column, chartRow, 0, chartRow+10, 5)
104+
chart, _ := charts.Get_Int(chartIndex)
105+
nseries, _ := chart.GetNSeries()
106+
nseries.Add_String_Bool(fmt.Sprintf("=Sheet1!$E$4:$E$%d", len(records)+3), true)
107+
nseries.SetCategoryData(fmt.Sprintf("=Sheet1!$B$4:$B$%d", len(records)+3))
108+
chart_title, _ := chart.GetTitle()
109+
chart_title.SetText("Product sales comparison")
110+
}
111+
112+
func (reportGenerator *ReportGenerator) formatReport() {
113+
// Set column width
114+
cells, _ := reportGenerator.worksheet.GetCells()
115+
cells.SetColumnWidth(0, 15)
116+
cells.SetColumnWidth(1, 20)
117+
cells.SetColumnWidth(2, 10)
118+
cells.SetColumnWidth(3, 10)
119+
cells.SetColumnWidth(4, 12)
120+
121+
// Set number Format
122+
numberStyle, _ := reportGenerator.workbook.CreateStyle()
123+
numberStyle.SetCustom_String("#,##0.00")
124+
columns, _ := cells.GetColumns()
125+
column, _ := columns.Get(3)
126+
column.SetStyle(numberStyle)
127+
column, _ = columns.Get(4)
128+
column.SetStyle(numberStyle)
129+
}
130+
func (reportGenerator *ReportGenerator) SaveReport(filePath string) {
131+
reportGenerator.workbook.Save_String(filePath)
132+
}
133+
134+
func GenerateReport() {
135+
t1, _ := time.ParseInLocation("2006-01-02", "2024-01-01", time.Local)
136+
t2, _ := time.ParseInLocation("2006-01-02", "2024-01-02", time.Local)
137+
t3, _ := time.ParseInLocation("2006-01-02", "2024-01-03", time.Local)
138+
records := []SaleRecord{
139+
{t1, "Product A", 100, 25.5},
140+
{t2, "Product B", 150, 30.0},
141+
{t3, "Product A", 80, 25.5},
142+
}
143+
generator := NewReportGenerator()
144+
generator.GenerateSaleReport(records)
145+
generator.SaveReport("Data/Output/SalesAnalysisReport.xlsx")
146+
}

0 commit comments

Comments
 (0)