123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- package produce
- import (
- "fmt"
- "log"
- "strings"
- "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 testtoWord() {
-
- doc := document.New()
- para := doc.AddParagraph()
- run := para.AddRun()
- run.AddText("这里是段落文字信息")
-
- run.AddText("这里第一行段落文字信息\r这里是第二行段落文字信息")
-
- para.SetStyle("Title")
- para.SetStyle("Heading1")
- para.Properties().SetFirstLineIndent(0.5 * measurement.Inch)
- para.Properties().AddSection(wml.ST_SectionMarkNextPage)
-
- 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)
-
- 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()
-
-
-
- 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, tableUpperPart TableUpperPart, SingleImage []byte) {
-
-
-
-
- table := wd.doc.AddTable()
-
- table.Properties().SetWidthAuto()
-
- 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()
- rowCellLeft := row.AddCell()
- rowCellLeft.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
- rowCellLeft.Properties().SetWidthPercent(25)
- rowCellLeft.AddParagraph().AddRun().AddText(col1)
- rowCellRight := row.AddCell()
- if col1 == "用例名称" {
- rowCellLeft.Properties().SetShading(wml.ST_ShdSolid, color.RGB(128, 128, 128), color.RGB(128, 128, 128))
- rowCellRight.Properties().SetShading(wml.ST_ShdSolid, color.RGB(128, 128, 128), color.RGB(128, 128, 128))
- }
-
-
- startIndex := 0
- for {
- index := strings.Index(col2[startIndex:], ";")
- if index == -1 {
- break
- }
-
- rowCellRight.AddParagraph().AddRun().AddText(col2[startIndex : startIndex+index+1])
-
- startIndex += index + 1
- }
-
- if startIndex < len(col2) {
- rowCellRight.AddParagraph().AddRun().AddText(col2[startIndex:])
- }
- }
- func (wd *WordInfo) InsertSingleImage(table *document.Table, data []byte) {
- row := table.AddRow()
- rowCellLeft := row.AddCell()
-
- rowCellLeft.AddParagraph().AddRun().AddText("测试结果截图")
- rowCellLeft.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)
-
- 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(300, 400)
- }
- 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)
- }
-
- }
- }
|