1. 程式人生 > 其它 >Golang採用OpenXML標準寫Word文件

Golang採用OpenXML標準寫Word文件

技術標籤:golanggomswordopenxml

Golang採用OpenXML標準寫Word文件

Git路徑:https://github.com/ErmaiSoft/GoOpenXml

##參考程式碼
package main

import (
“fmt”

"github.com/ErmaiSoft/GoOpenXml/word"

)

func main() {
titleFont := word.Font{Family: “微軟雅黑”, Size: 28, Bold: true, Color: “CC0000”}//字型
normalFont := word.Font{Family: “宋體”, Size: 10.5, Bold: false, Space: true, Color: “000000”}

normalBoldFont := word.Font{Family: “宋體”, Size: 10.5, Bold: true, Space: true, Color: “000000”}
normalLine := word.Line{Height: 1.5, Rule: word.LineRuleExact}//行高、行間距
subTitleFont := word.Font{Family: “微軟雅黑”, Size: 15, Bold: true, Color: “000000”}
subTitleLine := word.Line{Rule: word.LineRuleAuto, Height: 1.5}
contentFont := word.Font{Family: “宋體”, Size: 10.5, Bold: false, Color: “000000”}
contentLine := word.Line{Rule: word.LineRuleAuto, FirstLineChars: 2, Height: 1.5}//行高、行間距、首行縮排

docx := word.CreateDocx()
docx.AddParagraph([]word.Paragraph{
	{
		F: titleFont,
		L: word.Line{After: 0.8, Rule: word.LineRuleAuto},
		T: []word.Text{
			{T: "會議紀要", F: &titleFont},
		},
		Rect: []*word.DrawRect{
			{W: float64(docx.PageSize.W - docx.PageSize.L - docx.PageSize.R), H: 1, PH: 0, PV: 16, T: "line", C: "CC0000"},
			{W: float64(docx.PageSize.W - docx.PageSize.L - docx.PageSize.R), H: 0.25, PH: 0, PV: 17, T: "line", C: "CC0000"},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "時間"},
			{T: " | 2021-02-02 15:30", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "地點"},
			{T: " | 二樓會議室", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "應到"},
			{T: " | 25 人", F: &normalBoldFont},
			{T: "    實到", F: &normalFont},
			{T: " | 22 人", F: &normalBoldFont},
			{T: "    參會率", F: &normalFont},
			{T: " | 88%", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "出席"},
			{T: " | 張三、李四、王五", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "缺席"},
			{T: " | 麻六", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "列席"},
			{T: " | 周吳", F: &normalBoldFont},
		},
	},
	{
		F: normalFont,
		L: normalLine,
		T: []word.Text{
			{T: "主持人"},
			{T: " | 李總", F: &normalBoldFont},
		},
	},
	{
		F: word.Font{Family: "宋體", Size: 10.5, Color: "000000"},
		L: word.Line{Height: 1.5, After: 1, Rule: word.LineRuleAuto},
		T: []word.Text{
			{T: "記錄人"},
			{T: " | 張三", F: &normalBoldFont},
		},
		Rect: []*word.DrawRect{
			{W: float64(docx.PageSize.W - docx.PageSize.L - docx.PageSize.R), H: 1, PH: 0, PV: 8, T: "line", C: "CC0000"},
		}},
	{
		F: subTitleFont,
		L: subTitleLine,
		T: []word.Text{
			{T: "會議議題", F: &subTitleFont},
		},
	},
	{
		F: contentFont,
		L: contentLine,
		T: []word.Text{
			{T: "第一段文字",
				F: &contentFont},
		},
	},
	{
		F: contentFont,
		L: contentLine,
		T: []word.Text{
			{T: "第二段文字。",
				F: &contentFont},
		},
	},
	{
		F: contentFont,
		L: contentLine,
		T: []word.Text{
			{T: "第三段文字。",
				F: &contentFont},
		},
	},
})

err := docx.WriteToFile("D:/Dev/Demo/Word/word_test.docx")
if err != nil {
	fmt.Println(err)
}

}