技术标签: Python&Go自动化办公 golang 开发语言
新建样式
func (f *File) NewStyle(style interface{}) (int, error)
通过给定的样式格式 JSON 或结构体的指针创建样式并返回样式索引。请注意,颜色需要使用 RGB 色域代码表示。
设置样式
func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) error
样式结构体包含的设置项
type Style struct {
Border []Border `json:"border"`
Fill Fill `json:"fill"`
Font *Font `json:"font"`
Alignment *Alignment `json:"alignment"`
Protection *Protection `json:"protection"`
NumFmt int `json:"number_format"`
DecimalPlaces int `json:"decimal_places"`
CustomNumFmt *string `json:"custom_number_format"`
Lang string `json:"lang"`
NegRed bool `json:"negred"`
}
type Border struct {
Type string `json:"type"` // top bottom left right diagonalDown diagonalUp
Color string `json:"color"` // 常用的颜色字符串 以及 十六进制的颜色编 red #F8F8F8
Style int `json:"style"` // 见下表 样式可以去文档上看
}
| 索引 | 线条样式 | 线宽 |
|---|---|---|
| 0 | 无 | 0 |
| 1 | 连续线 | 1 |
| 2 | 连续线 | 2 |
| 3 | 短线 | 1 |
| 4 | 点线 | 1 |
| 5 | 连续线 | 3 |
| 6 | 双线 | 3 |
| 7 | 连续线 | 0 |
| 8 | 短线 | 2 |
| 9 | 短线与点间隔线 | 1 |
| 10 | 短线与点间隔线 | 2 |
| 11 | 短线与两个点一组重复线 | 1 |
| 12 | 短线与两个点一组重复线 | 2 |
| 13 | 斜线与点线 | 2 |
type Fill struct {
Type string `json:"type"` // gradient pattern
Pattern int `json:"pattern"`
Color []string `json:"color"`
Shading int `json:"shading"`
}
填充有两种类型 即 Type 有两种选择 gradient -> 渐变色 pattern -> 填充图案
当Type是 gradient 时,Shading 可以选择 0-5 分别代表标横向(每种颜色横向分布),纵向,纵向 对角向上 对角向下 有外向内 由内向外;此时的Color 切片中的颜色可以有多个,Pattern 可以忽略
当Type是 pattern 时,Pattern 可选 0-18 其中 1是实体填充 ;Color 切片中只要一种;Shading可以不填
只有上述两种组合,其他的组合都不显示填充颜色
type Font struct {
Bold bool `json:"bold"` // 是否加粗
Italic bool `json:"italic"` // 是否倾斜
Underline string `json:"underline"` // single double
Family string `json:"family"` // 字体样式
Size float64 `json:"size"` // 字体大小
Strike bool `json:"strike"` // 删除线
Color string `json:"color"` // 字体颜色
}
注意字体是指针类型的
type Protection struct {
Hidden bool `json:"hidden"`
Locked bool `json:"locked"`
}
貌似没用,待进一步学习
type Alignment struct {
Horizontal string `json:"horizontal"` // 水平对齐方式
Indent int `json:"indent"` // 缩进 只要设置了值,就变成了左对齐
JustifyLastLine bool `json:"justify_last_line"` // 两端分散对齐,只有在水平对齐选择 distributed 时起作用
ReadingOrder uint64 `json:"reading_order"` // 文字方向 不知道值范围和具体的含义
RelativeIndent int `json:"relative_indent"` // 不知道具体的含义
ShrinkToFit bool `json:"shrink_to_fit"` // 缩小字体填充
TextRotation int `json:"text_rotation"` // 文本旋转
Vertical string `json:"vertical"` // 垂直对齐
WrapText bool `json:"wrap_text"` // 自动换行
}
对齐方式也是指针类型
多个样式设置在同一个单元格,后面的会覆盖前面的
推荐使用JSON字符串的方式创建
Al_style, err := wb.NewStyle(`{"alignment":{
"horizontal":"center",
"vertical":"center"
}}`)
if err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "F6", "F6", Al_style); err != nil {
fmt.Println(err)
}
练习案例:main.go
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
// wb := excelize.NewFile()
wb, err := excelize.OpenFile("../excel_files/TMP_05.xlsx")
if err != nil {
fmt.Println(err)
return
}
sheetName := wb.GetSheetName(wb.GetActiveSheetIndex())
sty_idx, err := wb.NewStyle(&excelize.Style{
Border: []excelize.Border{
{
Type: "right", // top bottom left right diagonalDown diagonalUp 中的一个
Color: "#000000", // 十六进制的颜色编码
Style: 2, // 0-13 有对应的样式
},
{
Type: "left",
Color: "#000000",
Style: 2,
},
{
Type: "top",
Color: "#000000",
Style: 2,
},
{
Type: "bottom",
Color: "#000000",
Style: 2,
},
}, Fill: excelize.Fill{
Type: "gradient", // gradient 渐变色 pattern 填充图案
// Pattern: 1, // 填充样式 当类型是 pattern 0-18 填充图案 1 实体填充
// Color: []string{"#FF0000"}, // 当Type = pattern 时,只有一个
Color: []string{"#00F700", "#00F700"},
Shading: 1, // 类型是 gradient 使用 0-5 横向(每种颜色横向分布) 纵向 对角向上 对角向下 有外向内 由内向外
}, Font: &excelize.Font{
Bold: true,
// Italic: false,
// Underline: "single",
Size: 14,
Family: "宋体",
// Strike: true, // 删除线
Color: "#0000FF",
}, Alignment: &excelize.Alignment{
Horizontal: "center", // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
Vertical: "center", // 垂直对齐方式 center top justify distributed
// Indent: 1, // 缩进 只要有值就变成了左对齐 + 缩进
// TextRotation: 30, // 旋转
// RelativeIndent: 10, // 好像没啥用
// ReadingOrder: 0, // 不知道怎么设置
// JustifyLastLine: true, // 两端分散对齐,只有 水平对齐 为 distributed 时 设置true 才有效
// WrapText: true, // 自动换行
// ShrinkToFit: true, // 缩小字体以填充单元格
}, Protection: &excelize.Protection{
Hidden: true, // 貌似没啥用
Locked: true, // 貌似没啥用
}, NumFmt: 0, // 内置的数字格式样式 0-638 常用的 0-58 配合lang使用,因为语言不同样式不同 具体的样式参照文档
Lang: "zh-cn", // zh-cn 中文
DecimalPlaces: 2, // 小数位数 只有NumFmt是 2-11 有效
// CustomNumFmt: "",// 自定义样式 是指针,只能通过变量的方式
NegRed: true, // 不知道具体的含义
})
if err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "A1", "B3", sty_idx); err != nil {
fmt.Println(err)
return
}
// 除了以Go语言的方式传参外,还支持JSON格式的传参
// 边框样式
border_sty, err := wb.NewStyle(`{"border":[
{"type":"top","color":"#000000","style":1},
{"type":"left","color":"#000000","style":1},
{"type":"right","color":"#000000","style":1},
{"type":"bottom","color":"#000000","style":1}
]}`)
if err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "D2", "G10", border_sty); err != nil {
fmt.Println(err)
}
// 字体样式
// 链接的字体样式 下划线 + 蓝色
font_link_sty, err := wb.NewStyle(`{"font":{"underline":"single","size":12,"color":"#0000FF","family":"仿宋"}}`)
if err != nil {
fmt.Println(err)
}
fmt.Println(font_link_sty)
if err := wb.SetCellValue(sheetName, "F6", "这是链接"); err != nil {
fmt.Println(err)
}
if err := wb.SetCellHyperLink(sheetName, "F6", "Sheet1!A1", "Location"); err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "F6", "F6", font_link_sty); err != nil {
fmt.Println(err)
}
styleIdx, err := wb.GetCellStyle(sheetName, "F6")
if err != nil {
fmt.Println(err)
}
fmt.Println(styleIdx)
// 对齐方式
Al_style, err := wb.NewStyle(`{"alignment":{
"horizontal":"center",
"vertical":"center"
}}`)
if err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "F6", "F6", Al_style); err != nil {
fmt.Println(err)
}
// 多个样式设置在同一个单元格,后面的会覆盖前面的
link_sty, err := wb.NewStyle(`{
"border":[
{"type":"top","color":"#000000","style":1},
{"type":"left","color":"#000000","style":1},
{"type":"right","color":"#000000","style":1},
{"type":"bottom","color":"#000000","style":1}
],
"font":{
"underline":"single",
"size":12,
"color":"#0000FF",
"family":"仿宋"
},
"alignment":{
"horizontal":"center",
"vertical":"center"
}
}`)
if err != nil {
fmt.Println(err)
}
if err := wb.SetCellStyle(sheetName, "D11", "D11", link_sty); err != nil {
fmt.Println(err)
}
if err := wb.SetCellValue(sheetName, "D11", "这是链接"); err != nil {
fmt.Println(err)
}
if err := wb.SetCellHyperLink(sheetName, "D11", "http://www.baidu.com", "External"); err != nil {
fmt.Println(err)
}
wb.Save()
// wb.SaveAs("../excel_files/TMP_05.xlsx")
}
更多内容去到大佬的文档或者视频
作者大佬的文档 : https://xuri.me/excelize/zh-hans/
作者大佬的视频 : https://www.bilibili.com/video/BV1hU4y1F7wQ
2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/liuhuiweb/blog/199024...
Golang导出并下载excel封装 博客地址 这里使用的是: github.com/xuri/excelize/v2 封装了两个方法,数据源为map和struct(map数据源表头和数据需要自己排序) 其他方法...
excel--excelize...
go get不能安装github包的解决办法_marina_1的博客-CSDN博客 根据上文,在linux终端运行了 结果是: 还有哪儿不对?...
使用go-excelize读取Excel指定列的数据...
CSS:层叠样式表,通过样式定义如何显示HTML标签,将内容的显示与表现进行了分离,提高了工作的效率。 使用CSS设置样式时,首先需要通过选择器获取到标签信息,然后才能对标签设置属性信息。 普通选择器 id选择器:标签中设置id属性,样式中用“#”代表id选择器。 类选择器:标签中设置class属性,样式中用“.”代表类选择器。 标签选择器:样式中使...
/* Copyright 2002-2019 Petro-CyberWorks Information Technology Co. Ltd. All rights reserved. PCITC PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.pcitc.scl.business.producto...
记录常用的excel样式设置: 注意,若引用到框架中,需注意类实例化 ...
...
在对打开的excel进行修改操作时发现程序报错: 错误信息已经定位得非常明确了,很显然是数组越界,找到对应的方法checkCellInArea其中 ref[1]并不存在,而引发该问题的根本原因是传入的参数area有问题,再看一下调用该方法的地方: 根源就在这里:xlsx.MergeCells.Cells这个数组的内容本应该是所有合并的单元格,但在处理我给的excel时却发现了并未合并的单元格,也就...