export_exel.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package xvdoc
  2. import (
  3. "bytes"
  4. "fmt"
  5. _ "image/gif"
  6. _ "image/jpeg"
  7. _ "image/png"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. func exelStyleFromMap(m JSONMap) {
  14. }
  15. func exportXLSX(mList []Matrix) (res []byte, err error) {
  16. if len(mList) == 0 {
  17. return nil, fmt.Errorf("XVDoc export xlsx error :: expexcted not empty matrix list")
  18. }
  19. // create excelize file and setup sheet name
  20. f := excelize.NewFile()
  21. for index, matrix := range mList {
  22. sName := matrix.Name
  23. if len(sName) == 0 {
  24. sName = fmt.Sprintf("Sheet%v", index)
  25. }
  26. if index == 0 {
  27. f.SetSheetName("Sheet1", sName)
  28. } else {
  29. f.NewSheet(sName)
  30. }
  31. ////////////////
  32. // setup global styles
  33. var sID int
  34. xStyles := make(map[string]int)
  35. for name, style := range matrix.Styles {
  36. log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", name, style)
  37. var styleRes JSONMap
  38. if styleRes, err = style.Result(matrix.Styles, name); err != nil {
  39. return
  40. }
  41. //fmt.Println("!!!!!!!!!!!!!!", name, string(styleRes.JSON()))
  42. /*
  43. if sID, err = f.NewStyle(string(styleRes.JSON())); err != nil {
  44. return
  45. }
  46. */
  47. if sID, err = f.NewStyle(styleRes.ToStyle()); err != nil {
  48. return
  49. }
  50. xStyles[name] = sID
  51. }
  52. ////////////////
  53. // setup cells values
  54. var coords, cellName string
  55. for rIndex, row := range matrix.Cells {
  56. for cIndex, cell := range row {
  57. if coords, err = excelize.CoordinatesToCellName(cIndex+1, rIndex+1); err == nil {
  58. if cell != nil {
  59. if cell.Type() == "formula" {
  60. formula := cell.(*CellFormula)
  61. args := make([]string, len(formula.args))
  62. for i, arg := range formula.args {
  63. if arg.aType != argTypeValue {
  64. x, y := arg.Coords(cIndex, rIndex)
  65. if cellName, err = excelize.CoordinatesToCellName(int(x)+1, int(y)+1); err != nil {
  66. return
  67. } else {
  68. args[i] = cellName
  69. }
  70. } else {
  71. args[i] = fmt.Sprint(arg.Value())
  72. }
  73. }
  74. if err = f.SetCellFormula(sName, coords, formula.ReplaceArgs(args)); err != nil {
  75. return
  76. }
  77. } else {
  78. f.SetCellValue(sName, coords, cell.Val())
  79. }
  80. // setup cell styles
  81. if styles := cell.Styles(); len(styles) > 0 {
  82. //fmt.Println("&&&&", styles)
  83. for _, style := range styles {
  84. if sID, check := xStyles[style]; check {
  85. if err = f.SetCellStyle(sName, coords, coords, sID); err != nil {
  86. return
  87. }
  88. }
  89. }
  90. }
  91. }
  92. } else {
  93. return nil, err
  94. }
  95. }
  96. if height, check := matrix.RowsHeight[rIndex]; check {
  97. f.SetRowHeight(sName, rIndex+1, height)
  98. }
  99. }
  100. var c, dc string
  101. // setup rect styles
  102. for _, rect := range matrix.StyleRects {
  103. if c, err = excelize.CoordinatesToCellName(rect.x+1, rect.y+1); err != nil {
  104. return
  105. }
  106. if dc, err = excelize.CoordinatesToCellName(rect.dx+1, rect.dy+1); err != nil {
  107. return
  108. }
  109. for _, style := range rect.styles {
  110. if sID, check := xStyles[style]; check {
  111. if err = f.SetCellStyle(sName, c, dc, sID); err != nil {
  112. return
  113. }
  114. }
  115. }
  116. }
  117. ///////////////////////////////////
  118. // setup columns width
  119. for _, cWidth := range matrix.ColumnsWidth {
  120. if c, err = excelize.ColumnNumberToName(cWidth.x + 1); err != nil {
  121. return
  122. }
  123. if dc, err = excelize.ColumnNumberToName(cWidth.dx + 1); err != nil {
  124. return
  125. }
  126. if err = f.SetColWidth(sName, c, dc, cWidth.width); err != nil {
  127. return
  128. }
  129. }
  130. // setup merges
  131. for _, merge := range matrix.Merges {
  132. if c, err = excelize.CoordinatesToCellName(merge.x+1, merge.y+1); err != nil {
  133. return
  134. }
  135. if dc, err = excelize.CoordinatesToCellName(merge.dx+1, merge.dy+1); err != nil {
  136. return
  137. }
  138. if err = f.MergeCell(sName, c, dc); err != nil {
  139. return
  140. }
  141. }
  142. for _, dropdown := range matrix.Dropdowns {
  143. if c, err = excelize.CoordinatesToCellName(dropdown.x+1, dropdown.y+1); err != nil {
  144. return
  145. }
  146. if dc, err = excelize.CoordinatesToCellName(dropdown.dx+1, dropdown.dy+1); err != nil {
  147. return
  148. }
  149. r := excelize.NewDataValidation(true)
  150. r.Sqref = "A5:A7"
  151. r.Sqref = fmt.Sprintf("%v:%v", c, dc)
  152. r.SetDropList(dropdown.values)
  153. f.AddDataValidation(sName, r)
  154. }
  155. ///////////////////////////////////
  156. // setup images
  157. for _, image := range matrix.Images {
  158. if c, err = excelize.CoordinatesToCellName(image.x+1, image.y+1); err != nil {
  159. return
  160. }
  161. options := &excelize.GraphicOptions{}
  162. if image.format != nil {
  163. options = image.format.ToGraphicOptions()
  164. }
  165. log.Println(image.ext)
  166. if image.ext != "" {
  167. var fl *os.File
  168. if fl, err = os.Open(image.path); err != nil {
  169. return
  170. }
  171. defer fl.Close()
  172. var src []byte
  173. if src, err = ioutil.ReadFile(image.path); err != nil {
  174. return
  175. }
  176. log.Println("!!!!!!!!!!!!!!!!!!!!!!!!!", options)
  177. img := &excelize.Picture{
  178. Extension: image.ext,
  179. File: src,
  180. Format: options,
  181. }
  182. if err = f.AddPictureFromBytes(sName, c, img); err != nil {
  183. return
  184. }
  185. } else {
  186. if err = f.AddPicture(sName, c, image.path, options); err != nil {
  187. return
  188. }
  189. }
  190. }
  191. ///////////////////////////////////
  192. }
  193. //r := excelize.NewDataValidation(true)
  194. //r.Sqref = "A5:A7"
  195. //r.SetDropList([]string{"10", "30", "50"})
  196. //f.AddDataValidation("xv-test", r)
  197. // setup result data
  198. var buf bytes.Buffer
  199. f.Write(&buf)
  200. return buf.Bytes(), nil
  201. }