设置富文本格式
func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error
根据给定的工作表、单元格坐标和富文本格式为指定单元格设置富文本。
例如,在名为 Sheet1
的工作表 A1
单元格设置富文本格式:
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
if err := f.SetRowHeight("Sheet1", 1, 35); err != nil {
fmt.Println(err)
return
}
if err := f.SetColWidth("Sheet1", "A", "A", 44); err != nil {
fmt.Println(err)
return
}
if err := f.SetCellRichText("Sheet1", "A1", []excelize.RichTextRun{
{
Text: "bold",
Font: &excelize.Font{
Bold: true,
Color: "2354E8",
Family: "Times New Roman",
},
},
{
Text: " and ",
Font: &excelize.Font{
Family: "Times New Roman",
},
},
{
Text: "italic ",
Font: &excelize.Font{
Bold: true,
Color: "E83723",
Italic: true,
Family: "Times New Roman",
},
},
{
Text: "text with color and font-family,",
Font: &excelize.Font{
Bold: true,
Color: "2354E8",
Family: "Times New Roman",
},
},
{
Text: "\r\nlarge text with ",
Font: &excelize.Font{
Size: 14,
Color: "AD23E8",
},
},
{
Text: "strike",
Font: &excelize.Font{
Color: "E89923",
Strike: true,
},
},
{
Text: " superscript",
Font: &excelize.Font{
Color: "DBC21F",
VertAlign: "superscript",
},
},
{
Text: " and ",
Font: &excelize.Font{
Size: 14,
Color: "AD23E8",
VertAlign: "baseline",
},
},
{
Text: "underline",
Font: &excelize.Font{
Color: "23E833",
Underline: "single",
},
},
{
Text: " subscript.",
Font: &excelize.Font{
Color: "017505",
VertAlign: "subscript",
},
},
}); err != nil {
fmt.Println(err)
return
}
style, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
WrapText: true,
},
})
if err != nil {
fmt.Println(err)
return
}
if err := f.SetCellStyle("Sheet1", "A1", "A1", style); err != nil {
fmt.Println(err)
return
}
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}