plan-process-track-excel.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. // 生产成本表
  9. type PlanProcessTrackExcel struct {
  10. Row int
  11. Title string
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content []*PlanStatusInfo
  16. RowMap map[string]int
  17. RowWidthArray []float64
  18. RowsHeightArray []map[int]float64
  19. }
  20. // 批量设置行高
  21. func (b *PlanProcessTrackExcel) setRowsHeight() {
  22. for _, rowHeight := range b.RowsHeightArray {
  23. for row, height := range rowHeight {
  24. b.Excel.SetRowHeight(b.SheetName, row, height)
  25. }
  26. }
  27. }
  28. // 获取范围内单元格的宽度 A:F
  29. func (b *PlanProcessTrackExcel) getRangeWidth(r string) float64 {
  30. rg := strings.Split(r, ":")
  31. if len(rg) == 1 {
  32. start := b.RowMap[rg[0]]
  33. return b.RowWidthArray[start]
  34. } else if len(rg) == 2 {
  35. start := b.RowMap[rg[0]]
  36. end := b.RowMap[rg[1]]
  37. rowr := b.RowWidthArray[start : end+1]
  38. width := 0.0
  39. for _, v := range rowr {
  40. width += v
  41. }
  42. return width
  43. }
  44. return 0.0
  45. }
  46. func (b *PlanProcessTrackExcel) drawTitle() error {
  47. b.Row++
  48. startCell := fmt.Sprintf("A%d", b.Row)
  49. // A1:S2
  50. b.Row++
  51. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("S%d", b.Row))
  52. if err != nil {
  53. return err
  54. }
  55. style, err := b.Excel.NewStyle(&excelize.Style{
  56. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  57. Font: &excelize.Font{Bold: true, Size: 18}})
  58. if err != nil {
  59. return err
  60. }
  61. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  62. if err != nil {
  63. return err
  64. }
  65. b.Excel.SetCellValue(b.SheetName, startCell, "2024月饼礼盒加工追踪表")
  66. return nil
  67. }
  68. func (b *PlanProcessTrackExcel) drawTableTitle() error {
  69. b.Row++
  70. var drawCol = func(prefix string, value string) error {
  71. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  72. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  73. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  74. if err != nil {
  75. return err
  76. }
  77. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  78. if err != nil {
  79. return err
  80. }
  81. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  82. }
  83. var drawMergeCol = func(prefix1 string, prefix2 string, value string) error {
  84. startCell := fmt.Sprintf("%s%d", prefix1, b.Row)
  85. endCell := fmt.Sprintf("%s%d", prefix2, b.Row)
  86. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  87. if err != nil {
  88. return err
  89. }
  90. err = b.Excel.SetCellStyle(b.SheetName, startCell, endCell, b.AlignCenterStyle)
  91. if err != nil {
  92. return err
  93. }
  94. b.Excel.SetCellValue(b.SheetName, startCell, value)
  95. return nil
  96. }
  97. var drawCell = func(prefix string, value string) error {
  98. cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  99. err := b.Excel.SetCellStyle(b.SheetName, cell, cell, b.AlignCenterStyle)
  100. if err != nil {
  101. return err
  102. }
  103. b.Excel.SetCellValue(b.SheetName, cell, value)
  104. return nil
  105. }
  106. drawCol("A", "序号")
  107. drawCol("B", "品名")
  108. drawCol("C", "箱规")
  109. drawCol("D", "数量合计(盒)")
  110. drawCell("E", "部件")
  111. drawCell("F", "下单")
  112. drawCell("G", "纸张")
  113. drawCell("H", "印刷")
  114. drawCell("I", "覆膜")
  115. drawCell("J", "烫金")
  116. drawCell("K", "丝印")
  117. drawCell("L", "对裱")
  118. drawCell("M", "压纹")
  119. drawCell("N", "裱瓦")
  120. drawCell("O", "模切")
  121. drawCell("P", "粘盒")
  122. drawCell("Q", "组装")
  123. drawMergeCol("E", "Q", "包装追踪")
  124. drawCol("R", "交货")
  125. drawCol("S", "备注")
  126. b.Row++
  127. return nil
  128. }
  129. func (b *PlanProcessTrackExcel) drawAllContent() error {
  130. b.Row++
  131. style, _ := b.Excel.NewStyle(&excelize.Style{
  132. Border: []excelize.Border{
  133. {
  134. Type: "left",
  135. Color: "FF000000",
  136. Style: 1,
  137. },
  138. {
  139. Type: "right",
  140. Color: "FF000000",
  141. Style: 1,
  142. },
  143. {
  144. Type: "top",
  145. Color: "FF000000",
  146. Style: 1,
  147. },
  148. {
  149. Type: "bottom",
  150. Color: "FF000000",
  151. Style: 1,
  152. },
  153. },
  154. Fill: excelize.Fill{
  155. Type: "pattern",
  156. Pattern: 1, // 1 表示实心填充
  157. Color: []string{CELL_BACKGROUND},
  158. },
  159. })
  160. var drawRow = func(rowIndex int, values ...string) float64 {
  161. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"}
  162. // 获取改行最大行高
  163. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  164. for i, c := range charas {
  165. v := ""
  166. if i < len(values) {
  167. v = values[i]
  168. }
  169. // 设置背景色
  170. if v == CELL_BACKGROUND {
  171. b.Excel.SetCellStyle(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), fmt.Sprintf("%s%d", c, rowIndex), style)
  172. } else {
  173. // 填充cell
  174. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  175. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  176. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  177. }
  178. if getRowHeight(v, b.getRangeWidth(c)) > max {
  179. max = getRowHeight(v, b.getRangeWidth(c))
  180. }
  181. }
  182. return max
  183. }
  184. for _, planStatusInfo := range b.Content {
  185. // 对应产品下部件's的状态
  186. for _, compStatus := range planStatusInfo.PlanCompStatus {
  187. _row := compStatus["行数"]
  188. row, _ := strconv.Atoi(_row)
  189. rowMaxHeight := drawRow(row, "", "", "", "", compStatus["部件"], compStatus["下单"], compStatus["纸张"], compStatus["印刷"], compStatus["覆膜"],
  190. compStatus["烫金"], compStatus["丝印"], compStatus["对裱"], compStatus["压纹"], compStatus["裱瓦"], compStatus["模切"], compStatus["粘盒"],
  191. compStatus["组装"], compStatus["交货"])
  192. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{row: rowMaxHeight})
  193. }
  194. }
  195. for index, planStatusInfo := range b.Content {
  196. fmt.Println(planStatusInfo.PlanRowStart)
  197. fmt.Println(planStatusInfo.PlanRowEnd)
  198. fmt.Println(planStatusInfo.PlanName)
  199. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("%s%d", "A", planStatusInfo.PlanRowStart), fmt.Sprintf("%s%d", "A", planStatusInfo.PlanRowEnd))
  200. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", "A", planStatusInfo.PlanRowStart), index+1)
  201. // 品名
  202. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("%s%d", "B", planStatusInfo.PlanRowStart), fmt.Sprintf("%s%d", "B", planStatusInfo.PlanRowEnd))
  203. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", "B", planStatusInfo.PlanRowStart), planStatusInfo.PlanName)
  204. // 箱规
  205. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("%s%d", "C", planStatusInfo.PlanRowStart), fmt.Sprintf("%s%d", "C", planStatusInfo.PlanRowEnd))
  206. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", "C", planStatusInfo.PlanRowStart), planStatusInfo.PlanUnit)
  207. // 数量合计
  208. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("%s%d", "D", planStatusInfo.PlanRowStart), fmt.Sprintf("%s%d", "D", planStatusInfo.PlanRowEnd))
  209. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", "D", planStatusInfo.PlanRowStart), planStatusInfo.PlanCount)
  210. }
  211. return nil
  212. }
  213. func (b *PlanProcessTrackExcel) Draws() {
  214. b.drawTitle()
  215. b.drawTableTitle()
  216. b.drawAllContent()
  217. // 根据类容设置最大行高
  218. b.setRowsHeight()
  219. }
  220. func NewPlanProcessTrackExcel(f *excelize.File) *PlanProcessTrackExcel {
  221. border := []excelize.Border{
  222. {Type: "top", Style: 1, Color: "000000"},
  223. {Type: "left", Style: 1, Color: "000000"},
  224. {Type: "right", Style: 1, Color: "000000"},
  225. {Type: "bottom", Style: 1, Color: "000000"},
  226. }
  227. styleLeft, _ := f.NewStyle(&excelize.Style{
  228. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  229. Border: border,
  230. Font: &excelize.Font{Size: 10},
  231. })
  232. b := &PlanProcessTrackExcel{
  233. Title: "2024月饼礼盒加工追踪表",
  234. Row: 0,
  235. SheetName: "Sheet1",
  236. Excel: f,
  237. AlignCenterStyle: styleLeft,
  238. Content: make([]*PlanStatusInfo, 0),
  239. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11, "M": 12, "N": 13, "O": 14, "P": 15, "Q": 16, "R": 17, "S": 18},
  240. RowWidthArray: []float64{6, 12, 12, 12, 16, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 12, 12},
  241. RowsHeightArray: make([]map[int]float64, 0),
  242. }
  243. // 初始化批量设置列宽
  244. for index, cw := range b.RowWidthArray {
  245. for k, v := range b.RowMap {
  246. if v == index {
  247. fmt.Println(index)
  248. fmt.Println(k, cw)
  249. b.Excel.SetColWidth(b.SheetName, k, k, cw)
  250. }
  251. }
  252. }
  253. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  254. return b
  255. }