bill-purchase-excel.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. type PurchaseBillExcel struct {
  8. Offset int
  9. Title string //标题
  10. Excel *excelize.File
  11. SheetName string
  12. AlignCenterStyle int
  13. Content *model.PurchaseBill
  14. }
  15. func (b *PurchaseBillExcel) drawTitle() error {
  16. tileIndex := b.Offset + 1
  17. startCell := fmt.Sprintf("A%d", tileIndex)
  18. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("I%d", tileIndex))
  19. if err != nil {
  20. return err
  21. }
  22. style, err := b.Excel.NewStyle(&excelize.Style{
  23. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  24. Font: &excelize.Font{Bold: true, Size: 18}})
  25. if err != nil {
  26. return err
  27. }
  28. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  29. if err != nil {
  30. return err
  31. }
  32. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  33. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  34. return nil
  35. }
  36. func (b *PurchaseBillExcel) drawSubTitles() error {
  37. row := b.Offset + 2
  38. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  39. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  40. Font: &excelize.Font{Size: 11}})
  41. if err != nil {
  42. return err
  43. }
  44. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  45. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  46. Font: &excelize.Font{Size: 11}})
  47. if err != nil {
  48. return err
  49. }
  50. var drawLeft = func(rowIndex int, value string) error {
  51. //左边1
  52. left1Cell := fmt.Sprintf("A%d", rowIndex)
  53. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  54. if err != nil {
  55. return err
  56. }
  57. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  58. if err != nil {
  59. return err
  60. }
  61. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  62. return nil
  63. }
  64. var drawRight = func(rowIndex int, value string) error {
  65. right1Cell := fmt.Sprintf("F%d", rowIndex)
  66. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  67. if err != nil {
  68. return err
  69. }
  70. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  71. if err != nil {
  72. return err
  73. }
  74. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  75. return nil
  76. }
  77. //第一行
  78. drawLeft(row, "类别:"+b.Content.Type)
  79. drawRight(row, "单号:"+b.Content.SerialNumber)
  80. b.Excel.SetRowHeight(b.SheetName, row, 21)
  81. //第二行
  82. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  83. timeformat := b.Content.CreateTime.Format("2006年01月02号 15:04:05")
  84. drawRight(row+1, "下单时间:"+timeformat)
  85. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  86. //第三行
  87. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  88. drawRight(row+2, "")
  89. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  90. return nil
  91. }
  92. func (b *PurchaseBillExcel) drawTableTitle() error {
  93. row := b.Offset + 5
  94. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  95. var drawCol = func(prefix string, value string) error {
  96. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  97. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  98. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  99. if err != nil {
  100. return err
  101. }
  102. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  103. if err != nil {
  104. return err
  105. }
  106. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  107. }
  108. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  109. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  110. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  111. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  112. if err != nil {
  113. return err
  114. }
  115. if err != nil {
  116. fmt.Println(err)
  117. return err
  118. }
  119. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  120. if err != nil {
  121. return err
  122. }
  123. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  124. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  125. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  126. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  127. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  128. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  129. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  130. return nil
  131. }
  132. drawCol("A", "采购项目")
  133. drawCol("B", "规格(克)")
  134. drawCol("H", "交货时间")
  135. drawCol("I", " 备注")
  136. drawCol("E", " 数量")
  137. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  138. drawCol2("C", "D", "尺寸(mm)", "长", "宽")
  139. drawCol2("F", "G", "单价", "元/张", "元/吨")
  140. return nil
  141. }
  142. func (b *PurchaseBillExcel) drawTableContent() error {
  143. row := b.Offset + 7
  144. var DrawRow = func(rowIndex int, values ...string) {
  145. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  146. for i, c := range charas {
  147. v := ""
  148. if i < len(values) {
  149. v = values[i]
  150. }
  151. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  152. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  153. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  154. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  155. }
  156. }
  157. papers := b.Content.Paper
  158. if len(papers) > 0 {
  159. for _, paper := range papers {
  160. deliveryTime := paper.DeliveryTime.Format("2006-01-02")
  161. DrawRow(row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.Count), paper.Price, paper.Price2, deliveryTime, "")
  162. row++
  163. }
  164. }
  165. return nil
  166. }
  167. func (b *PurchaseBillExcel) drawTableFooter() error {
  168. row := b.Offset + 10
  169. left1Cell := fmt.Sprintf("A%d", row)
  170. border := []excelize.Border{
  171. {Type: "top", Style: 1, Color: "000000"},
  172. {Type: "left", Style: 1, Color: "000000"},
  173. {Type: "right", Style: 1, Color: "000000"},
  174. {Type: "bottom", Style: 1, Color: "000000"},
  175. }
  176. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  177. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  178. Border: border,
  179. })
  180. b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  181. b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址")
  182. addCel := fmt.Sprintf("B%d", row)
  183. b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", row))
  184. b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", row), styleLeft)
  185. b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
  186. sureCel := fmt.Sprintf("F%d", row)
  187. b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("I%d", row))
  188. b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("I%d", row), styleLeft)
  189. b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
  190. b.Excel.SetRowHeight(b.SheetName, row, 21)
  191. return nil
  192. }
  193. func (b *PurchaseBillExcel) Draws() {
  194. b.drawTitle()
  195. b.drawSubTitles()
  196. b.drawTableTitle()
  197. b.drawTableContent()
  198. b.drawTableFooter()
  199. }
  200. func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel {
  201. border := []excelize.Border{
  202. {Type: "top", Style: 1, Color: "000000"},
  203. {Type: "left", Style: 1, Color: "000000"},
  204. {Type: "right", Style: 1, Color: "000000"},
  205. {Type: "bottom", Style: 1, Color: "000000"},
  206. }
  207. styleLeft, _ := f.NewStyle(&excelize.Style{
  208. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  209. Border: border,
  210. Font: &excelize.Font{Size: 10},
  211. })
  212. b := &PurchaseBillExcel{
  213. Title: "原材料采购单",
  214. SheetName: "Sheet1",
  215. Excel: f,
  216. Offset: 0,
  217. AlignCenterStyle: styleLeft,
  218. }
  219. f.SetColWidth(b.SheetName, "A", "A", 17)
  220. f.SetColWidth(b.SheetName, "B", "B", 13)
  221. f.SetColWidth(b.SheetName, "C", "G", 9.5)
  222. f.SetColWidth(b.SheetName, "H", "I", 12)
  223. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  224. return b
  225. }