设置富文本格式
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()
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: " and ",
Font: &excelize.Font{
Size: 14,
Color: "ad23e8",
},
},
{
Text: "underline.",
Font: &excelize.Font{
Color: "23e833",
Underline: "single",
},
},
}); 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)
}
}