lucky_sheet.go 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage/excel/request"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "os"
  9. "reflect"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/shopspring/decimal"
  15. "github.com/tealeg/xlsx"
  16. "github.com/xuri/excelize/v2"
  17. )
  18. type LuckySheetDataBak struct {
  19. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  20. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  21. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  22. Config struct {
  23. BorderInfo []struct {
  24. BorderType string `json:"borderType" description:""`
  25. Color string `json:"color" description:""`
  26. Range []struct {
  27. Column []int64 `json:"column" description:""`
  28. Row []int64 `json:"row" description:""`
  29. } `json:"range" description:""`
  30. RangeType string `json:"rangeType" description:""`
  31. Style string `json:"style" description:""`
  32. Value struct {
  33. B struct {
  34. Color string `json:"color" description:""`
  35. Style string `json:"style" description:""`
  36. } `json:"b" description:""`
  37. ColIndex int64 `json:"col_index" description:""`
  38. L struct {
  39. Color string `json:"color" description:""`
  40. Style string `json:"style" description:""`
  41. } `json:"l" description:""`
  42. R struct {
  43. Color string `json:"color" description:""`
  44. Style string `json:"style" description:""`
  45. } `json:"r" description:""`
  46. RowIndex int64 `json:"row_index" description:""`
  47. T struct {
  48. Color string `json:"color" description:""`
  49. Style string `json:"style" description:""`
  50. } `json:"t" description:""`
  51. } `json:"value" description:"" description:""`
  52. } `json:"borderInfo" description:""`
  53. Colhidden struct{} `json:"colhidden" description:""`
  54. Columnlen map[string]float64 `json:"columnlen" description:""`
  55. CustomHeight struct {
  56. Zero int64 `json:"0"`
  57. } `json:"customHeight" description:""`
  58. CustomWidth struct {
  59. Two int64 `json:"2" description:""`
  60. } `json:"customWidth" description:""`
  61. Merge struct{} `json:"merge" description:""`
  62. Rowlen map[string]float64 `json:"rowlen" description:""`
  63. } `json:"config" description:""`
  64. Data [][]struct {
  65. Ct struct {
  66. Fa string `json:"fa"`
  67. T string `json:"t"`
  68. } `json:"ct"`
  69. M string `json:"m"`
  70. V interface{} `json:"v"`
  71. } `json:"data" description:""`
  72. DataVerification struct{} `json:"dataVerification" description:""`
  73. Filter interface{} `json:"filter" description:""`
  74. FilterSelect interface{} `json:"filter_select" description:""`
  75. Hyperlink struct{} `json:"hyperlink" description:""`
  76. Images struct{} `json:"images" description:""`
  77. Index string `json:"index" description:""`
  78. JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
  79. LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
  80. LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
  81. LuckysheetSelectSave []struct {
  82. Column []int64 `json:"column" description:""`
  83. ColumnFocus int64 `json:"column_focus" description:""`
  84. Height int64 `json:"height" description:""`
  85. HeightMove int64 `json:"height_move" description:""`
  86. Left int64 `json:"left" description:""`
  87. LeftMove int64 `json:"left_move" description:""`
  88. Row []int64 `json:"row" description:""`
  89. RowFocus int64 `json:"row_focus" description:""`
  90. Top int64 `json:"top" description:""`
  91. TopMove int64 `json:"top_move" description:""`
  92. Width int64 `json:"width" description:""`
  93. WidthMove int64 `json:"width_move" description:""`
  94. } `json:"luckysheet_select_save" description:"" description:""`
  95. LuckysheetSelectionRange []struct {
  96. Column []int64 `json:"column" description:""`
  97. Row []int64 `json:"row" description:""`
  98. } `json:"luckysheet_selection_range" description:""`
  99. RhHeight float64 `json:"rh_height" description:""`
  100. ScrollLeft float64 `json:"scrollLeft" description:""`
  101. ScrollTop float64 `json:"scrollTop" description:""`
  102. Status int64 `json:"status" description:""`
  103. Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
  104. Visibledatarow []int64 `json:"visibledatarow" description:""`
  105. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  106. }
  107. // LuckySheetData sheet表格数据
  108. type LuckySheetData struct {
  109. Name string `json:"name" description:"工作表名称"`
  110. Index interface{} `json:"index" description:"工作表索引"`
  111. Row int `json:"row" description:"行数"`
  112. Column int `json:"column" description:"列数"`
  113. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  114. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  115. Config LuckySheetDataConfig `json:"config" description:""`
  116. //Index int `json:"index" description:"工作表索引"`
  117. RhHeight float64 `json:"rh_height" description:"工作表区域的高度"`
  118. ScrollLeft float64 `json:"scrollLeft" description:"左右滚动条位置"`
  119. ScrollTop float64 `json:"scrollTop" description:"上下滚动条位置"`
  120. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  121. Status interface{} `json:"status" description:"激活状态"`
  122. VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
  123. VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
  124. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  125. }
  126. // LuckySheetDataConfig sheet表单的配置
  127. type LuckySheetDataConfig struct {
  128. BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
  129. Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
  130. Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
  131. //CustomHeight struct {
  132. // Zero int64 `json:"0"`
  133. //} `json:"customHeight" description:""`
  134. //CustomWidth struct {
  135. // Two int64 `json:"2" description:""`
  136. //} `json:"customWidth" description:""`
  137. Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
  138. Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
  139. }
  140. // LuckySheetDataConfigMerge 合并单元格设置
  141. type LuckySheetDataConfigMerge struct {
  142. Row int `json:"r" description:"行数"`
  143. Column int `json:"c" description:"列数"`
  144. Rs int `json:"rs" description:"合并的行数"`
  145. Cs int `json:"cs" description:"合并的列数"`
  146. }
  147. // LuckySheetDataConfigBorderInfo 单元格边框信息
  148. type LuckySheetDataConfigBorderInfo struct {
  149. BorderType string `json:"borderType" description:"边框类型 border-left | border-right | border-top | border-bottom | border-all | border-outside | border-inside | border-horizontal | border-vertical | border-none"`
  150. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  151. Range []struct {
  152. Column []int64 `json:"column" description:"行"`
  153. Row []int64 `json:"row" description:"列"`
  154. } `json:"range" description:"选区范围 range: 行列信息数组"`
  155. RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
  156. Style string `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  157. Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
  158. }
  159. // LuckySheetDataConfigBorderInfoCellValue 单元格边框信息(范围类型为:单个单元格)
  160. type LuckySheetDataConfigBorderInfoCellValue struct {
  161. B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
  162. L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
  163. R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
  164. T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
  165. ColIndex int64 `json:"col_index" description:"第几行"`
  166. RowIndex int64 `json:"row_index" description:"第几列"`
  167. }
  168. // LuckySheetDataConfigBorderInfoCell 单元格边框信息(cell类型)
  169. type LuckySheetDataConfigBorderInfoCell struct {
  170. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  171. Style int `description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  172. Sl interface{} `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  173. }
  174. // LuckySheetCellData 单个单元格数据
  175. type LuckySheetCellData struct {
  176. Col int64 `json:"c" description:"列"`
  177. Row int64 `json:"r" description:"行"`
  178. Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
  179. }
  180. // LuckySheetDataValue 单元格内值的数据
  181. type LuckySheetDataValue struct {
  182. CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
  183. Value interface{} `json:"v" description:"原始值"`
  184. Monitor string `json:"m" description:"显示值"`
  185. Background string `json:"bg" description:"背景色,实例值:#fff000"`
  186. FontFamily int `description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  187. FF interface{} `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  188. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  189. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  190. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  191. Fontsize int `description:"字体大小,14"`
  192. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  193. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  194. VerticalType int ` description:"垂直对齐, 0 中间、1 上、2下"`
  195. Fs interface{} `json:"fs" description:"字体大小,14"`
  196. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  197. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  198. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  199. //TextRotate string `json:"tr" description:"竖排文字, 3"`
  200. //RotateText string `json:"rt" description:"文字旋转角度, 介于0~180之间的整数,包含0和180"`
  201. TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
  202. Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  203. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  204. Function string `json:"f" description:"公式"`
  205. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  206. }
  207. // LuckySheetDataCellType 单元格值格式:文本、时间等
  208. type LuckySheetDataCellType struct {
  209. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  210. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  211. S []struct {
  212. FontFamily int `description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  213. FF interface{} `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  214. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  215. Fontsize int `description:"字体大小,14"`
  216. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  217. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  218. VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
  219. Fs interface{} `json:"fs" description:"字体大小,14"`
  220. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  221. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  222. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  223. Un int `json:"un" description:""`
  224. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  225. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  226. Value interface{} `json:"v" description:"原始值"`
  227. } `json:"s"`
  228. }
  229. // LuckySheetDataCellComment 批注
  230. type LuckySheetDataCellComment struct {
  231. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  232. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  233. Width int `json:"width" description:"批注框宽度"`
  234. Height int `json:"height" description:"批注框高度"`
  235. Value string `json:"value" description:"批注内容"`
  236. IsShow bool `json:"isshow" description:"是否显示批注"`
  237. }
  238. // GetLuckySheetData 获取LuckySheetData的结构体
  239. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  240. err = json.Unmarshal([]byte(jsonStr), &item)
  241. for k, v := range item.CellData {
  242. value := v.Value
  243. value.Fontsize = getIntValueByInterface(value.Fs)
  244. value.CancelLine = getIntValueByInterface(value.Cl)
  245. value.HorizontalType = getIntValueByInterface(value.Ht)
  246. value.VerticalType = getIntValueByInterface(value.Vt)
  247. value.FontFamily = getIntValueByInterface(value.FF)
  248. value.TextBeak = getIntValueByInterface(value.Tb)
  249. if len(value.CellType.S) > 0 {
  250. for kk, vv := range value.CellType.S {
  251. vv.Fontsize = getIntValueByInterface(vv.Fs)
  252. vv.CancelLine = getIntValueByInterface(vv.Cl)
  253. vv.HorizontalType = getIntValueByInterface(vv.Ht)
  254. vv.VerticalType = getIntValueByInterface(vv.Vt)
  255. vv.FontFamily = getIntValueByInterface(vv.FF)
  256. value.CellType.S[kk] = vv
  257. }
  258. }
  259. item.CellData[k].Value = value
  260. }
  261. if len(item.Config.BorderInfo) > 0 {
  262. for k, v := range item.Config.BorderInfo {
  263. v.Value.T.Style = getIntValueByInterface(v.Value.T.Style)
  264. v.Value.B.Style = getIntValueByInterface(v.Value.B.Style)
  265. v.Value.L.Style = getIntValueByInterface(v.Value.L.Style)
  266. v.Value.R.Style = getIntValueByInterface(v.Value.R.Style)
  267. item.Config.BorderInfo[k] = v
  268. }
  269. }
  270. return
  271. }
  272. func getIntValueByInterface(valInterface interface{}) (val int) {
  273. if valInterface == nil {
  274. return
  275. }
  276. switch reflect.TypeOf(valInterface).Kind() {
  277. case reflect.String:
  278. tmpValue := reflect.ValueOf(valInterface).String()
  279. tmpValInt, err := strconv.Atoi(tmpValue)
  280. if err != nil {
  281. val = 0
  282. } else {
  283. val = tmpValInt
  284. }
  285. case reflect.Int, reflect.Int32, reflect.Int64:
  286. tmpValue := reflect.ValueOf(valInterface).Int()
  287. val = int(tmpValue)
  288. }
  289. return
  290. }
  291. // TableData 表格数据
  292. type TableData struct {
  293. TableDataList [][]LuckySheetDataValue
  294. RowWidthList []float64
  295. RowHeightList []float64
  296. RemoveTopRow int `description:"移除表格上方的行数"`
  297. RemoveBottomRow int `description:"移除表格下方的行数"`
  298. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  299. RemoveRightColumn int `description:"移除表格右侧的列数"`
  300. MergeList []TableDataMerge `description:"合并数据列"`
  301. }
  302. // TableDataMerge 表格数据合并单元格配置
  303. type TableDataMerge struct {
  304. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  305. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  306. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  307. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  308. }
  309. // ToExcel 通过 TableData生成excel表格
  310. func (tableData TableData) ToExcel() (downloadFilePath string, err error) {
  311. //dir, err := os.Executable()
  312. //exPath := filepath.Dir(dir)
  313. //downloadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  314. downloadFilePath, err = getDownloadPath()
  315. if err != nil {
  316. return
  317. }
  318. xlsxFile := xlsx.NewFile()
  319. if err != nil {
  320. return
  321. }
  322. // 将单个sheet的数据写入到excel
  323. err = tableData.WriteExcelSheetData(xlsxFile, "sheet1")
  324. if err != nil {
  325. return
  326. }
  327. //return
  328. err = xlsxFile.Save(downloadFilePath)
  329. if err != nil {
  330. return
  331. }
  332. //randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  333. //downloadFileName := "即将到期客户数据_" + randStr + ".xlsx"
  334. //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  335. return
  336. }
  337. // WriteExcelSheetData 通过 TableData生成excel表格数据
  338. func (tableData TableData) WriteExcelSheetData(xlsxFile *xlsx.File, sheetName string) (err error) {
  339. style := xlsx.NewStyle()
  340. alignment := xlsx.Alignment{
  341. Horizontal: "center",
  342. Vertical: "center",
  343. WrapText: true,
  344. }
  345. style.Alignment = alignment
  346. style.ApplyAlignment = true
  347. sheet, err := xlsxFile.AddSheet(sheetName)
  348. if err != nil {
  349. return
  350. }
  351. for k, v := range tableData.RowWidthList {
  352. err = sheet.SetColWidth(k, k, v/10)
  353. if err != nil {
  354. return
  355. }
  356. }
  357. // 单元格高度配置列表
  358. lenRowHeight := len(tableData.RowHeightList)
  359. for index, v := range tableData.TableDataList {
  360. tmpRow := sheet.AddRow()
  361. //设置单元格高度
  362. if index < lenRowHeight && tableData.RowHeightList[index] > 0 {
  363. tmpRow.SetHeight(tableData.RowHeightList[index] / 2)
  364. }
  365. for _, cellInfo := range v {
  366. tmpStyle := new(xlsx.Style)
  367. //xlsx.Style{
  368. // Fill: xlsx.Fill{},
  369. // ApplyBorder: false,
  370. // ApplyFill: false,
  371. // ApplyFont: false,
  372. // NamedStyleIndex: nil,
  373. //}
  374. //fill := *NewFill("solid", "FF000000", "00FF0000")
  375. defaultFill := xlsx.DefaultFill()
  376. if cellInfo.Background != `` {
  377. defaultFill.PatternType = "solid"
  378. backgroundColor := cellInfo.Background
  379. // 这么做是为了避免传入的是RGB的格式(rgb(255, 255, 255))
  380. backgroundColor = getColor(backgroundColor)
  381. defaultFill.BgColor = strings.TrimPrefix(backgroundColor, "#")
  382. defaultFill.FgColor = strings.TrimPrefix(backgroundColor, "#")
  383. }
  384. tmpStyle.Fill = *defaultFill
  385. // 获取表格字体配置
  386. tmpStyle.Font = getExcelFontConf(cellInfo)
  387. //获取表格单元格排列配置
  388. tmpStyle.ApplyAlignment = true
  389. tmpStyle.Alignment = getExcelAlignmentConf(cellInfo)
  390. //边框配置
  391. tmpStyle.Border = xlsx.Border{
  392. Left: "thin",
  393. LeftColor: "000000",
  394. Right: "thin",
  395. RightColor: "000000",
  396. Top: "thin",
  397. TopColor: "000000",
  398. Bottom: "thin",
  399. BottomColor: "000000",
  400. }
  401. //tmpStyle.ApplyAlignment = true
  402. //tmpStyle.Alignment.WrapText = true
  403. tmpRow := tmpRow.AddCell()
  404. tmpRow.SetStyle(tmpStyle)
  405. valueStr := cellInfo.Monitor
  406. if valueStr == `` {
  407. //valueStr = fmt.Sprint(cellInfo.Value)
  408. if valueStr == `` && cellInfo.CellType.S != nil {
  409. //不是设置在单元格上面,而是设置在文本上
  410. for _, cellS := range cellInfo.CellType.S {
  411. valueStr += fmt.Sprint(cellS.Value)
  412. }
  413. }
  414. }
  415. //tmpRow.SetString(valueStr)
  416. switch cellInfo.CellType.Fa {
  417. case "General":
  418. if cellInfo.CellType.S != nil {
  419. tmpRow.SetString(valueStr)
  420. } else {
  421. tmpRow.SetValue(cellInfo.Value)
  422. }
  423. case "@":
  424. tmpRow.SetString(valueStr)
  425. default:
  426. tmpRow.SetString(valueStr)
  427. }
  428. if cellInfo.Function != `` {
  429. //xlsxFile.
  430. //xlsxFile.SetCellFormula
  431. tmpRow.SetFormula(cellInfo.Function)
  432. }
  433. //if cellInfo.Function != `` {
  434. // tmpRow.SetFormula(cellInfo.Function)
  435. //}
  436. }
  437. }
  438. for _, v := range tableData.MergeList {
  439. for k, cell := range sheet.Row(v.StartRowIndex).Cells {
  440. if v.StartColumnIndex == k {
  441. cell.Merge(v.MergeColumnNum, v.MergeRowNum)
  442. }
  443. }
  444. }
  445. return
  446. }
  447. // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
  448. func (item *LuckySheetData) GetTableDataByLuckySheetDataStr(isRemoveBlankCell bool) (selfTableData TableData, err error) {
  449. luckySheetCellDataList := item.CellData
  450. // 表格数据
  451. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  452. // 最大行,最大列
  453. var maxRow, maxCol int64
  454. for _, v := range luckySheetCellDataList {
  455. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  456. if v.Row > maxRow { //最大行
  457. maxRow = v.Row
  458. }
  459. if v.Col > maxCol { //最大列
  460. maxCol = v.Col
  461. }
  462. var tmpRow map[int64]LuckySheetDataValue
  463. tmpRow, ok := tableDataMap[v.Row]
  464. if ok {
  465. tmpRow[v.Col] = v.Value
  466. } else {
  467. tmpRow = make(map[int64]LuckySheetDataValue)
  468. tmpRow[v.Col] = v.Value
  469. }
  470. tableDataMap[v.Row] = tmpRow
  471. }
  472. tableDataList := make([][]LuckySheetDataValue, 0)
  473. var i int64
  474. // 单元格宽度
  475. configColumnConf := item.Config.Columnlen
  476. rowWidthMap := make(map[int]float64)
  477. rowWidthList := make([]float64, 0) //
  478. // 单元格高度
  479. configRowConf := item.Config.Rowlen
  480. rowHeightList := make([]float64, 0) //
  481. for i = 0; i <= maxRow; i++ {
  482. //列
  483. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  484. // 每个单元格的高度
  485. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  486. if !ok {
  487. tmpHeight = 0
  488. }
  489. rowHeightList = append(rowHeightList, tmpHeight)
  490. tmpRowData, ok := tableDataMap[i]
  491. // 如果没有该行数据,那么就默认添加空行数据处理
  492. if !ok {
  493. tmpRowData = make(map[int64]LuckySheetDataValue)
  494. }
  495. var j int64
  496. for j = 0; j <= maxCol; j++ {
  497. tmpTableColData, ok := tmpRowData[j]
  498. if !ok {
  499. tmpTableColData = LuckySheetDataValue{}
  500. }
  501. //单元格显示的数据处理
  502. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  503. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  504. // 每个单元格的宽度
  505. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  506. if !ok {
  507. tmpWidth = 0
  508. }
  509. rowIndex := len(tmpTableColDataList) - 1
  510. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  511. rowWidthList = append(rowWidthList, tmpWidth)
  512. }
  513. rowWidthMap[rowIndex] = tmpWidth
  514. }
  515. tableDataList = append(tableDataList, tmpTableColDataList)
  516. }
  517. tableDataMergeList := make([]TableDataMerge, 0)
  518. tableRemoveNum := TableRemoveNum{}
  519. // 数据处理,移除上下左右空行空列
  520. if isRemoveBlankCell {
  521. tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList = handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
  522. }
  523. // 表格数据
  524. {
  525. // 移除空白单元格信息
  526. selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
  527. selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
  528. selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
  529. selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
  530. }
  531. selfTableData.TableDataList = tableDataList
  532. selfTableData.RowWidthList = rowWidthList
  533. selfTableData.RowHeightList = rowHeightList
  534. selfTableData.MergeList = tableDataMergeList
  535. return
  536. }
  537. // TableRemoveNum 上下左右移除的空行空列数量
  538. type TableRemoveNum struct {
  539. RemoveTopRow int `description:"移除表格上方的行数"`
  540. RemoveBottomRow int `description:"移除表格下方的行数"`
  541. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  542. RemoveRightColumn int `description:"移除表格右侧的列数"`
  543. }
  544. // handleTableDataList 表格数据处理(移除上下左右的空行空列)
  545. func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
  546. var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
  547. tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
  548. //总共多少行
  549. lenRow := len(tableDataList)
  550. //移除上下空行
  551. deleteRowIndexList := make([]int, 0)
  552. if lenRow > 0 {
  553. var flag = false
  554. // 移除上方空列
  555. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  556. isDelete := true
  557. for _, v := range tableDataList[rowIndex] {
  558. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  559. isDelete = false
  560. flag = true
  561. break
  562. }
  563. }
  564. if flag {
  565. break
  566. }
  567. if isDelete {
  568. removeTopRow++
  569. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  570. }
  571. }
  572. // 移除下方空行
  573. flag = false
  574. //尾部
  575. deleteBottomRowIndexList := make([]int, 0)
  576. // 数据第1行不处理
  577. for rowIndex := lenRow - 1; rowIndex > removeTopRow; rowIndex-- {
  578. isDelete := true
  579. for _, v := range tableDataList[rowIndex] {
  580. if v.Monitor != `` {
  581. isDelete = false
  582. flag = true
  583. break
  584. }
  585. }
  586. if flag {
  587. break
  588. }
  589. if isDelete {
  590. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  591. removeBottomRow++
  592. }
  593. }
  594. if len(deleteBottomRowIndexList) > 0 {
  595. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  596. }
  597. lenDeleteRow := len(deleteRowIndexList)
  598. if lenDeleteRow > 0 {
  599. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  600. //表格数据
  601. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  602. //表格高度
  603. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  604. }
  605. }
  606. }
  607. //移除左右空列
  608. deleteColumnIndexList := make([]int, 0)
  609. if len(tableDataList) > 0 {
  610. var flag = false
  611. lenColumn := len(tableDataList[0])
  612. // 移除左边空列
  613. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  614. isDelete := true
  615. for _, v := range tableDataList {
  616. //如果一列都没有,说明是上面几行是空行,没有数据
  617. if len(v) <= 0 {
  618. continue
  619. }
  620. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  621. isDelete = false
  622. flag = true
  623. break
  624. }
  625. }
  626. if flag {
  627. break
  628. }
  629. if isDelete {
  630. removeLeftColumn++
  631. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  632. }
  633. }
  634. // 移除右方空列
  635. flag = false
  636. //右边
  637. deleteTailColumnIndexList := make([]int, 0)
  638. // 数据第1列不处理
  639. for columnIndex := lenColumn - 1; columnIndex > removeLeftColumn; columnIndex-- {
  640. isDelete := true
  641. for _, v := range tableDataList {
  642. //如果一列都没有,说明是上面几行是空行,没有数据
  643. if len(v) <= 0 {
  644. continue
  645. }
  646. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  647. isDelete = false
  648. flag = true
  649. break
  650. }
  651. }
  652. if flag {
  653. break
  654. }
  655. if isDelete {
  656. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  657. removeRightColumn++
  658. }
  659. }
  660. if len(deleteTailColumnIndexList) > 0 {
  661. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  662. }
  663. lenDeleteColumn := len(deleteColumnIndexList)
  664. if lenDeleteColumn > 0 {
  665. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  666. //表格数据
  667. for k, v := range tableDataList {
  668. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  669. }
  670. //表格宽度
  671. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  672. }
  673. }
  674. }
  675. //处理合并单元格
  676. for _, v := range luckySheetDataConfigMergeList {
  677. indexRow := v.Row - removeTopRow
  678. indexColumn := v.Column - removeLeftColumn
  679. tableDataMerge := TableDataMerge{
  680. StartRowIndex: indexRow,
  681. StartColumnIndex: indexColumn,
  682. MergeRowNum: v.Rs - 1,
  683. MergeColumnNum: v.Cs - 1,
  684. }
  685. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  686. }
  687. return tableDataList, TableRemoveNum{
  688. RemoveTopRow: removeTopRow,
  689. RemoveBottomRow: removeBottomRow,
  690. RemoveLeftColumn: removeLeftColumn,
  691. RemoveRightColumn: removeRightColumn,
  692. }, rowHeightList, rowWidthList, tableDataMergeList
  693. }
  694. // ToExcel 通过 luckySheetData生成excel表格
  695. func (item *LuckySheetData) ToExcel() (downloadFilePath string, err error) {
  696. tableData, err := item.GetTableDataByLuckySheetDataStr(true)
  697. if err != nil {
  698. return
  699. }
  700. downloadFilePath, err = tableData.ToExcel()
  701. return
  702. }
  703. // getDownloadPath 获取下载地址
  704. func getDownloadPath() (fpath string, err error) {
  705. dateDir := time.Now().Format("20060102")
  706. uploadDir := "static/xls/" + dateDir
  707. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  708. if err != nil {
  709. return
  710. }
  711. randStr := utils.GetRandStringNoSpecialChar(28)
  712. fileName := randStr + ".xlsx"
  713. fpath = uploadDir + "/" + fileName
  714. return
  715. }
  716. // getExcelFontConf 获取表格字体配置
  717. func getExcelFontConf(cellInfo LuckySheetDataValue) xlsx.Font {
  718. fontSize := 10
  719. familyName := ``
  720. var isBold, isItalic, isUnderline bool
  721. // 字体
  722. {
  723. if cellInfo.Bold == 1 {
  724. isBold = true
  725. }
  726. if cellInfo.Italic == 1 {
  727. isItalic = true
  728. }
  729. if cellInfo.CancelLine == 1 {
  730. isUnderline = true
  731. }
  732. //if cellInfo.Fontsize != `` {
  733. // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
  734. // if convSize > 0 {
  735. // fontSize = convSize
  736. // }
  737. //}
  738. if cellInfo.Fontsize > 0 {
  739. fontSize = cellInfo.Fontsize
  740. }
  741. tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
  742. if !familyNameOk {
  743. tmpFamilyName = "宋体"
  744. }
  745. familyName = tmpFamilyName
  746. }
  747. return xlsx.Font{
  748. Size: fontSize,
  749. Name: familyName,
  750. //Family: v2.FontFamily,
  751. //Charset: 0,
  752. Color: strings.TrimPrefix(getColor(cellInfo.FontColor), "#"),
  753. Bold: isBold,
  754. Italic: isItalic,
  755. Underline: isUnderline,
  756. }
  757. }
  758. // getExcelAlignmentConf 获取表格单元格排列配置
  759. func getExcelAlignmentConf(cellInfo LuckySheetDataValue) xlsx.Alignment {
  760. //自动换行
  761. isWrapText := false
  762. if cellInfo.TextBeak == 2 {
  763. isWrapText = true
  764. }
  765. rotateText := 0
  766. //if cellInfo.RotateText != `` {
  767. // convSize, _ := strconv.Atoi(cellInfo.RotateText)
  768. // if convSize > 0 {
  769. // rotateText = convSize
  770. // }
  771. //}
  772. return xlsx.Alignment{
  773. Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
  774. //Indent: 0,
  775. //ShrinkToFit: false,
  776. TextRotation: rotateText,
  777. Vertical: LuckyVerticalMap[cellInfo.VerticalType],
  778. WrapText: isWrapText,
  779. }
  780. }
  781. // getExcelBorderConf 获取表格边框配置
  782. //func getExcelBorderConf(list []LuckySheetDataConfigBorderInfo) (v xlsx.Border) {
  783. // // 字体
  784. // for _, v := range list {
  785. // //v.BorderType
  786. // //v.Style
  787. //
  788. // if v.RangeType == "cell" {
  789. //
  790. // xlsx.Border{
  791. // Left: LuckyBorderMap[v.Value.L.Style],
  792. // LeftColor: strings.TrimPrefix(v.Value.L.Color, "#"),
  793. // Right: LuckyBorderMap[v.Value.R.Style],
  794. // RightColor: strings.TrimPrefix(v.Value.R.Color, "#"),
  795. // Top: LuckyBorderMap[v.Value.T.Style],
  796. // TopColor: strings.TrimPrefix(v.Value.T.Color, "#"),
  797. // Bottom: LuckyBorderMap[v.Value.B.Style],
  798. // BottomColor: strings.TrimPrefix(v.Value.B.Color, "#"),
  799. // }
  800. // } else {
  801. // xlsx.Border{
  802. // Left: LuckyBorderMap[v.Style],
  803. // LeftColor: strings.TrimPrefix(v.Color, "#"),
  804. // Right: LuckyBorderMap[v.Style],
  805. // RightColor: strings.TrimPrefix(v.Color, "#"),
  806. // Top: LuckyBorderMap[v.Style],
  807. // TopColor: strings.TrimPrefix(v.Color, "#"),
  808. // Bottom: LuckyBorderMap[v.Style],
  809. // BottomColor: strings.TrimPrefix(v.Color, "#"),
  810. // }
  811. // }
  812. // //v.Value.T.Style
  813. //
  814. // }
  815. //
  816. // return
  817. //}
  818. // LuckyFontFamilyMap 字体map
  819. var LuckyFontFamilyMap = map[int]string{
  820. 0: "Times New Roman",
  821. 1: "Arial",
  822. 2: "Tahoma",
  823. 3: "Verdana",
  824. 4: "微软雅黑",
  825. 5: "宋体", //宋体(Song)、
  826. 6: "黑体", // 黑体(ST Heiti)
  827. 7: "楷体", //楷体(ST Kaiti),
  828. 8: "仿宋", //仿宋(ST FangSong),
  829. 9: "新宋体", //新宋体(ST Song),
  830. 10: "华文新魏",
  831. 11: "华文行楷",
  832. 12: "华文隶书",
  833. }
  834. // LuckyBorderMap 边框map
  835. var LuckyBorderMap = map[string]string{
  836. "": "Thin",
  837. "1": "Thin",
  838. "2": "Hair",
  839. "3": "Dotted",
  840. "4": "Dashed",
  841. "5": "DashDot", //宋体(Song)、
  842. "6": "DashDotDot", // 黑体(ST Heiti)
  843. "7": "Double", //楷体(ST Kaiti),
  844. "8": "Medium", //仿宋(ST FangSong),
  845. "9": "MediumDashed", //新宋体(ST Song),
  846. "10": "MediumDashDot",
  847. "11": "MediumDashDotDot",
  848. "12": "SlantedDashDot",
  849. "13": "Thick",
  850. }
  851. // LuckyHorizontalMap 水平对齐map
  852. var LuckyHorizontalMap = map[int]string{
  853. //"": "general",
  854. 0: "center",
  855. 1: "left",
  856. 2: "right",
  857. }
  858. // LuckyVerticalMap 垂直对齐map
  859. var LuckyVerticalMap = map[int]string{
  860. //"": "center",
  861. 0: "center",
  862. 1: "top",
  863. //2: "bottom",
  864. }
  865. // GetColumnName Return the ColumnName
  866. func GetColumnName(rowNumber int, colNumber int) (colName string, err error) {
  867. if rowNumber < 1 || colNumber < 1 {
  868. err = errors.New("Row and Column Number can not be less than 1")
  869. return
  870. }
  871. dividend := colNumber
  872. var modulo int
  873. for dividend > 0 {
  874. modulo = (dividend - 1) % 26
  875. // Convert int to char
  876. colName = string(65+modulo) + colName
  877. dividend = (int)((dividend - modulo) / 26)
  878. }
  879. colName = colName + strconv.Itoa(rowNumber)
  880. return
  881. }
  882. // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
  883. func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
  884. luckySheetCellDataList := item.CellData
  885. // 表格数据
  886. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  887. // 最大行,最大列
  888. var maxRow, maxCol int64
  889. for _, v := range luckySheetCellDataList {
  890. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  891. if v.Row > maxRow { //最大行
  892. maxRow = v.Row
  893. }
  894. if v.Col > maxCol { //最大列
  895. maxCol = v.Col
  896. }
  897. var tmpRow map[int64]LuckySheetDataValue
  898. tmpRow, ok := tableDataMap[v.Row]
  899. if ok {
  900. tmpRow[v.Col] = v.Value
  901. } else {
  902. tmpRow = make(map[int64]LuckySheetDataValue)
  903. tmpRow[v.Col] = v.Value
  904. }
  905. tableDataMap[v.Row] = tmpRow
  906. }
  907. tableDataList := make([][]LuckySheetDataValue, 0)
  908. var i int64
  909. // 单元格宽度
  910. configColumnConf := item.Config.Columnlen
  911. rowWidthMap := make(map[int]float64)
  912. rowWidthList := make([]float64, 0) //
  913. // 单元格高度
  914. configRowConf := item.Config.Rowlen
  915. rowHeightList := make([]float64, 0) //
  916. for i = 0; i <= maxRow; i++ {
  917. //列
  918. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  919. // 每个单元格的高度
  920. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  921. if !ok {
  922. tmpHeight = 0
  923. }
  924. rowHeightList = append(rowHeightList, tmpHeight)
  925. tmpRowData, ok := tableDataMap[i]
  926. // 如果没有该行数据,那么就默认添加空行数据处理
  927. if !ok {
  928. tmpRowData = make(map[int64]LuckySheetDataValue)
  929. }
  930. var j int64
  931. for j = 0; j <= maxCol; j++ {
  932. tmpTableColData, ok := tmpRowData[j]
  933. if !ok {
  934. tmpTableColData = LuckySheetDataValue{}
  935. }
  936. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  937. // 每个单元格的宽度
  938. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  939. if !ok {
  940. tmpWidth = 0
  941. }
  942. rowIndex := len(tmpTableColDataList) - 1
  943. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  944. rowWidthList = append(rowWidthList, tmpWidth)
  945. }
  946. rowWidthMap[rowIndex] = tmpWidth
  947. }
  948. tableDataList = append(tableDataList, tmpTableColDataList)
  949. }
  950. //总共多少行
  951. lenRow := len(tableDataList)
  952. //移除上下空行
  953. deleteRowIndexList := make([]int, 0)
  954. if lenRow > 0 {
  955. var flag = false
  956. // 移除上方空列
  957. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  958. isDelete := true
  959. for _, v := range tableDataList[rowIndex] {
  960. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  961. isDelete = false
  962. flag = true
  963. break
  964. }
  965. }
  966. if flag {
  967. break
  968. }
  969. if isDelete {
  970. selfTableData.RemoveTopRow++
  971. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  972. }
  973. }
  974. // 移除下方空行
  975. flag = false
  976. //尾部
  977. deleteBottomRowIndexList := make([]int, 0)
  978. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  979. isDelete := true
  980. for _, v := range tableDataList[rowIndex] {
  981. if v.Monitor != `` {
  982. isDelete = false
  983. flag = true
  984. break
  985. }
  986. }
  987. if flag {
  988. break
  989. }
  990. if isDelete {
  991. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  992. selfTableData.RemoveBottomRow++
  993. }
  994. }
  995. if len(deleteBottomRowIndexList) > 0 {
  996. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  997. }
  998. lenDeleteRow := len(deleteRowIndexList)
  999. if lenDeleteRow > 0 {
  1000. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  1001. //表格数据
  1002. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  1003. //表格高度
  1004. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  1005. }
  1006. }
  1007. }
  1008. //移除左右空列
  1009. deleteColumnIndexList := make([]int, 0)
  1010. if len(tableDataList) > 0 {
  1011. var flag = false
  1012. lenColumn := len(tableDataList[0])
  1013. // 移除左边空列
  1014. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  1015. isDelete := true
  1016. for _, v := range tableDataList {
  1017. //如果一列都没有,说明是上面几行是空行,没有数据
  1018. if len(v) <= 0 {
  1019. continue
  1020. }
  1021. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  1022. isDelete = false
  1023. flag = true
  1024. break
  1025. }
  1026. }
  1027. if flag {
  1028. break
  1029. }
  1030. if isDelete {
  1031. selfTableData.RemoveLeftColumn++
  1032. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  1033. }
  1034. }
  1035. // 移除右方空列
  1036. flag = false
  1037. //右边
  1038. deleteTailColumnIndexList := make([]int, 0)
  1039. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  1040. isDelete := true
  1041. for _, v := range tableDataList {
  1042. //如果一列都没有,说明是上面几行是空行,没有数据
  1043. if len(v) <= 0 {
  1044. continue
  1045. }
  1046. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  1047. isDelete = false
  1048. flag = true
  1049. break
  1050. }
  1051. }
  1052. if flag {
  1053. break
  1054. }
  1055. if isDelete {
  1056. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  1057. selfTableData.RemoveRightColumn++
  1058. }
  1059. }
  1060. if len(deleteTailColumnIndexList) > 0 {
  1061. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  1062. }
  1063. lenDeleteColumn := len(deleteColumnIndexList)
  1064. if lenDeleteColumn > 0 {
  1065. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  1066. //表格数据
  1067. for k, v := range tableDataList {
  1068. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  1069. }
  1070. //表格宽度
  1071. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  1072. }
  1073. }
  1074. }
  1075. //处理合并单元格
  1076. tableDataMergeList := make([]TableDataMerge, 0)
  1077. for _, v := range item.Config.Merge {
  1078. indexRow := v.Row - selfTableData.RemoveTopRow
  1079. indexColumn := v.Column - selfTableData.RemoveLeftColumn
  1080. tableDataMerge := TableDataMerge{
  1081. StartRowIndex: indexRow,
  1082. StartColumnIndex: indexColumn,
  1083. MergeRowNum: v.Rs - 1,
  1084. MergeColumnNum: v.Cs - 1,
  1085. }
  1086. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  1087. }
  1088. // 表格数据
  1089. selfTableData.TableDataList = tableDataList
  1090. selfTableData.RowWidthList = rowWidthList
  1091. selfTableData.RowHeightList = rowHeightList
  1092. selfTableData.MergeList = tableDataMergeList
  1093. return
  1094. }
  1095. func (item *LuckySheetData) ToExcel2() (downloadFilePath string, err error) {
  1096. tableData, err := item.GetTableDataByLuckySheetDataStr(true)
  1097. downloadFilePath, err = getDownloadPath()
  1098. if err != nil {
  1099. return
  1100. }
  1101. f := excelize.NewFile()
  1102. // Create a new sheet.
  1103. sheetName := `Sheet1`
  1104. sheetIndex, err := f.NewSheet(sheetName)
  1105. if err != nil {
  1106. return
  1107. }
  1108. //设置列宽度
  1109. for k, v := range tableData.RowWidthList {
  1110. colName, tmpErr := excelize.ColumnNumberToName(k + 1)
  1111. if tmpErr != nil {
  1112. err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
  1113. err = tmpErr
  1114. return
  1115. }
  1116. err = f.SetColWidth(sheetName, colName, colName, v/10)
  1117. if err != nil {
  1118. err = errors.New("设置列宽度,ERR:" + tmpErr.Error())
  1119. return
  1120. }
  1121. }
  1122. // 写入数据
  1123. for index, v := range tableData.TableDataList {
  1124. //设置单元格高度
  1125. if tableData.RowHeightList[index] > 0 {
  1126. tmpErr := f.SetRowHeight(sheetName, index+1, tableData.RowHeightList[index]/2)
  1127. if tmpErr != nil {
  1128. err = errors.New("设置单元格高度失败,ERR:" + tmpErr.Error())
  1129. return
  1130. }
  1131. }
  1132. for colunmIndex, cellInfo := range v {
  1133. //f.NewStyle
  1134. defaultFill := excelize.Fill{
  1135. //Type: "solid",
  1136. Pattern: 1,
  1137. //Color: []string{cellInfo.Background},
  1138. Shading: 0,
  1139. }
  1140. if cellInfo.Background != `` {
  1141. defaultFill.Type = "solid"
  1142. defaultFill.Color = []string{cellInfo.Background}
  1143. }
  1144. cellStyle := &excelize.Style{
  1145. Border: []excelize.Border{
  1146. {Type: "left", Color: "000000", Style: 1},
  1147. {Type: "top", Color: "000000", Style: 1},
  1148. {Type: "bottom", Color: "000000", Style: 1},
  1149. {Type: "right", Color: "000000", Style: 1},
  1150. //{Type: "diagonalDown", Color: "A020F0", Style: 7},
  1151. //{Type: "diagonalUp", Color: "A020F0", Style: 8},
  1152. },
  1153. Fill: defaultFill,
  1154. Font: getExcelizeFontConf(cellInfo),
  1155. Alignment: getExcelizeAlignmentConf(cellInfo),
  1156. //Protection: nil,
  1157. //NumFmt: 0,
  1158. //DecimalPlaces: 0,
  1159. //CustomNumFmt: nil,
  1160. //Lang: "",
  1161. //NegRed: false,
  1162. }
  1163. cellStyleId, tmpErr := f.NewStyle(cellStyle)
  1164. if tmpErr != nil {
  1165. err = errors.New("new Style 失败,ERR:" + tmpErr.Error())
  1166. return
  1167. }
  1168. columnName, tmpErr := excelize.ColumnNumberToName(colunmIndex + 1)
  1169. if tmpErr != nil {
  1170. err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
  1171. return
  1172. }
  1173. cellName := columnName + strconv.Itoa(index+1)
  1174. tmpErr = f.SetCellStyle(sheetName, cellName, cellName, cellStyleId)
  1175. if tmpErr != nil {
  1176. err = errors.New("设置style失败,ERR:" + tmpErr.Error())
  1177. return
  1178. }
  1179. valueStr := cellInfo.Monitor
  1180. if valueStr == `` {
  1181. //valueStr = fmt.Sprint(cellInfo.Value)
  1182. if valueStr == `` && cellInfo.CellType.S != nil {
  1183. //不是设置在单元格上面,而是设置在文本上
  1184. for _, cellS := range cellInfo.CellType.S {
  1185. valueStr += fmt.Sprint(cellS.Value)
  1186. }
  1187. }
  1188. }
  1189. tmpErr = f.SetCellStr(sheetName, cellName, valueStr)
  1190. if tmpErr != nil {
  1191. err = errors.New("设置单元格数据失败,ERR:" + tmpErr.Error())
  1192. return
  1193. }
  1194. }
  1195. }
  1196. for _, v := range tableData.MergeList {
  1197. //开始列
  1198. startColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + 1)
  1199. if tmpErr != nil {
  1200. err = tmpErr
  1201. return
  1202. }
  1203. startCellName := startColumnName + strconv.Itoa(v.StartRowIndex+1)
  1204. //结束列
  1205. endColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + v.MergeColumnNum + 1)
  1206. if tmpErr != nil {
  1207. err = tmpErr
  1208. return
  1209. }
  1210. endCellName := endColumnName + strconv.Itoa(v.StartRowIndex+v.MergeRowNum+1)
  1211. err = f.MergeCell(sheetName, startCellName, endCellName)
  1212. if err != nil {
  1213. return
  1214. }
  1215. }
  1216. // Set active sheet of the workbook.
  1217. f.SetActiveSheet(sheetIndex)
  1218. // Save spreadsheet by the given path.
  1219. err = f.SaveAs(downloadFilePath)
  1220. return
  1221. }
  1222. // getExcelizeFontConf 获取表格字体配置
  1223. func getExcelizeFontConf(cellInfo LuckySheetDataValue) *excelize.Font {
  1224. fontSize := 10
  1225. familyName := ``
  1226. //single 单线 、double 双线
  1227. underlineStyle := ``
  1228. var isBold, isItalic, isUnderline bool
  1229. // 字体
  1230. {
  1231. if cellInfo.Bold == 1 {
  1232. isBold = true
  1233. }
  1234. if cellInfo.Italic == 1 {
  1235. isItalic = true
  1236. }
  1237. if cellInfo.CancelLine == 1 {
  1238. isUnderline = true
  1239. }
  1240. //if cellInfo.Fontsize != `` {
  1241. // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
  1242. // if convSize > 0 {
  1243. // fontSize = convSize
  1244. // }
  1245. //}
  1246. if cellInfo.Fontsize > 0 {
  1247. fontSize = cellInfo.Fontsize
  1248. }
  1249. tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
  1250. if !familyNameOk {
  1251. tmpFamilyName = "宋体"
  1252. }
  1253. familyName = tmpFamilyName
  1254. }
  1255. if isUnderline {
  1256. underlineStyle = "single"
  1257. }
  1258. //VertAlign取值:
  1259. //BASELINE、superscript、subscript
  1260. return &excelize.Font{
  1261. Bold: isBold,
  1262. Italic: isItalic,
  1263. Underline: underlineStyle,
  1264. Family: familyName,
  1265. Size: float64(fontSize),
  1266. Strike: isUnderline,
  1267. Color: cellInfo.FontColor,
  1268. VertAlign: "",
  1269. }
  1270. }
  1271. // getExcelizeAlignmentConf 获取表格单元格排列配置
  1272. func getExcelizeAlignmentConf(cellInfo LuckySheetDataValue) *excelize.Alignment {
  1273. //自动换行
  1274. isWrapText := false
  1275. if cellInfo.TextBeak == 2 {
  1276. isWrapText = true
  1277. }
  1278. rotateText := 0
  1279. //if cellInfo.RotateText != `` {
  1280. // convSize, _ := strconv.Atoi(cellInfo.RotateText)
  1281. // if convSize > 0 {
  1282. // rotateText = convSize
  1283. // }
  1284. //}
  1285. return &excelize.Alignment{
  1286. Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
  1287. //Indent: 0,
  1288. //JustifyLastLine: false,
  1289. //ReadingOrder: 0,
  1290. //RelativeIndent: 0,
  1291. //ShrinkToFit: false,
  1292. TextRotation: rotateText,
  1293. Vertical: LuckyVerticalMap[cellInfo.VerticalType],
  1294. WrapText: isWrapText,
  1295. }
  1296. }
  1297. // getColor 获取hex颜色
  1298. func getColor(bgStr string) string {
  1299. isRgb := strings.HasPrefix(bgStr, "rgb")
  1300. re := regexp.MustCompile(`\(([^)]+)\)`)
  1301. matches := re.FindStringSubmatch(bgStr)
  1302. if len(matches) != 2 {
  1303. return bgStr
  1304. }
  1305. if isRgb {
  1306. arr := strings.Split(matches[1], ",")
  1307. if len(arr) < 3 {
  1308. return bgStr
  1309. }
  1310. rgbArr := make([]int64, 0)
  1311. for i, v := range arr {
  1312. if i >= 3 {
  1313. continue
  1314. }
  1315. tmpInt, err := strconv.Atoi(utils.TrimStr(v))
  1316. if err != nil {
  1317. return bgStr
  1318. }
  1319. rgbArr = append(rgbArr, int64(tmpInt))
  1320. }
  1321. rgb := RGB{
  1322. rgbArr[0],
  1323. rgbArr[1],
  1324. rgbArr[2],
  1325. }
  1326. bgStr = "" + rgb.Rgb2Hex().Str
  1327. }
  1328. return bgStr
  1329. }
  1330. // GetTableDataByCustomData 通过自定义表格数据获取表格数据
  1331. func GetTableDataByCustomData(excelType int, data request.TableDataReq, lang string) (selfTableData TableData, err error) {
  1332. tableDataList := make([][]LuckySheetDataValue, 0)
  1333. mergeList := make([]TableDataMerge, 0)
  1334. // 指标数
  1335. lenEdb := len(data.Data)
  1336. if lenEdb <= 0 {
  1337. return
  1338. }
  1339. // 日期数
  1340. lenCol := len(data.Data[0].Data)
  1341. if lenCol <= 0 {
  1342. return
  1343. }
  1344. // 指标列:1;指标行:2
  1345. if excelType == 1 {
  1346. mergeList = append(mergeList, TableDataMerge{
  1347. StartRowIndex: 0,
  1348. StartColumnIndex: 0,
  1349. MergeRowNum: 1,
  1350. MergeColumnNum: 0,
  1351. })
  1352. // 第一行
  1353. {
  1354. firstCol := make([]LuckySheetDataValue, 0)
  1355. dateStr := "日期"
  1356. switch lang {
  1357. case utils.EnLangVersion:
  1358. dateStr = "Date"
  1359. }
  1360. firstCol = append(firstCol, LuckySheetDataValue{
  1361. Value: dateStr,
  1362. Monitor: dateStr,
  1363. MergeCell: LuckySheetDataConfigMerge{
  1364. Row: 0, //行数
  1365. Column: 0, //列数
  1366. Rs: 2, //合并的行数
  1367. Cs: 1, //合并的列数
  1368. },
  1369. })
  1370. for _, v := range data.Data {
  1371. edbName := v.EdbName
  1372. if v.EdbAliasName != `` {
  1373. edbName = v.EdbAliasName
  1374. } else {
  1375. switch lang {
  1376. case utils.EnLangVersion:
  1377. edbName = v.EdbNameEn
  1378. }
  1379. }
  1380. firstCol = append(firstCol, LuckySheetDataValue{
  1381. Value: edbName,
  1382. Monitor: edbName,
  1383. MergeCell: LuckySheetDataConfigMerge{},
  1384. })
  1385. }
  1386. tableDataList = append(tableDataList, firstCol)
  1387. }
  1388. // 第二行
  1389. {
  1390. secondCol := make([]LuckySheetDataValue, 0)
  1391. secondCol = append(secondCol, LuckySheetDataValue{})
  1392. for _, v := range data.Data {
  1393. var name string
  1394. switch lang {
  1395. case utils.EnLangVersion:
  1396. name = v.UnitEn + " / " + utils.GetFrequencyEn(v.Frequency)
  1397. default:
  1398. name = v.Unit + " / " + v.Frequency
  1399. }
  1400. secondCol = append(secondCol, LuckySheetDataValue{
  1401. Value: name,
  1402. Monitor: name,
  1403. MergeCell: LuckySheetDataConfigMerge{},
  1404. })
  1405. }
  1406. tableDataList = append(tableDataList, secondCol)
  1407. }
  1408. // 开始数据了
  1409. {
  1410. for i := 0; i < lenCol; i++ {
  1411. dataCol := make([]LuckySheetDataValue, 0)
  1412. // 日期
  1413. dataCol = append(dataCol, LuckySheetDataValue{
  1414. Value: data.Data[0].Data[i].DataTime,
  1415. Monitor: data.Data[0].Data[i].DataTime,
  1416. MergeCell: LuckySheetDataConfigMerge{},
  1417. })
  1418. // 数据值
  1419. for _, v := range data.Data {
  1420. background := ``
  1421. if v.Data[i].DataType == 5 {
  1422. background = "#ffefdd"
  1423. }
  1424. dataCol = append(dataCol, LuckySheetDataValue{
  1425. Value: v.Data[i].Value,
  1426. Monitor: v.Data[i].ShowValue,
  1427. MergeCell: LuckySheetDataConfigMerge{},
  1428. Background: background,
  1429. })
  1430. }
  1431. tableDataList = append(tableDataList, dataCol)
  1432. }
  1433. }
  1434. // 开始文本行了
  1435. {
  1436. for _, textColList := range data.TextRowData {
  1437. dataCol := make([]LuckySheetDataValue, 0)
  1438. for _, v := range textColList {
  1439. dataCol = append(dataCol, LuckySheetDataValue{
  1440. Value: v.Value,
  1441. Monitor: v.ShowValue,
  1442. MergeCell: LuckySheetDataConfigMerge{},
  1443. })
  1444. }
  1445. tableDataList = append(tableDataList, dataCol)
  1446. }
  1447. }
  1448. } else {
  1449. // 指标行
  1450. mergeList = append(mergeList, TableDataMerge{
  1451. StartRowIndex: 0,
  1452. StartColumnIndex: 0,
  1453. MergeRowNum: 0,
  1454. MergeColumnNum: 1,
  1455. })
  1456. // 第一行
  1457. {
  1458. firstCol := make([]LuckySheetDataValue, 0)
  1459. dateStr := "日期"
  1460. switch lang {
  1461. case utils.EnLangVersion:
  1462. dateStr = "Date"
  1463. }
  1464. firstCol = append(firstCol, LuckySheetDataValue{
  1465. Value: dateStr,
  1466. Monitor: dateStr,
  1467. MergeCell: LuckySheetDataConfigMerge{
  1468. Row: 0, //行数
  1469. Column: 0, //列数
  1470. Rs: 1, //合并的行数
  1471. Cs: 2, //合并的列数
  1472. },
  1473. })
  1474. firstCol = append(firstCol, LuckySheetDataValue{})
  1475. // 日期列
  1476. for _, v := range data.Data[0].Data {
  1477. firstCol = append(firstCol, LuckySheetDataValue{
  1478. Value: v.DataTime,
  1479. Monitor: v.DataTime,
  1480. MergeCell: LuckySheetDataConfigMerge{},
  1481. })
  1482. }
  1483. // 文本列
  1484. for _, textColList := range data.TextRowData {
  1485. firstCol = append(firstCol, LuckySheetDataValue{
  1486. Value: textColList[0].Value,
  1487. Monitor: textColList[0].ShowValue,
  1488. MergeCell: LuckySheetDataConfigMerge{},
  1489. })
  1490. }
  1491. tableDataList = append(tableDataList, firstCol)
  1492. }
  1493. // 日期列+文本列+两列表头(日期这个文案表头)
  1494. //colLen := lenCol+2+len(data.TextRowData)
  1495. for i := 0; i < lenEdb; i++ {
  1496. dataCol := make([]LuckySheetDataValue, 0)
  1497. // 指标信息
  1498. tmpEdbInfo := data.Data[i]
  1499. // 指标名称
  1500. {
  1501. edbName := tmpEdbInfo.EdbName
  1502. if tmpEdbInfo.EdbAliasName != `` {
  1503. edbName = tmpEdbInfo.EdbAliasName
  1504. } else {
  1505. switch lang {
  1506. case utils.EnLangVersion:
  1507. edbName = tmpEdbInfo.EdbNameEn
  1508. }
  1509. }
  1510. dataCol = append(dataCol, LuckySheetDataValue{
  1511. Value: edbName,
  1512. Monitor: edbName,
  1513. MergeCell: LuckySheetDataConfigMerge{},
  1514. })
  1515. }
  1516. // 指标单位、频度
  1517. {
  1518. var name string
  1519. switch lang {
  1520. case utils.EnLangVersion:
  1521. name = tmpEdbInfo.UnitEn + " / " + utils.GetFrequencyEn(tmpEdbInfo.Frequency)
  1522. default:
  1523. name = tmpEdbInfo.Unit + " / " + tmpEdbInfo.Frequency
  1524. }
  1525. dataCol = append(dataCol, LuckySheetDataValue{
  1526. Value: name,
  1527. Monitor: name,
  1528. MergeCell: LuckySheetDataConfigMerge{},
  1529. })
  1530. }
  1531. // 指标数据列
  1532. for _, tmpData := range tmpEdbInfo.Data {
  1533. background := ``
  1534. if tmpData.DataType == 5 {
  1535. background = "#ffefdd"
  1536. }
  1537. dataCol = append(dataCol, LuckySheetDataValue{
  1538. Value: tmpData.Value,
  1539. Monitor: tmpData.ShowValue,
  1540. MergeCell: LuckySheetDataConfigMerge{},
  1541. Background: background,
  1542. })
  1543. }
  1544. // 文本列
  1545. for _, textColList := range data.TextRowData {
  1546. dataCol = append(dataCol, LuckySheetDataValue{
  1547. Value: textColList[i+1].Value,
  1548. Monitor: textColList[i+1].ShowValue,
  1549. MergeCell: LuckySheetDataConfigMerge{},
  1550. })
  1551. }
  1552. tableDataList = append(tableDataList, dataCol)
  1553. }
  1554. }
  1555. selfTableData.MergeList = mergeList
  1556. selfTableData.TableDataList = tableDataList
  1557. return
  1558. }
  1559. // GetTableDataByMixedTableData 通过混合表格数据获取表格数据
  1560. func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hideMerged bool) (selfTableData TableData, err error) {
  1561. tableDataList := make([][]LuckySheetDataValue, 0)
  1562. mergeList := make([]TableDataMerge, 0)
  1563. // 开始文本行了
  1564. {
  1565. for _, row := range config {
  1566. dataCol := make([]LuckySheetDataValue, 0)
  1567. for _, cell := range row {
  1568. tmp := LuckySheetDataValue{
  1569. Value: cell.Value,
  1570. Monitor: cell.ShowValue,
  1571. MergeCell: LuckySheetDataConfigMerge{},
  1572. }
  1573. if cell.DataType != request.CustomTextDT && tmp.Monitor == `` {
  1574. tmp.Monitor = "-"
  1575. cell.ShowFormatValue = "-"
  1576. }
  1577. // TODO: 混合表格/平衡表-合并单元格
  1578. // 前端需要隐藏被合并的单元格, 混合表格/平衡表通过这个字段判断, 不通过HandleTableCell方法隐藏
  1579. if cell.MerData != nil {
  1580. if hideMerged && cell.MerData.Type == "merged" {
  1581. continue
  1582. }
  1583. tmp.MergeCell.Rs = cell.MerData.Mer.Rowspan
  1584. tmp.MergeCell.Cs = cell.MerData.Mer.Colspan
  1585. tmp.MergeCell.Row = cell.MerData.Mer.Row
  1586. tmp.MergeCell.Column = cell.MerData.Mer.Col
  1587. }
  1588. if cell.ShowStyle != "" {
  1589. var styleConfig request.MixCellShowStyle
  1590. if err := json.Unmarshal([]byte(cell.ShowStyle), &styleConfig); err != nil {
  1591. utils.FileLog.Info("表格样式showStyle解析失败", err.Error())
  1592. }
  1593. if styleConfig.BackgroundColor != "" {
  1594. tmp.Background = styleConfig.BackgroundColor
  1595. }
  1596. if styleConfig.Color != "" {
  1597. tmp.FontColor = styleConfig.Color
  1598. }
  1599. switch styleConfig.Align {
  1600. case "center":
  1601. tmp.HorizontalType = 0
  1602. tmp.Ht = 0
  1603. case "left":
  1604. tmp.HorizontalType = 1
  1605. tmp.Ht = 1
  1606. case "right":
  1607. tmp.HorizontalType = 2
  1608. tmp.Ht = 2
  1609. }
  1610. showFormatValue := fmt.Sprintf("%v", cell.ShowFormatValue)
  1611. tmp.Monitor = showFormatValue
  1612. // 如果没有showValue, 则使用value
  1613. if cell.ShowValue == "" {
  1614. _, err := strconv.ParseFloat(cell.Value, 64)
  1615. if err == nil {
  1616. cell.ShowValue = cell.Value
  1617. }
  1618. }
  1619. _, err := strconv.ParseFloat(cell.Value, 64)
  1620. if err == nil {
  1621. hasPercent := false
  1622. if styleConfig.Nt == "percent" {
  1623. hasPercent = true
  1624. }
  1625. // 修复历史数据
  1626. if styleConfig.Pn != 0 {
  1627. if styleConfig.Decimal == nil {
  1628. styleConfig.Decimal = new(int)
  1629. }
  1630. *styleConfig.Decimal += styleConfig.Pn
  1631. if *styleConfig.Decimal < 0 {
  1632. *styleConfig.Decimal = 0
  1633. }
  1634. }
  1635. if styleConfig.Decimal != nil {
  1636. tmp.Monitor = utils.RoundNumber(cell.ShowValue, *styleConfig.Decimal, hasPercent)
  1637. } else {
  1638. if hasPercent {
  1639. numDecimal, _ := decimal.NewFromString(cell.ShowValue)
  1640. tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
  1641. tmp.Monitor = tmpStr + "%"
  1642. } else {
  1643. tmp.Monitor = cell.ShowValue
  1644. }
  1645. }
  1646. } else {
  1647. if cell.DataType == request.CustomTextDT {
  1648. tmp.Monitor = cell.Value
  1649. }
  1650. }
  1651. // switch styleConfig.Last {
  1652. // case "nt":
  1653. // // 先进行数字的百分比计算,然后保留小数点位数
  1654. // percent := tmpShowValue * 100
  1655. // if styleConfig.Decimal == nil {
  1656. // continue
  1657. // }
  1658. // factor := math.Pow(10, float64(*styleConfig.Decimal))
  1659. // rounded := math.Round(percent*factor) / factor
  1660. // tmp.Monitor = fmt.Sprintf("%g%%", rounded)
  1661. // case "decimal":
  1662. // // 先保留小数点位数,再进行百分比计算
  1663. // if styleConfig.Decimal == nil {
  1664. // continue
  1665. // }
  1666. // factor := math.Pow(10, float64(*styleConfig.Decimal))
  1667. // rounded := math.Round(tmpShowValue*factor) / factor
  1668. // if styleConfig.Nt == "percent" {
  1669. // percent := rounded * 100
  1670. // var precisionStr string
  1671. // if *styleConfig.Decimal > 2 {
  1672. // precision := *styleConfig.Decimal - 2
  1673. // precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
  1674. // } else {
  1675. // precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
  1676. // }
  1677. // tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
  1678. // } else {
  1679. // tmp.Monitor = fmt.Sprintf("%g", rounded)
  1680. // }
  1681. // }
  1682. // }
  1683. }
  1684. dataCol = append(dataCol, tmp)
  1685. }
  1686. tableDataList = append(tableDataList, dataCol)
  1687. }
  1688. }
  1689. selfTableData.MergeList = mergeList
  1690. selfTableData.TableDataList = tableDataList
  1691. return
  1692. }