You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB
Go

package common
import (
"cu-helper/high-opt-loss/service"
"fmt"
"github.com/mizuki1412/go-core-kit/service/logkit"
"github.com/spf13/cast"
"github.com/xuri/excelize/v2"
"strconv"
)
func GenExcelFile(m map[string]*service.HighOptLoss) {
// Open the Excel file
f, err := excelize.OpenFile("/Users/leo/Documents/大光衰通报模板.xlsx")
if err != nil {
fmt.Println(err)
return
}
// Cut and paste data from E4:E22 to C4:C22
for i := 4; i <= 22; i++ {
eCell := "E" + strconv.Itoa(i)
cCell := "C" + strconv.Itoa(i)
value, _ := f.GetCellValue("汇总", eCell)
f.SetCellValue("汇总", cCell, cast.ToInt(value))
f.SetCellValue("汇总", eCell, "")
}
// Populate E4:H22 with data from the map
for i := 4; i <= 22; i++ {
bCell := "B" + strconv.Itoa(i)
key, _ := f.GetCellValue("汇总", bCell)
if data, ok := m[key]; ok {
f.SetCellValue("汇总", "E"+strconv.Itoa(i), data.Count)
f.SetCellValue("汇总", "F"+strconv.Itoa(i), data.DurationOverSeven)
f.SetCellValue("汇总", "G"+strconv.Itoa(i), data.DurationOverFifteen)
f.SetCellValue("汇总", "H"+strconv.Itoa(i), data.Book)
} else {
f.SetCellValue("汇总", "E"+strconv.Itoa(i), 0)
f.SetCellValue("汇总", "F"+strconv.Itoa(i), 0)
f.SetCellValue("汇总", "G"+strconv.Itoa(i), 0)
f.SetCellValue("汇总", "H"+strconv.Itoa(i), 0)
}
}
// Calculate D4:D22 as C-E
for i := 4; i <= 22; i++ {
cCell := "C" + strconv.Itoa(i)
eCell := "E" + strconv.Itoa(i)
dCell := "D" + strconv.Itoa(i)
cValue, _ := f.GetCellValue("汇总", cCell)
eValue, _ := f.GetCellValue("汇总", eCell)
cInt, _ := strconv.Atoi(cValue)
eInt, _ := strconv.Atoi(eValue)
f.SetCellValue("汇总", dCell, cInt-eInt)
}
// Sum columns C:H and place the results in row 23
for col := 'C'; col <= 'H'; col++ {
sumCell := string(col) + "23"
sumFormula := fmt.Sprintf("SUM(%s4:%s22)", string(col), string(col))
f.SetCellFormula("汇总", sumCell, sumFormula)
}
// Recalculate the sum for each cell to ensure it appears without manual intervention
for col := 'C'; col <= 'H'; col++ {
sumCell := string(col) + "23"
value, err := f.CalcCellValue("汇总", sumCell)
if err != nil {
fmt.Println(err)
} else {
f.SetCellValue("汇总", sumCell, cast.ToInt(value))
}
}
// Save the file
if err := f.SaveAs("/Users/leo/Documents/大光衰通报模板.xlsx"); err != nil {
fmt.Println(err)
}
logkit.Info("生成结束...")
}