Skip to content

Commit 01f43b5

Browse files
committed
export to excel: use StreamWriter
1 parent a631bc7 commit 01f43b5

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

services/export/excel.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package export
55

66
import (
77
"fmt"
8+
89
"github.com/xuri/excelize/v2"
910

1011
issues_model "code.gitea.io/gitea/models/issues"
@@ -13,12 +14,31 @@ import (
1314

1415
func IssuesToExcel(ctx *context.Context, issues issues_model.IssueList) *excelize.File {
1516
f := excelize.NewFile()
16-
sheet := f.GetSheetName(f.GetActiveSheetIndex())
17+
sw, err := f.NewStreamWriter("Sheet1")
18+
if err != nil {
19+
fmt.Println(err)
20+
return f
21+
}
22+
// print headers
23+
cell, err := excelize.CoordinatesToCellName(1, 1)
24+
if err != nil {
25+
fmt.Println(err)
26+
return f
27+
}
28+
sw.SetRow(cell, []interface{}{
29+
excelize.Cell{Value: "ID"},
30+
excelize.Cell{Value: "Title"},
31+
excelize.Cell{Value: "Status"},
32+
excelize.Cell{Value: "Assignee(s)"},
33+
excelize.Cell{Value: "Label(s)"},
34+
excelize.Cell{Value: "Created At"},
35+
})
1736

18-
headers := []string{"ID", "Title", "Status", "Assignee(s)", "Label(s)", "Created At"}
19-
for col, h := range headers {
20-
cell, _ := excelize.CoordinatesToCellName(col+1, 1)
21-
f.SetCellValue(sheet, cell, h)
37+
// built-in format ID 22 ("m/d/yy h:mm")
38+
datetimeStyleID, err := f.NewStyle(&excelize.Style{NumFmt: 22})
39+
if err != nil {
40+
fmt.Println(err)
41+
return f
2242
}
2343

2444
for i, issue := range issues {
@@ -51,12 +71,19 @@ func IssuesToExcel(ctx *context.Context, issues issues_model.IssueList) *exceliz
5171
}
5272
}
5373

54-
f.SetCellValue(sheet, fmt.Sprintf("A%d", i+2), issue.Index)
55-
f.SetCellValue(sheet, fmt.Sprintf("B%d", i+2), issue.Title)
56-
f.SetCellValue(sheet, fmt.Sprintf("C%d", i+2), issue.State())
57-
f.SetCellValue(sheet, fmt.Sprintf("D%d", i+2), assignees)
58-
f.SetCellValue(sheet, fmt.Sprintf("E%d", i+2), labels)
59-
f.SetCellValue(sheet, fmt.Sprintf("F%d", i+2), issue.CreatedUnix.AsTime()) // .Format("2006-01-02"))
74+
cell, _ := excelize.CoordinatesToCellName(1, i+1)
75+
sw.SetRow(cell, []interface{}{
76+
excelize.Cell{Value: issue.Index},
77+
excelize.Cell{Value: issue.Title},
78+
excelize.Cell{Value: issue.State()},
79+
excelize.Cell{Value: assignees},
80+
excelize.Cell{Value: labels},
81+
excelize.Cell{StyleID: datetimeStyleID, Value: issue.CreatedUnix.AsTime()},
82+
})
83+
6084
}
85+
86+
sw.Flush()
87+
6188
return f
62-
}
89+
}

0 commit comments

Comments
 (0)