在企业级应用开发中,Word文档操作是一个常见需求。无论是生成报告、制作合同模板,还是批量处理文档,我们都需要一个可靠、高效的文档操作库。今天给大家介绍一个纯Go语言实现的Word文档操作库——WordZero,让复杂的文档操作变得简单而优雅。
WordZero完全使用Go语言编写,无需任何外部依赖,符合Go语言"少即是多"的设计哲学。安装即用,部署无忧。
内置18种预定义样式,完全兼容Word内置样式,包括标题、正文、代码块、引用等,让你的文档瞬间变得专业。
go get github.com/ZeroHawkeye/wordZero
让我们从最简单的例子开始:
package main
import (
"github.com/ZeroHawkeye/wordZero/pkg/document"
"github.com/ZeroHawkeye/wordZero/pkg/style"
)
func main() {
// 创建新文档
doc := document.New()
// 添加标题
title := doc.AddParagraph("我的第一个WordZero文档")
title.SetStyle(style.StyleTitle)
// 添加正文
content := doc.AddParagraph("WordZero让Go语言操作Word文档变得如此简单!")
content.SetStyle(style.StyleNormal)
// 保存文档
doc.Save("hello_wordzero.docx")
}
仅仅几行代码,你就创建了一个完整的Word文档!
WordZero支持丰富的文本格式化功能:
// 创建混合格式段落
para := doc.AddParagraph("")
para.AddFormattedText("普通文字", nil)
para.AddFormattedText("粗体文字", &document.TextFormat{Bold: true})
para.AddFormattedText("红色文字", &document.TextFormat{FontColor: "FF0000"})
para.AddFormattedText("大号字体", &document.TextFormat{FontSize: 16})
创建专业的表格从未如此简单:
// 创建表格数据
tableData := [][]string{
{"姓名", "职位", "部门"},
{"张三", "工程师", "技术部"},
{"李四", "经理", "销售部"},
}
// 配置表格
config := &document.TableConfig{
Rows: 3,
Cols: 3,
Width: 8000,
Data: tableData,
}
// 添加表格到文档
table := doc.AddTable(config)
// 动态修改表格内容
table.SetCellText(1, 1, "高级工程师")
表格还支持:
WordZero内置了完整的样式系统,支持Word的18种预定义样式:
// 使用预定义样式
doc.AddParagraph("这是一级标题").SetStyle(style.StyleHeading1)
doc.AddParagraph("这是二级标题").SetStyle(style.StyleHeading2)
doc.AddParagraph("这是代码块").SetStyle(style.StyleCodeBlock)
doc.AddParagraph("这是引用").SetStyle(style.StyleQuote)
这些样式完全兼容Word导航窗格,生成的文档可以在Word中正常显示目录结构。
// 设置页面为A4尺寸
err := doc.SetPageSize(document.PageSizeA4)
// 设置页面方向为横向
err = doc.SetPageOrientation(document.OrientationLandscape)
// 设置页面边距(毫米为单位)
err = doc.SetPageMargins(20, 20, 20, 20) // 上下左右边距各20mm
// 或者设置自定义页面尺寸
err = doc.SetCustomPageSize(200, 300) // 宽200mm,高300mm
doc := document.New()
// 报告标题
doc.AddParagraph("月度销售报告").SetStyle(style.StyleTitle)
doc.AddParagraph("2024年3月").SetStyle(style.StyleSubtitle)
// 添加汇总表格
summaryData := [][]string{
{"指标", "本月", "上月", "同比"},
{"销售额", "100万", "95万", "+5.3%"},
{"订单数", "1250", "1180", "+5.9%"},
}
config := &document.TableConfig{
Rows: 3,
Cols: 4,
Width: 8000,
Data: summaryData,
}
doc.AddTable(config)
// 模板数据
contracts := []struct{
Name string
Amount string
Date string
}{
{"张三", "10万", "2024-03-15"},
{"李四", "15万", "2024-03-16"},
}
for _, contract := range contracts {
doc := document.New()
doc.AddParagraph("服务合同").SetStyle(style.StyleTitle)
doc.AddParagraph(fmt.Sprintf("甲方:%s", contract.Name))
doc.AddParagraph(fmt.Sprintf("金额:%s", contract.Amount))
doc.Save(fmt.Sprintf("contract_%s.docx", contract.Name))
}
WordZero是一个完全开源的项目,欢迎社区贡献:
GitHub仓库: https://github.com/ZeroHawkeye/wordZero
GitHub Wiki: https://github.com/ZeroHawkeye/wordZero/wiki
Gitee仓库: https://gitee.com/Zmata_admin/WordZero
Gitee Wiki: https://gitee.com/Zmata_admin/WordZero/wikis/Home
特性 | WordZero | 其他Go库 | Office COM |
---|---|---|---|
纯Go实现 | ✅ | ❌ | ❌ |
跨平台 | ✅ | ✅ | ❌ |
零依赖 | ✅ | ❌ | ❌ |
样式系统 | ✅ | ❌ | ✅ |
易用性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ |
性能 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐ |
WordZero为Go语言开发者提供了一个强大而简洁的Word文档操作解决方案。无论你是需要生成报告、处理模板,还是批量操作文档,WordZero都能让你的工作变得更加高效。
立即开始你的WordZero之旅吧!
go get github.com/ZeroHawkeye/wordZero
提示: 更多详细教程和示例代码,请访问项目的GitHub仓库。如果这个项目对你有帮助,别忘了给个Star⭐支持一下哦!
作者简介: 专注于Go语言开发,致力于构建高质量的开源工具。欢迎关注我的CSDN博客,获取更多技术分享。
相关文章推荐:
标签: #Golang #Word文档 #开源项目 #文档处理 #OOXML