word.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package produce
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "xgAutoTest/internal/global"
  7. "github.com/carmel/gooxml/color"
  8. "github.com/carmel/gooxml/common"
  9. "github.com/carmel/gooxml/document"
  10. "github.com/carmel/gooxml/measurement"
  11. "github.com/carmel/gooxml/schema/soo/wml"
  12. )
  13. type TableUpperPart struct {
  14. Left []string
  15. Right []string
  16. SqlToTextsList []string
  17. ImagesList [][]byte
  18. }
  19. type WordInfo struct {
  20. doc *document.Document
  21. }
  22. func ToWord() {
  23. //1. 创建文档并填写内容
  24. doc := document.New()
  25. para := doc.AddParagraph() // 新增段落
  26. run := para.AddRun()
  27. run.AddText("这里是段落文字信息") // 添加文字信息
  28. // 如果想对添加的文字信息进行换行处理,使用'\r'
  29. run.AddText("这里第一行段落文字信息\r这里是第二行段落文字信息")
  30. // 2. 给段落添加各种样式
  31. para.SetStyle("Title")
  32. para.SetStyle("Heading1") // Heading1 Heading2 Heading3
  33. para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) // 段落添加首行缩进
  34. para.Properties().AddSection(wml.ST_SectionMarkNextPage) // 另起一页(用在AddText之后)
  35. // 3. 给文字添加各种样式
  36. run.Properties().SetBold(true) // 是否加粗
  37. run.Properties().SetFontFamily("Courier") // 字体
  38. run.Properties().SetSize(15) // 字号
  39. run.Properties().SetColor(color.Red) // 文字颜色
  40. run.Properties().SetKerning(5) // 文字字距
  41. run.Properties().SetCharacterSpacing(5) // 字符间距调整
  42. run.Properties().SetHighlight(wml.ST_HighlightColorYellow) // 设置高亮
  43. run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red) // 下划线
  44. // 4. 设置页眉页脚
  45. hdr := doc.AddHeader()
  46. para = hdr.AddParagraph()
  47. para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
  48. run = para.AddRun()
  49. run.AddTab()
  50. run.AddText("My Document Title")
  51. ftr := doc.AddFooter()
  52. para = ftr.AddParagraph()
  53. para.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)
  54. run = para.AddRun()
  55. run.AddText("Some subtitle goes here")
  56. run.AddTab()
  57. // 5. 添加图片
  58. //
  59. // 初始化图片信息
  60. img1, err := common.ImageFromFile("./test_pic_1.png")
  61. if err != nil {
  62. log.Fatalf("unable to create image: %s", err)
  63. }
  64. img1ref, err := doc.AddImage(img1)
  65. if err != nil {
  66. log.Fatalf("unable to add image to document: %s", err)
  67. }
  68. // 将图片添加到对应的段落
  69. para = doc.AddParagraph()
  70. anchored, err := para.AddRun().AddDrawingAnchored(img1ref)
  71. if err != nil {
  72. log.Fatalf("unable to add anchored image: %s", err)
  73. }
  74. // 设置图片相关样式
  75. anchored.SetName("图片名称")
  76. anchored.SetSize(2*measurement.Inch, 2*measurement.Inch)
  77. anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin)
  78. anchored.SetHAlignment(wml.WdST_AlignHCenter)
  79. anchored.SetYOffset(3 * measurement.Inch)
  80. anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides)
  81. doc.SaveToFile("simple.docx") // 保存文件路径,此处应为绝对路径
  82. }
  83. func Newword() *WordInfo {
  84. return &WordInfo{
  85. doc: document.New(),
  86. }
  87. }
  88. func (wd *WordInfo) WriterTable(fileLocal string, tableName string, tableUpperPart TableUpperPart, SingleImage []byte) {
  89. //wd.doc = document.New()
  90. // 写入表名
  91. wd.doc.AddParagraph().AddRun().AddText(tableName)
  92. // 添加一个表格
  93. table := wd.doc.AddTable()
  94. // width of the page
  95. table.Properties().SetWidthAuto()
  96. // with thick borers
  97. borders := table.Properties().Borders()
  98. borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)
  99. for i, left := range tableUpperPart.Left {
  100. addRow(&table, left, tableUpperPart.Right[i])
  101. }
  102. wd.InsertSingleImage(&table, SingleImage)
  103. wd.doc.AddParagraph()
  104. }
  105. func (wd *WordInfo) SaveWord(fileLocal string) {
  106. timeNow := time.Now().Format("2006-01-02-15.04.05")
  107. wd.doc.SaveToFile(fmt.Sprintf("%s/ok_%s.docx", fileLocal, timeNow))
  108. }
  109. func addRow(table *document.Table, col1, col2 string) {
  110. row := table.AddRow()
  111. rowCell := row.AddCell()
  112. rowCell.Properties().SetWidthPercent(60)
  113. rowCell.AddParagraph().AddRun().AddText(col1)
  114. row.AddCell().AddParagraph().AddRun().AddText(col2)
  115. }
  116. // 插入合并后的图片到表格内
  117. func (wd *WordInfo) InsertSingleImage(table *document.Table, data []byte) {
  118. row := table.AddRow()
  119. row.AddCell().AddParagraph().AddRun().AddText("测试结果截图")
  120. ///row = table.AddRow()
  121. ParagraphTemp := row.AddCell().AddParagraph()
  122. img1, err := common.ImageFromBytes(data)
  123. if err != nil {
  124. log.Fatalf("unable to create image: %s", err)
  125. }
  126. // 创建一个图片对象
  127. image, err := wd.doc.AddImage(img1) // 替换为实际图片路径
  128. if err != nil {
  129. log.Fatalf("unable to doc.AddImage: %s", err)
  130. }
  131. inlineDrawing, err := ParagraphTemp.AddRun().AddDrawingInline(image)
  132. if err != nil {
  133. global.Logs.Fatalf("unable to doc.AddImage: %s", err)
  134. }
  135. inlineDrawing.SetSize(100, 100) // 设置图像大小为 100x100 像素
  136. }
  137. func (wd *WordInfo) InsertImages(table *document.Table, data [][]byte) {
  138. row := table.AddRow()
  139. ParagraphTemp := row.AddCell().AddParagraph()
  140. for _, v := range data {
  141. img1, err := common.ImageFromBytes(v)
  142. if err != nil {
  143. log.Fatalf("unable to create image: %s", err)
  144. }
  145. // 创建一个图片对象
  146. image, err := wd.doc.AddImage(img1) // 替换为实际图片路径
  147. if err != nil {
  148. log.Fatalf("unable to doc.AddImage: %s", err)
  149. }
  150. _, err = ParagraphTemp.AddRun().AddDrawingInline(image)
  151. if err != nil {
  152. global.Logs.Fatalf("unable to doc.AddImage: %s", err)
  153. }
  154. //inlineDrawing.SetSize(100, 100) // 设置图像大小为 100x100 像素
  155. }
  156. }