word.go 6.7 KB

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