Golang | Docx编辑 | 解决unioffice/gooxml没有背景颜色(底纹颜色)方法问题

收费(功能全)

github.com/unidoc/unioffice

免费(不维护,基本够用)

github.com/carmel/gooxml

导入包

go get github.com/carmel/gooxml

基本使用

package main

import (
	"github.com/carmel/gooxml/color"
	"github.com/carmel/gooxml/document"
	"github.com/carmel/gooxml/measurement"
	"github.com/carmel/gooxml/schema/soo/wml"
)

func main() {
    doc := document.New()
    para := docc.AddParagraph()

		
    para.Properties().SetAlignment(wml.ST_JcLeft)                // 对齐
    para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) // 缩进
    para.Properties().Spacing().SetLineSpacing(1.5 * 12 * measurement.Point, wml.ST_LineSpacingRuleAuto)                                  // 行高

    run := para.AddRun()                          // 创建一个新段
    run.AddText(text.Content)                     // 增加文字
    run.Properties().SetSize(10)                  // 字体大小
    run.Properties().SetColor(color.RGB(0, 0, 0)) // 字体颜色
    run.Properties().SetBold(true)                // 加粗
    run.Properties().SetHighlight(color.Red)      // 突出显示
    run.Properties().SetStrikeThrough(true)       // 删线
    run.Properties().SetUnderline(wml.ST_UnderlineWavyHeavy, color.Black) // 下划线
    run.Properties().SetItalic(true)              // 斜体
    run.Properties().SetFontFamily("黑体")        // 字体



    // 第三方库没有底纹颜色方法,自定义
    SetShading(run, color.RGB(0, 0, 0)) // 底纹颜色
	if err := doc.SaveToFile(path); err != nil {
        fmt.Println(err)
        return
    }
}

func SetShading(run document.Run, color color.Color) {
	shd := wml.NewCT_Shd()
	shd.FillAttr = &wml.ST_HexColor{
		ST_HexColorRGB: color.AsRGBAString(),
	}
	shd.ValAttr = wml.ST_ShdClear
	run.X().RPr.Shd = shd
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Golang 中,使用 `github.com/jlaffaye/ftp` 库可以连接到 FTP 服务器并进行文件的上传和下载等操作。 要向 FTP 服务器上已经存在的文件中写入数据,可以使用 `ftpConn.StorFrom` 方法,该方法定义如下: ``` func (c *Conn) StorFrom(filename string, r io.ReaderAt, offset int64) (int64, error) ``` 其中,`filename` 参数表示要写入数据的文件名;`r` 参数是一个实现了 `io.ReaderAt` 接口的对象,用于读取要写入的数据;`offset` 参数表示从文件的哪个位置开始写入数据。 示例代码如下: ```go package main import ( "fmt" "io/ioutil" "os" "github.com/jlaffaye/ftp" ) func main() { // 连接 FTP 服务器 conn, err := ftp.Dial("ftp.example.com:21") if err != nil { panic(err) } defer conn.Quit() // 登录 FTP 服务器 err = conn.Login("username", "password") if err != nil { panic(err) } // 打开文件进行写入 file, err := os.Open("data.txt") if err != nil { panic(err) } defer file.Close() // 读取文件内容 data, err := ioutil.ReadAll(file) if err != nil { panic(err) } // 写入数据到 FTP 服务器上的文件 n, err := conn.StorFrom("remote_file.txt", bytes.NewReader(data), 0) if err != nil { panic(err) } fmt.Printf("写入 %d 字节到文件\n", n) } ``` 在上面的示例代码中,我们首先连接到 FTP 服务器,然后登录该服务器。接着,我们打开本地的文件 `data.txt` 并读取该文件的内容,最后将读取到的数据写入到 FTP 服务器上的文件 `remote_file.txt` 中。在写入数据时,我们使用 `bytes.NewReader` 将读取到的数据转换为 `io.ReaderAt` 接口类型的对象,以便传递给 `ftpConn.StorFrom` 方法。 需要注意的是,当使用 `ftpConn.StorFrom` 方法写入数据时,如果要覆盖 FTP 服务器上已经存在的文件,需要先执行 `ftpConn.Delete` 方法删除该文件,然后再执行写入操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值