Closed ShowerBandV closed 6 months ago
Thanks for your issue. The SetRow
function of the stream writer accepts []excelize.RichTextRun
data type in the Cell
data type, so you can set rich text with background color with stream writer like this:
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)
}
}()
sw, err := f.NewStreamWriter("Sheet1")
if err != nil {
fmt.Println(err)
return
}
styleID, err := f.NewStyle(&excelize.Style{
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"F1F4F7"},
Pattern: 1,
},
})
if err != nil {
fmt.Println(err)
return
}
if err := sw.SetRow("A1",
[]interface{}{
excelize.Cell{
StyleID: styleID, // Set cell style with background color
Value: []excelize.RichTextRun{ // Set rich text as cell value
{Text: "Rich ", Font: &excelize.Font{Color: "6666CC"}},
{Text: "Text", Font: &excelize.Font{Color: "0099CC"}},
}},
}); err != nil {
fmt.Println(err)
return
}
if err := sw.Flush(); err != nil {
fmt.Println(err)
return
}
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}
Thanks for your issue. The
SetRow
function of the stream writer accepts[]excelize.RichTextRun
data type in theCell
data type, so you can set rich text with background color with stream writer like this: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) } }() sw, err := f.NewStreamWriter("Sheet1") if err != nil { fmt.Println(err) return } styleID, err := f.NewStyle(&excelize.Style{ Fill: excelize.Fill{ Type: "pattern", Color: []string{"F1F4F7"}, Pattern: 1, }, }) if err != nil { fmt.Println(err) return } if err := sw.SetRow("A1", []interface{}{ excelize.Cell{ StyleID: styleID, // Set cell style with background color Value: []excelize.RichTextRun{ // Set rich text as cell value {Text: "Rich ", Font: &excelize.Font{Color: "6666CC"}}, {Text: "Text", Font: &excelize.Font{Color: "0099CC"}}, }}, }); err != nil { fmt.Println(err) return } if err := sw.Flush(); err != nil { fmt.Println(err) return } if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) } }
thanks!
I want to know how streamWriter sets rich text and background color in the same cell,I need write amount of cells but some of them is rich text with background color or other cell style