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)
}
}
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)
}
}
func (f *File) SetCellDefault(sheet string, axis string, value string) error
f.SetCellDefault("Sheet1","B1","liuBbei")
据说和“默认字符型的区别是”它会进行特殊字符过滤,并且字符串的累计长度应不超过 32767,但是并没有试验出差别
func (f *File) SetCellStr(sheet, axis, value string) error
f.SetCellStr("Sheet1","B1","liuBbei")
func (f *File) SetCellInt(sheet, axis string, value int) error
f.SetCellInt("Sheet1","B1",1)
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string, opts ...HyperlinkOpts) error
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")
}
func (f *File) GetCellHyperLink(sheet, axis string) (bool, string, error)
func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error
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")
}
结果显示
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}]
func (f *File) GetCellValue(sheet, axis string, opts ...Options) (string, error)
cell, err := f.GetCellValue("Sheet1", "B2")
func (f *File) GetCols(sheet string, opts ...Options) ([][]string, error)
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] [责任人 关羽 关羽 关羽 关羽 诸葛亮 诸葛亮 诸葛亮 诸葛亮 诸葛亮 张飞 张飞 张飞 赵云 赵云 赵云]]
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 赵云]]
func (f *File) GetCellType(sheet, axis string) (CellType, error)
func (f *File) GetCellStyle(sheet, axis string) (int, error)
func (f *File) MergeCell(sheet, hCell, vCell string) error
err := f.UnmergeCell("Sheet1", "B2", "D4")
func (f *File) UnmergeCell(sheet string, hCell, vCell string) error
err := f.UnmergeCell("Sheet1", "B2", "D4")
获取整个表的所有合并单元格位置
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)
}
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
获取合并单元格区域左上角单元格坐标
func (m *MergeCell) GetEndAxis() string