From 01f43b53681645ec57fa292f74785221117c4197 Mon Sep 17 00:00:00 2001 From: Timotheus Pokorra Date: Tue, 7 Oct 2025 22:00:37 +0200 Subject: [PATCH] export to excel: use StreamWriter --- services/export/excel.go | 51 ++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/services/export/excel.go b/services/export/excel.go index 4e4f1ab5b9..b3c219fdd8 100644 --- a/services/export/excel.go +++ b/services/export/excel.go @@ -5,6 +5,7 @@ package export import ( "fmt" + "github.com/xuri/excelize/v2" issues_model "code.gitea.io/gitea/models/issues" @@ -13,12 +14,31 @@ import ( func IssuesToExcel(ctx *context.Context, issues issues_model.IssueList) *excelize.File { f := excelize.NewFile() - sheet := f.GetSheetName(f.GetActiveSheetIndex()) + sw, err := f.NewStreamWriter("Sheet1") + if err != nil { + fmt.Println(err) + return f + } + // print headers + cell, err := excelize.CoordinatesToCellName(1, 1) + if err != nil { + fmt.Println(err) + return f + } + sw.SetRow(cell, []interface{}{ + excelize.Cell{Value: "ID"}, + excelize.Cell{Value: "Title"}, + excelize.Cell{Value: "Status"}, + excelize.Cell{Value: "Assignee(s)"}, + excelize.Cell{Value: "Label(s)"}, + excelize.Cell{Value: "Created At"}, + }) - headers := []string{"ID", "Title", "Status", "Assignee(s)", "Label(s)", "Created At"} - for col, h := range headers { - cell, _ := excelize.CoordinatesToCellName(col+1, 1) - f.SetCellValue(sheet, cell, h) + // built-in format ID 22 ("m/d/yy h:mm") + datetimeStyleID, err := f.NewStyle(&excelize.Style{NumFmt: 22}) + if err != nil { + fmt.Println(err) + return f } for i, issue := range issues { @@ -51,12 +71,19 @@ func IssuesToExcel(ctx *context.Context, issues issues_model.IssueList) *exceliz } } - f.SetCellValue(sheet, fmt.Sprintf("A%d", i+2), issue.Index) - f.SetCellValue(sheet, fmt.Sprintf("B%d", i+2), issue.Title) - f.SetCellValue(sheet, fmt.Sprintf("C%d", i+2), issue.State()) - f.SetCellValue(sheet, fmt.Sprintf("D%d", i+2), assignees) - f.SetCellValue(sheet, fmt.Sprintf("E%d", i+2), labels) - f.SetCellValue(sheet, fmt.Sprintf("F%d", i+2), issue.CreatedUnix.AsTime()) // .Format("2006-01-02")) + cell, _ := excelize.CoordinatesToCellName(1, i+1) + sw.SetRow(cell, []interface{}{ + excelize.Cell{Value: issue.Index}, + excelize.Cell{Value: issue.Title}, + excelize.Cell{Value: issue.State()}, + excelize.Cell{Value: assignees}, + excelize.Cell{Value: labels}, + excelize.Cell{StyleID: datetimeStyleID, Value: issue.CreatedUnix.AsTime()}, + }) + } + + sw.Flush() + return f -} \ No newline at end of file +}