fix:1.仅分销商导出也使用模板 2.修改分销商、分销商详情模板 3.增加日志

This commit is contained in:
fuzhongyun 2025-12-30 09:36:37 +08:00
parent 8deb91dea9
commit 37a8318814
3 changed files with 27 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import (
"sort"
"time"
"github.com/gofiber/fiber/v2/log"
"github.com/xuri/excelize/v2"
)
@ -104,12 +105,11 @@ func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) {
//总量生成excel
if len(total) > 0 {
filePath := b.cacheDir + "/kshj_total" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx"
err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_total_v2.xlsx", filePath, total)
err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_total.xlsx", filePath, total)
}
if len(gt) > 0 {
filePath := b.cacheDir + "/kshj_gt" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx"
// err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, total)
err = b.resellerDetailFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, gt)
}
return err
@ -126,6 +126,20 @@ func (b *BbxtTools) SimpleFillExcel(templatePath, outputPath string, dataSlice i
sheet := f.GetSheetName(0)
// 1.1 获取第二行模板样式
resellerTplRow := 2
styleIDReseller, err := f.GetCellStyle(sheet, fmt.Sprintf("A%d", resellerTplRow))
if err != nil {
log.Errorf("获取分销商总计样式失败: %v", err)
styleIDReseller = 0
}
// 1.2 获取分销商总计行高
rowHeightReseller, err := f.GetRowHeight(sheet, resellerTplRow)
if err != nil {
log.Errorf("获取分销商总计行高失败: %v", err)
rowHeightReseller = 31 // 默认高度
}
// 2. 反射获取切片数据
v := reflect.ValueOf(dataSlice)
if v.Kind() != reflect.Slice {
@ -157,12 +171,19 @@ func (b *BbxtTools) SimpleFillExcel(templatePath, outputPath string, dataSlice i
} else {
rowData = []interface{}{item}
}
// 4.1 设置行高
f.SetRowHeight(sheet, currentRow, rowHeightReseller)
// 5. 填充到Excel
for col, value := range rowData {
cell := fmt.Sprintf("%c%d", 'A'+col, currentRow)
f.SetCellValue(sheet, cell, value)
}
// 5.1 使用第二行模板样式
if styleIDReseller != 0 {
f.SetCellStyle(sheet, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("B%d", currentRow), styleIDReseller)
}
}
// 6. 保存
@ -188,22 +209,24 @@ func (b *BbxtTools) resellerDetailFillExcel(templatePath, outputPath string, dat
resellerTplRow := 2
styleIDReseller, err := f.GetCellStyle(sheet, fmt.Sprintf("A%d", resellerTplRow))
if err != nil {
// 如果获取失败,就不应用样式,或者记录日志,这里选择忽略错误继续
log.Errorf("获取分销商总计样式失败: %v", err)
styleIDReseller = 0
}
rowHeightReseller, err := f.GetRowHeight(sheet, resellerTplRow)
if err != nil {
log.Errorf("获取分销商总计行高失败: %v", err)
rowHeightReseller = 31 // 默认高度
}
// 获取模板样式2第三行-产品亏损明细
productTplRow := 3
styleIDProduct, err := f.GetCellStyle(sheet, fmt.Sprintf("A%d", productTplRow))
if err != nil {
// 如果获取失败,就不应用样式,或者记录日志,这里选择忽略错误继续
log.Errorf("获取商品详情样式失败: %v", err)
styleIDProduct = 0
}
rowHeightProduct, err := f.GetRowHeight(sheet, productTplRow)
if err != nil {
log.Errorf("获取商品详情行高失败: %v", err)
rowHeightProduct = 25 // 默认高度
}

Binary file not shown.

BIN
tmpl/excel_temp/kshj_total.xlsx Normal file → Executable file

Binary file not shown.