1
0
Fork 0
forked from goffee/cup

add sample xlsx

This commit is contained in:
jacs 2024-12-22 10:37:58 -05:00
parent 83f0db05da
commit d840d3eba4

View file

@ -11,8 +11,12 @@ import (
"time"
"git.smarteching.com/goffee/core"
// for graphs
"git.smarteching.com/zeni/go-chart/v2"
// for PDF
"github.com/jung-kurt/gofpdf"
// for xslx
"github.com/xuri/excelize/v2"
)
// sample buffer back, pdf
@ -103,10 +107,69 @@ func Themepdf(c *core.Context) *core.Response {
}
return c.Response.BufferPDF("elreport.pdf", b)
return c.Response.BufferFile("elreport.pdf", "application/pdf", b)
}
func Themexslx(c *core.Context) *core.Response {
var b bytes.Buffer
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for idx, row := range [][]interface{}{
{nil, "Cerveza", "Vodka", "Ron"}, {"Small", 2, 3, 3},
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
} {
cell, err := excelize.CoordinatesToCellName(1, idx+1)
if err != nil {
fmt.Println(err)
}
f.SetSheetRow("Sheet1", cell, &row)
}
if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
Type: excelize.Pie,
Series: []excelize.ChartSeries{
{
Name: "Totales",
Categories: "Sheet1!$B$1:$D$1",
Values: "Sheet1!$B$2:$D$2",
},
},
Format: excelize.GraphicOptions{
OffsetX: 15,
OffsetY: 10,
},
Title: []excelize.RichTextRun{
{
Text: "Fruit Pie Chart",
},
},
PlotArea: excelize.ChartPlotArea{
ShowPercent: true,
},
}); err != nil {
fmt.Println(err)
}
buf, err := f.WriteToBuffer()
if err != nil {
fmt.Fprint(buf, err.Error())
}
// Get byte array
b.ReadFrom(buf)
return c.Response.BufferFile("elreport.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", b)
}
func getContents() [][]string {
return [][]string{
{"node1", "Swamp"},