qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.05k stars 1.71k forks source link

Can I set the styles for both RichTextRun and Cell at the same location? #1845

Closed ShowerBandV closed 6 months ago

ShowerBandV commented 6 months ago

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

xuri commented 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)
    }
}
ShowerBandV commented 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!