.docx模板如何使用golang(表格内容)

如何将 golang 用于 .docx 模板(表格内容):

像那样(客户数量是动态的)

http://img.mukewang.com/63847c810001875706100159.jpg

FFIVE
浏览 215回答 1
1回答

白板的微信

您可以使用这个简单的包:"github.com/lukasjarosch/go-docx".&nbsp;{variables}这个包可以帮助您通过替换给定的文本上下文来填充 docx 文件模板。示例用法:填充模板的代码:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; docx "github.com/lukasjarosch/go-docx")func main() {&nbsp; &nbsp; replaceMap := docx.PlaceholderMap{&nbsp; &nbsp; &nbsp; &nbsp; "_contract_name_": "Home rental",&nbsp; &nbsp; &nbsp; &nbsp; "_name_":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "John Doe",&nbsp; &nbsp; &nbsp; &nbsp; "_summary_":&nbsp; &nbsp; &nbsp; &nbsp;"Terms and conditions",&nbsp; &nbsp; &nbsp; &nbsp; "_date_":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "13-04-2022",&nbsp; &nbsp; &nbsp; &nbsp; "_condition_1_":&nbsp; &nbsp;"apartment should always be cleaned",&nbsp; &nbsp; &nbsp; &nbsp; "_condition_2_":&nbsp; &nbsp;"term 2 ...",&nbsp; &nbsp; &nbsp; &nbsp; "_condition_4_":&nbsp; &nbsp;"term 4 ...",&nbsp; &nbsp; &nbsp; &nbsp; "_condition_3_":&nbsp; &nbsp;"term 3 ...",&nbsp; &nbsp; &nbsp; &nbsp; "_condition_5_":&nbsp; &nbsp;"term 5 ...",&nbsp; &nbsp; }&nbsp; &nbsp; for i := 1; i <= 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; replaceMap[fmt.Sprintf("_accept_%d", i)] = "✔️"&nbsp; &nbsp; &nbsp; &nbsp; replaceMap[fmt.Sprintf("_reject_%d", i)] = ""&nbsp; &nbsp; }&nbsp; &nbsp; // read and parse the template docx&nbsp; &nbsp; doc, err := docx.Open("template.docx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; // replace the keys with values from replaceMap&nbsp; &nbsp; err = doc.ReplaceAll(replaceMap)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; // write out a new file&nbsp; &nbsp; err = doc.WriteToFile("replaced.docx")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }}结果文件:PS:这个包不提供插入图片的功能。如果你想插入图片,你可以使用这个商业包:"github.com/unidoc/unioffice/document"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go