Go语言excelize包-05-单元格操作(设置单元格格式、超链接、富文本、获取单元格信息、合并单元格)

1. 设置单元格格式

1.1 值类型

  • 语法
func (f *File) SetCellValue(sheet string, axis string, value interface{}) error

说明:

  • axis:单元格位置
  • value:向单元格中写入的值
  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"time"
)

func main() {
	f := excelize.NewFile()

	t, _ := time.ParseInLocation("20060102150405", "20100424082959", time.Local)

	//字串
	f.SetCellValue("Sheet1","B1","LiuBei")
	//时间
	f.SetCellValue("Sheet1","B2",t)
	//整形
	f.SetCellValue("Sheet1","B3",11)
	//浮点型
	f.SetCellValue("Sheet1","B4",3.1415926)

	if err := f.SaveAs("sanGuo.xlsx"); err != nil {
		fmt.Println(err)
	}
}

1.2 设置布尔型值

  • 语法
func (f *File) SetCellBool(sheet, axis string, value bool) error
  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()
	
	f.SetCellBool("Sheet1","B1",true)
	f.SetCellBool("Sheet1","B2",false)

	if err := f.SaveAs("sanGuo.xlsx"); err != nil {
		fmt.Println(err)
	}
}
  • 结果
    在这里插入图片描述

1.3 设置为默认字符型值

  • 语法
func (f *File) SetCellDefault(sheet string, axis string, value string) error
  • 语法示例
f.SetCellDefault("Sheet1","B1","liuBbei")

1.4 设置字符型值

据说和“默认字符型的区别是”它会进行特殊字符过滤,并且字符串的累计长度应不超过 32767,但是并没有试验出差别

  • 语法
func (f *File) SetCellStr(sheet, axis, value string) error
  • 语法示例
f.SetCellStr("Sheet1","B1","liuBbei")

1.5 设置实数格式

  • 语法
func (f *File) SetCellInt(sheet, axis string, value int) error
  • 语法示例
f.SetCellInt("Sheet1","B1",1)

2. 超链接

2.1 设置超链接

  • 语法
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string, opts ...HyperlinkOpts) error
  • HyperlinkOpts结构体
type HyperlinkOpts struct {
    Display *string
    Tooltip *string
}
  • 语法示例
	f.SetCellHyperLink("Sheet1", "B2", url, "External", excelize.HyperlinkOpts{
			Display: &url,
			Tooltip: &tooltip,
		})
  • 完整示例
package main

import (
	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()

	tooltip := "My blog address"
	url := "https://blog.csdn.net/xingzuo_1840"

	//为单元格设置超链接
	f.SetCellHyperLink("Sheet1", "B2",url, "External", excelize.HyperlinkOpts{
			Display: &url,
			Tooltip: &tooltip,
		})
	
	// 为单元格设置字体和下划线样式
	style,_ := f.NewStyle(&excelize.Style{
		Font: &excelize.Font{Color: "#1265BE", Underline: "single"},
	})
	f.SetCellStyle("Sheet1", "B2", "A3", style)
	
	//给单元格写写内容
	f.SetCellValue("Sheet1", "B2", "LiuBei")

	f.SaveAs("sanGuo.xlsx")
}

结果显示
在这里插入图片描述

2.2 获取超链接

func (f *File) GetCellHyperLink(sheet, axis string) (bool, string, error)

3.富文本

3.1 设置富文本格式

  • 语法
func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error
  • RichTextRun 结构体
type RichTextRun struct {
    Font *Font
    Text string
}
  • 完整示例
package main

import (
	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()

	//设置富文本
	runs := []excelize.RichTextRun{
		{
		  Font: &excelize.Font{
			Size: 20,
			Color: "#0000FF",
		    },
		  Text: "LiuBei",
	    },
		{
		  Font: &excelize.Font{
			Size: 12,
			Color: "#FF0000",
			},
		  Text: "\r\nGuanYu",
		},
		{
		  Font: &excelize.Font{
				Size: 12,
				Color: "#000000",
		  },
		  Text: "\r\nZhangFei",
		},
	}
	f.SetCellRichText("Sheet1","B2",runs)


	//设置自动换行
	styleId,_ := f.NewStyle(&excelize.Style{
		Alignment: &excelize.Alignment{
			WrapText: true,
		},
	})
	f.SetCellStyle("Sheet1","B2","B2",styleId)

	
	//给单元格写写内容
	
	f.SaveAs("sanGuo.xlsx")
}

结果显示

在这里插入图片描述

3.2 获取富文本格式

  • 语法

  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
    //读取刚才生成的电子表格
	f, err := excelize.OpenFile("sanGuo.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err = f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	// 获取富文本格式
	rune,_ := f.GetCellRichText("Sheet1","B2")
	fmt.Printf("%+v",rune)
}
  • 结果显示
[{Font:0xc00034d920 Text:LiuBei} 
{Font:0xc00034d980 Text:GuanYu}
{Font:0xc00034d9e0 Text:ZhangFei}]

4. 获取单元格信息

4.1 获取单元格的值

  • 语法
func (f *File) GetCellValue(sheet, axis string, opts ...Options) (string, error)
  • 语法示例
cell, err := f.GetCellValue("Sheet1", "B2")

4.2 按列获取单元格数据

  • 语法
func (f *File) GetCols(sheet string, opts ...Options) ([][]string, error)
  • 完整示例
    • 创建sanGuo.xlsx表如下:
      在这里插入图片描述
    • 代码
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
	f, err := excelize.OpenFile("sanGuo.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err = f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	cols,_ := f.GetCols("Sheet1")
	fmt.Printf("GetCols函数获取到数据:\n%+v\n",cols)
}

结果显示

GetCols函数获取到数据:
[[序号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] [IP 10.10.181.128 10.10.181.129 10.10.181.130 10.10.181.131 10.10.181.132 10.10.181.133 10.10.181.134 10.10.181.135 10.10.181.136 10.10.181.137 10.10.181.138 10.10.181.139 10.10.181.140 10.10.181.141 10.10.181.142] [责任人 关羽 关羽 关羽 关羽 诸葛亮 诸葛亮 诸葛亮 诸葛亮 诸葛亮 张飞 张飞 张飞 赵云 赵云 赵云]]

4.3 按行获取全部单元格的值

  • 语法
func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error)
  • 语法示例
rows,_ := f.GetRows("Sheet1")
  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
	f, err := excelize.OpenFile("sanGuo.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err = f.Close(); err != nil {
			fmt.Println(err)
		}
	}()
	rows,_ := f.GetRows("Sheet1")
	fmt.Printf("GetRows函数获取到数据:\n%+v\n",rows)
}
  • 结果显示
GetRows函数获取到数据:
[[序号 IP 责任人] [1 10.10.181.128 关羽] [2 10.10.181.129 关羽] [3 10.10.181.130 关羽] [4 10.10.181.131 关羽] [5 10.10.181.132 诸葛亮] [6 10.10.181.133 诸葛亮] [7 10.10.181.134 诸葛亮] [8 10.10.181.135 诸葛亮] [9 10.10.181.136 诸葛亮] [10 10.10.181.137 张飞] [11 10.10.181.138 张飞] [12 10.10.181.139 张飞] [13 10.10.181.140 赵云] [14 10.10.181.141 赵云] [15 10.10.181.142 赵云]]

4.4 获取单元格类型

  • 语法
func (f *File) GetCellType(sheet, axis string) (CellType, error)

4.5 获取样式索引

func (f *File) GetCellStyle(sheet, axis string) (int, error)

5. 合并单元格

5.1 合并单元格

  • 语法
func (f *File) MergeCell(sheet, hCell, vCell string) error
  • 语法示例
err := f.UnmergeCell("Sheet1", "B2", "D4")

5.2 取消合并单元格

  • 语法
func (f *File) UnmergeCell(sheet string, hCell, vCell string) error
  • 语法示例
err := f.UnmergeCell("Sheet1", "B2", "D4")

5.3 获取合并单元格位置

获取整个表的所有合并单元格位置

  • 语法
func (f *File) GetMergeCells(sheet string) ([]MergeCell, error)

说明:MergeCell 类型为 []string,标记了一个合并单元格的开始和结束位置。

  • 语法示例
mergeCells,_ := f.GetMergeCells("Sheet1")
  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()
	f.MergeCell("Sheet1","B2","D4")
	f.MergeCell("Sheet1","B9","E10")
	mergeCells,_ := f.GetMergeCells("Sheet1")
	fmt.Printf("%+v",mergeCells)
}

5.4 获取合并单元格数值

  • 语法
func (m *MergeCell) GetCellValue() string

注意,该方法是*MergeCell,不是*File

  • 完整示例
package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()
	f.SetCellValue("sheet1","B2","LiuBei")
	f.SetCellValue("sheet1","B9","GuanYu")
	f.MergeCell("Sheet1","B2","D4")
	f.MergeCell("Sheet1","B9","E10")
	mergeCells,_ := f.GetMergeCells("Sheet1")
	for _,mergeCell := range mergeCells {
		mergeCellValue := mergeCell.GetCellValue()
		fmt.Println(mergeCellValue)
	}
}
  • 结果显示
LiuBei
GuanYu

5.5 获取合并单元格区域左上角单元格坐标

  • 语法
获取合并单元格区域左上角单元格坐标

5.6 获取合并单元格区域左上角单元格坐标

  • 语法
func (m *MergeCell) GetEndAxis() string

在这里插入图片描述

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值