1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| type GetExcelData struct { Campus uint8 `form:"campus" binding:"required"` }
func ExportSuppliesRecord(c *gin.Context) { var data GetExcelData err := c.ShouldBindQuery(&data) if err != nil { fmt.Println(err) return }
ExcelData, err := suppliesServices.GetExcelData(*publisher, data.Campus) if err != nil { fmt.Println(err) return }
campusMap := map[uint8]string{ } statusMap := map[int]string{ }
f := excelize.NewFile() streamWriter, err := f.NewStreamWriter("Sheet1") if err != nil { fmt.Println(err) return } if err := streamWriter.SetColWidth(1, 20, 15); err != nil { fmt.Println(err) return } if err := streamWriter.SetColWidth(5, 5, 25); err != nil { fmt.Println(err) return } styleID, err := f.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, Fill: excelize.Fill{Type: "pattern", Color: []string{"#DFEBF6"}, Pattern: 1}, }) if err != nil { fmt.Println(err) return } if err := streamWriter.SetRow("A1", []interface{}{ excelize.Cell{Value: "表名", StyleID: styleID}, }, excelize.RowOpts{Height: 30, Hidden: false}); err != nil { fmt.Println(err) return } if err := streamWriter.MergeCell("A1", "P1"); err != nil { fmt.Println(err) return } header := []interface{}{} for _, cell := range []string{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", } { header = append(header, cell) } if err := streamWriter.SetRow("A2", header); err != nil { fmt.Println(err) return } for rowID, record := range ExcelData { campus, _ := campusMap[record.Campus] status, _ := statusMap[record.Status] result, err := suppliesServices.GetSuppliesById(record.SuppliesID) if err != nil { fmt.Println(err) return } row := []interface{}{ record.ID, record.Name, record.Gender, record.StudentID, record.College, record.Dormitory, record.Contact, campus, result.Name, result.Kind, result.Spec, record.Count, status, record.ApplyTime, record.BorrowTime, record.ReturnTime, } cell, _ := excelize.CoordinatesToCellName(1, rowID+3) if err := streamWriter.SetRow(cell, row); err != nil { fmt.Println(err) return } } if err := streamWriter.Flush(); err != nil { fmt.Println(err) return } fileName := uuid.NewString() + ".xlsx" filePath := "./files/" + fileName if err := f.SaveAs(filePath); err != nil { fmt.Println(err) return } }
|