package api import ( "box-cost/db/model" "fmt" "github.com/xuri/excelize/v2" ) type ReportProduceExcel struct { Offset int Row int Title string //标题 Excel *excelize.File SheetName string AlignCenterStyle int Content *model.ProduceBill BudgetAmount float64 RealAmount float64 } func (b *ReportProduceExcel) drawTitle() error { // 设置外边距 marginLeft := excelize.PageMarginLeft(0.15) b.Excel.SetPageMargins(b.SheetName, marginLeft) b.Row++ startCell := fmt.Sprintf("A%d", b.Row) err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", b.Row)) if err != nil { return err } style, err := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, Font: &excelize.Font{Bold: true, Size: 18}}) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style) if err != nil { return err } b.Excel.SetRowHeight(b.SheetName, b.Row, 23) b.Excel.SetCellValue(b.SheetName, startCell, b.Title) return nil } func (b *ReportProduceExcel) drawSubTitles() error { b.Row++ styleLeft, err := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"}, Font: &excelize.Font{Size: 11}}) if err != nil { return err } styleRight, err := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"}, Font: &excelize.Font{Size: 11}}) if err != nil { return err } var drawLeft = func(rowIndex int, value string) error { //左边1 left1Cell := fmt.Sprintf("A%d", rowIndex) err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex)) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, left1Cell, value) return nil } var drawRight = func(rowIndex int, value string) error { right1Cell := fmt.Sprintf("F%d", rowIndex) err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex)) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, right1Cell, value) return nil } //第一行 drawLeft(b.Row, "类别:"+b.Content.Type) drawRight(b.Row, "单号:"+b.Content.SerialNumber) b.Excel.SetRowHeight(b.SheetName, b.Row, 21) //第二行 drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier) timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05") drawRight(b.Row+1, "下单时间:"+timeformat) b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21) //第三行 drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName) status := "" if b.Content.Status == "complete" { status = "已完成" } if b.Content.Status == "created" { status = "进行中" } drawRight(b.Row+2, "状态:"+status) b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21) return nil } func (b *ReportProduceExcel) drawTableTitle() error { b.Row += 3 //A加工项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I var drawCol = func(prefix string, value string) error { left1Cell := fmt.Sprintf("%s%d", prefix, b.Row) left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1) err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle) if err != nil { return err } return b.Excel.SetCellValue(b.SheetName, left1Cell, value) } var drawCol2 = func(prefix string, value1 string, value2 string) error { left1Cell := fmt.Sprintf("%s%d", prefix, b.Row) left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1) err := b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, left1Cell, value1) b.Excel.SetCellValue(b.SheetName, left2Cell, value2) return nil } drawCol("A", "加工项目") drawCol("B", "规格") drawCol("C", "纸张") drawCol("D", "来纸尺寸") drawCol("E", "印刷尺寸") drawCol("F", "下单数量") drawCol("G", "完成数量") drawCol2("H", "单价", "元/张") drawCol("I", "预算金额") drawCol("J", "实际金额") drawCol("K", "交货时间") drawCol("L", "备注") return nil } func (b *ReportProduceExcel) drawTableContent() error { b.Row += 2 var DrawRow = func(rowIndex int, values ...string) { charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"} for i, c := range charas { v := "" if i < len(values) { v = values[i] } b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v) val2Cel := fmt.Sprintf("%s%d", c, rowIndex) b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle) b.Excel.SetRowHeight(b.SheetName, rowIndex, 21) } } produces := b.Content.Produces if len(produces) > 0 { for _, produce := range produces { realCount := "" price := produce.OrderPrice priceStr := fmt.Sprintf("%.3f", price) b.FormatToEmpty(&priceStr) // 预算金额 budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount)) b.FormatToEmpty(&budgetAmount) // 实际金额 realPrice := "" // 实际完成数 realCount = fmt.Sprintf("%d", produce.ConfirmCount) b.FormatToEmpty(&realCount) realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount)) b.FormatToEmpty(&realPrice) deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02") DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark) b.Row++ b.BudgetAmount += produce.OrderPrice * float64(produce.OrderCount) b.RealAmount += produce.OrderPrice * float64(produce.ConfirmCount) } } return nil } func (b *ReportProduceExcel) Draws() { b.drawTitle() b.drawSubTitles() b.drawTableTitle() b.drawTableContent() } func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel { border := []excelize.Border{ {Type: "top", Style: 1, Color: "000000"}, {Type: "left", Style: 1, Color: "000000"}, {Type: "right", Style: 1, Color: "000000"}, {Type: "bottom", Style: 1, Color: "000000"}, } styleLeft, _ := f.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, Border: border, }) b := &ReportProduceExcel{ Title: "中鱼互动加工单", SheetName: "Sheet1", Excel: f, Offset: 0, AlignCenterStyle: styleLeft, } f.SetColWidth(b.SheetName, "A", "A", 12) f.SetColWidth(b.SheetName, "B", "K", 9.5) f.SetColWidth(b.SheetName, "L", "L", 10) f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0)) return b } func (b *ReportProduceExcel) FormatToEmpty(str *string) { if *str == "0" || *str == "0.000" { *str = "" } }