数据透视表

    1. package main
    2. import (
    3. "fmt"
    4. "math/rand"
    5. "github.com/360EntSecGroup-Skylar/excelize"
    6. )
    7. func main() {
    8. f := excelize.NewFile()
    9. month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    10. year := []int{2017, 2018, 2019}
    11. types := []string{"Meat", "Dairy", "Beverages", "Produce"}
    12. region := []string{"East", "West", "North", "South"}
    13. f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})
    14. for i := 0; i < 30; i++ {
    15. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", i+2), month[rand.Intn(12)])
    16. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", i+2), year[rand.Intn(3)])
    17. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", i+2), types[rand.Intn(4)])
    18. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", i+2), rand.Intn(5000))
    19. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", i+2), region[rand.Intn(4)])
    20. }
    21. DataRange: "Sheet1!$A$1:$E$31",
    22. PivotTableRange: "Sheet1!$G$2:$M$34",
    23. Rows: []string{"Month", "Year"},
    24. Columns: []string{"Type"},
    25. Data: []string{"Sales"},
    26. })
    27. if err != nil {
    28. fmt.Println(err)
    29. }
    30. err = f.SaveAs("Book1.xlsx")
    31. if err != nil {
    32. fmt.Println(err)
    33. }