Browse Source
Merge remote-tracking branch 'origin/milestone-20260325-学习笔记' into milestone-20260325-学习笔记
milestone-20260325-学习笔记
Merge remote-tracking branch 'origin/milestone-20260325-学习笔记' into milestone-20260325-学习笔记
milestone-20260325-学习笔记
2 changed files with 356 additions and 0 deletions
@ -0,0 +1,356 @@ |
|||
## Excelize |
|||
|
|||
#### 样式结构体 excelize.Style{} |
|||
|
|||
Border 边框相关配置 |
|||
|
|||
Fill 背景填充 |
|||
|
|||
Font 字体设置 |
|||
|
|||
Alignment 对齐配置 |
|||
|
|||
TextRotation 文字旋转 |
|||
|
|||
NumFmt 预定义数字格式索引 |
|||
|
|||
DecimalPlaces 强制指定小数位数 |
|||
|
|||
CustomNumFmt 自定义格式字符串 |
|||
|
|||
Protection 单元格保护 |
|||
|
|||
NegRed 负数显示红色 |
|||
|
|||
#### 整体显示选项 excelize.ViewOptions{} |
|||
|
|||
ShowRowColHeader(false) 隐藏行列号 |
|||
|
|||
ShowGridLines(false) 关闭网格显示 |
|||
|
|||
ShowZeros(false) 隐藏0值 |
|||
|
|||
ZoomScale(int)设置缩放比例 |
|||
|
|||
DefaultGridColor(16进制字符串)更改网格颜色 |
|||
|
|||
#### Table结构体 excelize.Table{} |
|||
|
|||
Range 指定表格范围 |
|||
|
|||
Name 表格内部唯一key |
|||
|
|||
StyleName 配色方案 |
|||
|
|||
ShowColumnStripes 是否隔列换色 |
|||
|
|||
ShowRowStripes 是否隔行换色 |
|||
|
|||
ShowFirstColumn 是否突出第一列 |
|||
|
|||
ShowLastColumn 是否突出最后一列 |
|||
|
|||
ShowHeaderRow 是否显示表头行 |
|||
|
|||
#### 窗格选项 excelize.Panes{} |
|||
|
|||
Freeze 是否冻结 |
|||
|
|||
Split 是否拆分 |
|||
|
|||
YSplit 从第几行拆分 |
|||
|
|||
XSplit 从第几列拆分 |
|||
|
|||
TopLeftCell 拆分后右下角窗口的起始单元格 |
|||
|
|||
ActivePane 默认激活哪个窗格 |
|||
|
|||
Selection 光标默认停留在哪一格 |
|||
|
|||
#### 图表选项 excliez.Chart |
|||
|
|||
Type 图表类型 Col柱状图 Line折线图 Pie 饼状图 Scatter 散点图 |
|||
|
|||
Series 数据来源 |
|||
|
|||
Format 设置格式 |
|||
|
|||
Dimension 表格大小 |
|||
|
|||
Legend 图例 |
|||
|
|||
Title 标题 |
|||
|
|||
VaryColors 是否给同一系列图形上色区分(饼图必选) |
|||
|
|||
XAxis 设置横坐标轴 |
|||
|
|||
YAxis 设置纵坐标轴 |
|||
|
|||
PlotArea 设置绘图区 |
|||
|
|||
Fill 设置背景 |
|||
|
|||
Border 设置边框 |
|||
|
|||
ShowBlankAs 遇到空格如何处理 |
|||
|
|||
BubbleSize 气泡图中的气泡大小 |
|||
|
|||
HoleSize 甜甜圈图中的空洞大小 |
|||
|
|||
Order 层级,相当于z-index |
|||
|
|||
。。。。。。。。。。。。剩余见图 |
|||
|
|||
部分代码如下: |
|||
|
|||
```go |
|||
func ExcelCalculate() { |
|||
file := excelize.NewFile() |
|||
file.SetSheetName("Sheet1", "成绩单") |
|||
data := [][]interface{}{ |
|||
{"学号", "班级", "数学", "英语", "语文", "总分", "平均分"}, |
|||
{1, "一班", 98, 90, 86}, |
|||
{2, "一班", 88, 94, 85}, |
|||
{3, "一班", 79, 90, 95}, |
|||
{4, "一班", 92, 90, 91}, |
|||
{5, "一班", 95, 77, 93}, |
|||
{6, "二班", 90, 97, 75}, |
|||
{7, "二班", 86, 89, 99}, |
|||
{8, "二班", 97, 90, 78}, |
|||
{9, "二班", 90, 90, 90}, |
|||
{10, "二班", 94, 83, 91}, |
|||
{"总分"}, |
|||
{"平均分"}, |
|||
} |
|||
for i, row := range data { |
|||
startCell, _ := excelize.JoinCellName("B", i+2) |
|||
file.SetSheetRow("成绩单", startCell, &row) |
|||
} |
|||
fType := excelize.STCellFormulaTypeShared |
|||
area := "G3:G12" |
|||
file.SetCellFormula("成绩单", "G3", "=SUM(D3:F3)", excelize.FormulaOpts{ |
|||
Type: &fType, |
|||
Ref: &area, |
|||
}) |
|||
area2 := "D13:G13" |
|||
file.SetCellFormula("成绩单", "D13", "=SUM(D3:D12)", excelize.FormulaOpts{ |
|||
Type: &fType, |
|||
Ref: &area2, |
|||
}) |
|||
area3 := "H3:H12" |
|||
file.SetCellFormula("成绩单", "H3", "=AVERAGE(D3:F3)", excelize.FormulaOpts{ |
|||
Type: &fType, |
|||
Ref: &area3, |
|||
}) |
|||
area4 := "D14:H14" |
|||
file.SetCellFormula("成绩单", "D14", "=AVERAGE(D3:D12)", excelize.FormulaOpts{ |
|||
Type: &fType, |
|||
Ref: &area4, |
|||
}) |
|||
file.MergeCell("成绩单", "B13", "C13") |
|||
file.MergeCell("成绩单", "B14", "C14") |
|||
file.AddTable("成绩单", &excelize.Table{ |
|||
Range: "B2:H14", |
|||
Name: "", |
|||
StyleName: "TableStyleMedium9", |
|||
ShowColumnStripes: true, |
|||
ShowFirstColumn: true, |
|||
ShowLastColumn: true, |
|||
}) |
|||
type Integer *int |
|||
|
|||
newStyle, _ := file.NewStyle(&excelize.Style{ |
|||
NumFmt: 2, |
|||
}) |
|||
file.SetCellStyle("成绩单", "H1", "H14", newStyle) |
|||
file.SetCellStyle("成绩单", "D14", "G14", newStyle) |
|||
file.SetPanes("成绩单", &excelize.Panes{ |
|||
Freeze: false, |
|||
Split: true, |
|||
XSplit: 2000, |
|||
YSplit: 1000, |
|||
TopLeftCell: "A1", |
|||
ActivePane: "bottomRight", |
|||
Selection: nil, |
|||
}) |
|||
chart1 := getChart() |
|||
chart2 := getChart() |
|||
fmt.Printf("%p\n%p", chart1, chart2) |
|||
file.AddChart("成绩单", "J2", chart1) |
|||
err := file.AddChartSheet("统计图", chart2) |
|||
if err != nil { |
|||
fmt.Printf("%+v\n", err) |
|||
} |
|||
file.SetSheetBackground("统计图", "D:\\note\\golang.png") |
|||
file.SetSheetBackground("成绩单", "D:\\note\\golang.png") |
|||
style1, err := file.NewConditionalStyle(&excelize.Style{ |
|||
Border: []excelize.Border{ |
|||
excelize.Border{ |
|||
Type: "top", |
|||
Color: "#ff0303", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "bottom", |
|||
Color: "#ff0303", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "left", |
|||
Color: "#ff0303", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "right", |
|||
Color: "#ff0303", |
|||
Style: 6, |
|||
}, |
|||
}, |
|||
Fill: excelize.Fill{ |
|||
Type: "pattern", |
|||
Color: []string{"ffa3a3"}, |
|||
}, |
|||
Font: &excelize.Font{ |
|||
Color: "#8c2e2e", |
|||
}, |
|||
Alignment: nil, |
|||
Protection: nil, |
|||
NumFmt: 0, |
|||
DecimalPlaces: nil, |
|||
CustomNumFmt: nil, |
|||
NegRed: false, |
|||
}) |
|||
style2, err := file.NewConditionalStyle(&excelize.Style{ |
|||
Border: []excelize.Border{ |
|||
excelize.Border{ |
|||
Type: "top", |
|||
Color: "#1f9900", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "bottom", |
|||
Color: "#1f9900", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "left", |
|||
Color: "#1f9900", |
|||
Style: 6, |
|||
}, |
|||
excelize.Border{ |
|||
Type: "right", |
|||
Color: "#1f9900", |
|||
Style: 6, |
|||
}, |
|||
}, |
|||
Font: &excelize.Font{ |
|||
Color: "#1f9900", |
|||
}, |
|||
Alignment: nil, |
|||
Protection: nil, |
|||
NumFmt: 0, |
|||
DecimalPlaces: nil, |
|||
CustomNumFmt: nil, |
|||
NegRed: false, |
|||
}) |
|||
err = file.SetConditionalFormat("成绩单", "D3:F12", []excelize.ConditionalFormatOptions{ |
|||
{ |
|||
Type: "data_bar", |
|||
BarColor: "#dfb5ff", |
|||
MinType: "num", |
|||
Criteria: "=", |
|||
MinValue: "70", |
|||
MaxType: "num", |
|||
MaxValue: "100", |
|||
BarSolid: true, |
|||
}, |
|||
}) |
|||
if err != nil { |
|||
fmt.Printf("%+v\n", err) |
|||
} |
|||
columns := []string{"D", "E", "F", "G"} |
|||
for _, column := range columns { |
|||
file.SetConditionalFormat("成绩单", column+"3:"+column+"12", []excelize.ConditionalFormatOptions{ |
|||
{ |
|||
Type: "bottom", |
|||
Format: style1, |
|||
Criteria: "=", |
|||
Value: "1", |
|||
}, |
|||
}) |
|||
file.SetConditionalFormat("成绩单", column+"3:"+column+"12", []excelize.ConditionalFormatOptions{ |
|||
{ |
|||
Type: "top", |
|||
Format: style2, |
|||
Criteria: "=", |
|||
Value: "1", |
|||
}, |
|||
}) |
|||
} |
|||
for i := range columns { |
|||
for j := 3; j <= 12; j++ { |
|||
column := columns[i] |
|||
file.AddComment("成绩单", excelize.Comment{ |
|||
Author: "god", |
|||
AuthorID: 1, |
|||
Cell: column + strconv.Itoa(j), |
|||
Text: "红色最低分,绿色最高分", |
|||
}) |
|||
} |
|||
} |
|||
_ = file.SaveAs("D:\\note\\test.xlsx") |
|||
} |
|||
|
|||
func getChart() *excelize.Chart { |
|||
return &excelize.Chart{ |
|||
Type: excelize.Col, |
|||
XAxis: excelize.ChartAxis{ |
|||
Font: excelize.Font{ |
|||
Color: "#000000", |
|||
}, |
|||
}, |
|||
YAxis: excelize.ChartAxis{ |
|||
Font: excelize.Font{ |
|||
Color: "#000000", |
|||
}, |
|||
}, |
|||
Series: []excelize.ChartSeries{{ |
|||
Name: "成绩单!$D$2", |
|||
Categories: "成绩单!$B$3:$B$12,成绩单!$B$14", |
|||
Values: "成绩单!$D$3:$D$12,成绩单!$D$14", |
|||
DataLabelPosition: excelize.ChartDataLabelsPositionAbove}, |
|||
{ |
|||
Name: "成绩单!$E$2", |
|||
Categories: "成绩单!$B$3:$B$12,成绩单!$B$14", |
|||
Values: "成绩单!$E$3:$E$12,成绩单!$E$14", |
|||
DataLabelPosition: excelize.ChartDataLabelsPositionAbove}, |
|||
{ |
|||
Name: "成绩单!$F$2", |
|||
Categories: "成绩单!$B$3:$B$12,成绩单!$B$14", |
|||
Values: "成绩单!$F$3:$F$12,成绩单!$F$14", |
|||
DataLabelPosition: excelize.ChartDataLabelsPositionAbove}, |
|||
}, |
|||
|
|||
Legend: excelize.ChartLegend{ |
|||
Position: "right", |
|||
ShowLegendKey: true, |
|||
}, |
|||
Title: []excelize.RichTextRun{ |
|||
{ |
|||
Font: nil, |
|||
Text: "成绩单", |
|||
}, |
|||
}, |
|||
PlotArea: excelize.ChartPlotArea{ |
|||
ShowVal: true, |
|||
}, |
|||
} |
|||
} |
|||
``` |
|||
|
|||
效果图如下: |
|||
|
|||
 |
|||
|
After Width: 1873 | Height: 805 | Size: 250 KiB |
Write
Preview
Loading…
Cancel
Save
Reference in new issue