package produce import ( "fmt" "log" "time" "xgAutoTest/internal/global" "github.com/carmel/gooxml/color" "github.com/carmel/gooxml/common" "github.com/carmel/gooxml/document" "github.com/carmel/gooxml/measurement" "github.com/carmel/gooxml/schema/soo/wml" ) type TableUpperPart struct { Left []string Right []string SqlToTextsList []string ImagesList [][]byte } type WordInfo struct { doc *document.Document } func ToWord() { //1. 创建文档并填写内容 doc := document.New() para := doc.AddParagraph() // 新增段落 run := para.AddRun() run.AddText("这里是段落文字信息") // 添加文字信息 // 如果想对添加的文字信息进行换行处理,使用'\r' run.AddText("这里第一行段落文字信息\r这里是第二行段落文字信息") // 2. 给段落添加各种样式 para.SetStyle("Title") para.SetStyle("Heading1") // Heading1 Heading2 Heading3 para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) // 段落添加首行缩进 para.Properties().AddSection(wml.ST_SectionMarkNextPage) // 另起一页(用在AddText之后) // 3. 给文字添加各种样式 run.Properties().SetBold(true) // 是否加粗 run.Properties().SetFontFamily("Courier") // 字体 run.Properties().SetSize(15) // 字号 run.Properties().SetColor(color.Red) // 文字颜色 run.Properties().SetKerning(5) // 文字字距 run.Properties().SetCharacterSpacing(5) // 字符间距调整 run.Properties().SetHighlight(wml.ST_HighlightColorYellow) // 设置高亮 run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red) // 下划线 // 4. 设置页眉页脚 hdr := doc.AddHeader() para = hdr.AddParagraph() para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone) run = para.AddRun() run.AddTab() run.AddText("My Document Title") ftr := doc.AddFooter() para = ftr.AddParagraph() para.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone) run = para.AddRun() run.AddText("Some subtitle goes here") run.AddTab() // 5. 添加图片 // // 初始化图片信息 img1, err := common.ImageFromFile("./test_pic_1.png") if err != nil { log.Fatalf("unable to create image: %s", err) } img1ref, err := doc.AddImage(img1) if err != nil { log.Fatalf("unable to add image to document: %s", err) } // 将图片添加到对应的段落 para = doc.AddParagraph() anchored, err := para.AddRun().AddDrawingAnchored(img1ref) if err != nil { log.Fatalf("unable to add anchored image: %s", err) } // 设置图片相关样式 anchored.SetName("图片名称") anchored.SetSize(2*measurement.Inch, 2*measurement.Inch) anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin) anchored.SetHAlignment(wml.WdST_AlignHCenter) anchored.SetYOffset(3 * measurement.Inch) anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides) doc.SaveToFile("simple.docx") // 保存文件路径,此处应为绝对路径 } func Newword() *WordInfo { return &WordInfo{ doc: document.New(), } } func (wd *WordInfo) WriterTable(fileLocal string, tableName string, tableUpperPart TableUpperPart, SingleImage []byte) { //wd.doc = document.New() // 写入表名 wd.doc.AddParagraph().AddRun().AddText(tableName) // 添加一个表格 table := wd.doc.AddTable() // width of the page table.Properties().SetWidthAuto() // with thick borers borders := table.Properties().Borders() borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero) for i, left := range tableUpperPart.Left { addRow(&table, left, tableUpperPart.Right[i]) } wd.InsertSingleImage(&table, SingleImage) wd.doc.AddParagraph() } func (wd *WordInfo) SaveWord(fileLocal string) { timeNow := time.Now().Format("2006-01-02-15.04.05") wd.doc.SaveToFile(fmt.Sprintf("%s/ok_%s.docx", fileLocal, timeNow)) } func addRow(table *document.Table, col1, col2 string) { row := table.AddRow() rowCell := row.AddCell() rowCell.Properties().SetWidthPercent(60) rowCell.AddParagraph().AddRun().AddText(col1) row.AddCell().AddParagraph().AddRun().AddText(col2) } // 插入合并后的图片到表格内 func (wd *WordInfo) InsertSingleImage(table *document.Table, data []byte) { row := table.AddRow() row.AddCell().AddParagraph().AddRun().AddText("测试结果截图") ///row = table.AddRow() ParagraphTemp := row.AddCell().AddParagraph() img1, err := common.ImageFromBytes(data) if err != nil { log.Fatalf("unable to create image: %s", err) } // 创建一个图片对象 image, err := wd.doc.AddImage(img1) // 替换为实际图片路径 if err != nil { log.Fatalf("unable to doc.AddImage: %s", err) } inlineDrawing, err := ParagraphTemp.AddRun().AddDrawingInline(image) if err != nil { global.Logs.Fatalf("unable to doc.AddImage: %s", err) } inlineDrawing.SetSize(100, 100) // 设置图像大小为 100x100 像素 } func (wd *WordInfo) InsertImages(table *document.Table, data [][]byte) { row := table.AddRow() ParagraphTemp := row.AddCell().AddParagraph() for _, v := range data { img1, err := common.ImageFromBytes(v) if err != nil { log.Fatalf("unable to create image: %s", err) } // 创建一个图片对象 image, err := wd.doc.AddImage(img1) // 替换为实际图片路径 if err != nil { log.Fatalf("unable to doc.AddImage: %s", err) } _, err = ParagraphTemp.AddRun().AddDrawingInline(image) if err != nil { global.Logs.Fatalf("unable to doc.AddImage: %s", err) } //inlineDrawing.SetSize(100, 100) // 设置图像大小为 100x100 像素 } }