lucky_sheet.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage/excel"
  6. "eta/eta_api/models/data_manage/excel/request"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "os"
  10. "reflect"
  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. if len(tableDataList) > 1 {
  578. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  579. isDelete := true
  580. for _, v := range tableDataList[rowIndex] {
  581. if v.Monitor != `` {
  582. isDelete = false
  583. flag = true
  584. break
  585. }
  586. }
  587. if flag {
  588. break
  589. }
  590. if isDelete {
  591. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  592. removeBottomRow++
  593. }
  594. }
  595. }
  596. if len(deleteBottomRowIndexList) > 0 {
  597. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  598. }
  599. lenDeleteRow := len(deleteRowIndexList)
  600. if lenDeleteRow > 0 {
  601. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  602. //表格数据
  603. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  604. //表格高度
  605. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  606. }
  607. }
  608. }
  609. //移除左右空列
  610. deleteColumnIndexList := make([]int, 0)
  611. if len(tableDataList) > 0 {
  612. var flag = false
  613. lenColumn := len(tableDataList[0])
  614. // 移除左边空列
  615. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  616. isDelete := true
  617. for _, v := range tableDataList {
  618. //如果一列都没有,说明是上面几行是空行,没有数据
  619. if len(v) <= 0 {
  620. continue
  621. }
  622. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  623. isDelete = false
  624. flag = true
  625. break
  626. }
  627. }
  628. if flag {
  629. break
  630. }
  631. if isDelete {
  632. removeLeftColumn++
  633. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  634. }
  635. }
  636. // 移除右方空列
  637. flag = false
  638. //右边
  639. deleteTailColumnIndexList := make([]int, 0)
  640. // 数据要大于1列才会处理
  641. if lenColumn > 1 {
  642. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  643. isDelete := true
  644. for _, v := range tableDataList {
  645. //如果一列都没有,说明是上面几行是空行,没有数据
  646. if len(v) <= 0 {
  647. continue
  648. }
  649. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  650. isDelete = false
  651. flag = true
  652. break
  653. }
  654. }
  655. if flag {
  656. break
  657. }
  658. if isDelete {
  659. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  660. removeRightColumn++
  661. }
  662. }
  663. }
  664. if len(deleteTailColumnIndexList) > 0 {
  665. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  666. }
  667. lenDeleteColumn := len(deleteColumnIndexList)
  668. if lenDeleteColumn > 0 {
  669. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  670. //表格数据
  671. for k, v := range tableDataList {
  672. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  673. }
  674. //表格宽度
  675. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  676. }
  677. }
  678. }
  679. //处理合并单元格
  680. for _, v := range luckySheetDataConfigMergeList {
  681. indexRow := v.Row - removeTopRow
  682. indexColumn := v.Column - removeLeftColumn
  683. tableDataMerge := TableDataMerge{
  684. StartRowIndex: indexRow,
  685. StartColumnIndex: indexColumn,
  686. MergeRowNum: v.Rs - 1,
  687. MergeColumnNum: v.Cs - 1,
  688. }
  689. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  690. }
  691. return tableDataList, TableRemoveNum{
  692. RemoveTopRow: removeTopRow,
  693. RemoveBottomRow: removeBottomRow,
  694. RemoveLeftColumn: removeLeftColumn,
  695. RemoveRightColumn: removeRightColumn,
  696. }, rowHeightList, rowWidthList, tableDataMergeList
  697. }
  698. // ToExcel 通过 luckySheetData生成excel表格
  699. func (item *LuckySheetData) ToExcel() (downloadFilePath string, err error) {
  700. tableData, err := item.GetTableDataByLuckySheetDataStr(true)
  701. if err != nil {
  702. return
  703. }
  704. downloadFilePath, err = tableData.ToExcel()
  705. return
  706. }
  707. // getDownloadPath 获取下载地址
  708. func getDownloadPath() (fpath string, err error) {
  709. dateDir := time.Now().Format("20060102")
  710. uploadDir := "static/xls/" + dateDir
  711. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  712. if err != nil {
  713. return
  714. }
  715. randStr := utils.GetRandStringNoSpecialChar(28)
  716. fileName := randStr + ".xlsx"
  717. fpath = uploadDir + "/" + fileName
  718. return
  719. }
  720. // getExcelFontConf 获取表格字体配置
  721. func getExcelFontConf(cellInfo LuckySheetDataValue) xlsx.Font {
  722. fontSize := 10
  723. familyName := ``
  724. var isBold, isItalic, isUnderline bool
  725. // 字体
  726. {
  727. if cellInfo.Bold == 1 {
  728. isBold = true
  729. }
  730. if cellInfo.Italic == 1 {
  731. isItalic = true
  732. }
  733. if cellInfo.CancelLine == 1 {
  734. isUnderline = true
  735. }
  736. //if cellInfo.Fontsize != `` {
  737. // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
  738. // if convSize > 0 {
  739. // fontSize = convSize
  740. // }
  741. //}
  742. if cellInfo.Fontsize > 0 {
  743. fontSize = cellInfo.Fontsize
  744. }
  745. tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
  746. if !familyNameOk {
  747. tmpFamilyName = "宋体"
  748. }
  749. familyName = tmpFamilyName
  750. }
  751. return xlsx.Font{
  752. Size: fontSize,
  753. Name: familyName,
  754. //Family: v2.FontFamily,
  755. //Charset: 0,
  756. Color: strings.TrimPrefix(cellInfo.FontColor, "#"),
  757. Bold: isBold,
  758. Italic: isItalic,
  759. Underline: isUnderline,
  760. }
  761. }
  762. // getExcelAlignmentConf 获取表格单元格排列配置
  763. func getExcelAlignmentConf(cellInfo LuckySheetDataValue) xlsx.Alignment {
  764. //自动换行
  765. isWrapText := false
  766. if cellInfo.TextBeak == 2 {
  767. isWrapText = true
  768. }
  769. rotateText := 0
  770. //if cellInfo.RotateText != `` {
  771. // convSize, _ := strconv.Atoi(cellInfo.RotateText)
  772. // if convSize > 0 {
  773. // rotateText = convSize
  774. // }
  775. //}
  776. return xlsx.Alignment{
  777. Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
  778. //Indent: 0,
  779. //ShrinkToFit: false,
  780. TextRotation: rotateText,
  781. Vertical: LuckyVerticalMap[cellInfo.VerticalType],
  782. WrapText: isWrapText,
  783. }
  784. }
  785. // getExcelBorderConf 获取表格边框配置
  786. //func getExcelBorderConf(list []LuckySheetDataConfigBorderInfo) (v xlsx.Border) {
  787. // // 字体
  788. // for _, v := range list {
  789. // //v.BorderType
  790. // //v.Style
  791. //
  792. // if v.RangeType == "cell" {
  793. //
  794. // xlsx.Border{
  795. // Left: LuckyBorderMap[v.Value.L.Style],
  796. // LeftColor: strings.TrimPrefix(v.Value.L.Color, "#"),
  797. // Right: LuckyBorderMap[v.Value.R.Style],
  798. // RightColor: strings.TrimPrefix(v.Value.R.Color, "#"),
  799. // Top: LuckyBorderMap[v.Value.T.Style],
  800. // TopColor: strings.TrimPrefix(v.Value.T.Color, "#"),
  801. // Bottom: LuckyBorderMap[v.Value.B.Style],
  802. // BottomColor: strings.TrimPrefix(v.Value.B.Color, "#"),
  803. // }
  804. // } else {
  805. // xlsx.Border{
  806. // Left: LuckyBorderMap[v.Style],
  807. // LeftColor: strings.TrimPrefix(v.Color, "#"),
  808. // Right: LuckyBorderMap[v.Style],
  809. // RightColor: strings.TrimPrefix(v.Color, "#"),
  810. // Top: LuckyBorderMap[v.Style],
  811. // TopColor: strings.TrimPrefix(v.Color, "#"),
  812. // Bottom: LuckyBorderMap[v.Style],
  813. // BottomColor: strings.TrimPrefix(v.Color, "#"),
  814. // }
  815. // }
  816. // //v.Value.T.Style
  817. //
  818. // }
  819. //
  820. // return
  821. //}
  822. // LuckyFontFamilyMap 字体map
  823. var LuckyFontFamilyMap = map[int]string{
  824. 0: "Times New Roman",
  825. 1: "Arial",
  826. 2: "Tahoma",
  827. 3: "Verdana",
  828. 4: "微软雅黑",
  829. 5: "宋体", //宋体(Song)、
  830. 6: "黑体", // 黑体(ST Heiti)
  831. 7: "楷体", //楷体(ST Kaiti),
  832. 8: "仿宋", //仿宋(ST FangSong),
  833. 9: "新宋体", //新宋体(ST Song),
  834. 10: "华文新魏",
  835. 11: "华文行楷",
  836. 12: "华文隶书",
  837. }
  838. // LuckyBorderMap 边框map
  839. var LuckyBorderMap = map[string]string{
  840. "": "Thin",
  841. "1": "Thin",
  842. "2": "Hair",
  843. "3": "Dotted",
  844. "4": "Dashed",
  845. "5": "DashDot", //宋体(Song)、
  846. "6": "DashDotDot", // 黑体(ST Heiti)
  847. "7": "Double", //楷体(ST Kaiti),
  848. "8": "Medium", //仿宋(ST FangSong),
  849. "9": "MediumDashed", //新宋体(ST Song),
  850. "10": "MediumDashDot",
  851. "11": "MediumDashDotDot",
  852. "12": "SlantedDashDot",
  853. "13": "Thick",
  854. }
  855. // LuckyHorizontalMap 水平对齐map
  856. var LuckyHorizontalMap = map[int]string{
  857. //"": "general",
  858. 0: "center",
  859. 1: "left",
  860. 2: "right",
  861. }
  862. // LuckyVerticalMap 垂直对齐map
  863. var LuckyVerticalMap = map[int]string{
  864. //"": "center",
  865. 0: "center",
  866. 1: "top",
  867. //2: "bottom",
  868. }
  869. // GetColumnName Return the ColumnName
  870. func GetColumnName(rowNumber int, colNumber int) (colName string, err error) {
  871. if rowNumber < 1 || colNumber < 1 {
  872. err = errors.New("Row and Column Number can not be less than 1")
  873. return
  874. }
  875. dividend := colNumber
  876. var modulo int
  877. for dividend > 0 {
  878. modulo = (dividend - 1) % 26
  879. // Convert int to char
  880. colName = string(65+modulo) + colName
  881. dividend = (int)((dividend - modulo) / 26)
  882. }
  883. colName = colName + strconv.Itoa(rowNumber)
  884. return
  885. }
  886. // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
  887. func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
  888. luckySheetCellDataList := item.CellData
  889. // 表格数据
  890. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  891. // 最大行,最大列
  892. var maxRow, maxCol int64
  893. for _, v := range luckySheetCellDataList {
  894. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  895. if v.Row > maxRow { //最大行
  896. maxRow = v.Row
  897. }
  898. if v.Col > maxCol { //最大列
  899. maxCol = v.Col
  900. }
  901. var tmpRow map[int64]LuckySheetDataValue
  902. tmpRow, ok := tableDataMap[v.Row]
  903. if ok {
  904. tmpRow[v.Col] = v.Value
  905. } else {
  906. tmpRow = make(map[int64]LuckySheetDataValue)
  907. tmpRow[v.Col] = v.Value
  908. }
  909. tableDataMap[v.Row] = tmpRow
  910. }
  911. tableDataList := make([][]LuckySheetDataValue, 0)
  912. var i int64
  913. // 单元格宽度
  914. configColumnConf := item.Config.Columnlen
  915. rowWidthMap := make(map[int]float64)
  916. rowWidthList := make([]float64, 0) //
  917. // 单元格高度
  918. configRowConf := item.Config.Rowlen
  919. rowHeightList := make([]float64, 0) //
  920. for i = 0; i <= maxRow; i++ {
  921. //列
  922. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  923. // 每个单元格的高度
  924. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  925. if !ok {
  926. tmpHeight = 0
  927. }
  928. rowHeightList = append(rowHeightList, tmpHeight)
  929. tmpRowData, ok := tableDataMap[i]
  930. // 如果没有该行数据,那么就默认添加空行数据处理
  931. if !ok {
  932. tmpRowData = make(map[int64]LuckySheetDataValue)
  933. }
  934. var j int64
  935. for j = 0; j <= maxCol; j++ {
  936. tmpTableColData, ok := tmpRowData[j]
  937. if !ok {
  938. tmpTableColData = LuckySheetDataValue{}
  939. }
  940. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  941. // 每个单元格的宽度
  942. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  943. if !ok {
  944. tmpWidth = 0
  945. }
  946. rowIndex := len(tmpTableColDataList) - 1
  947. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  948. rowWidthList = append(rowWidthList, tmpWidth)
  949. }
  950. rowWidthMap[rowIndex] = tmpWidth
  951. }
  952. tableDataList = append(tableDataList, tmpTableColDataList)
  953. }
  954. //总共多少行
  955. lenRow := len(tableDataList)
  956. //移除上下空行
  957. deleteRowIndexList := make([]int, 0)
  958. if lenRow > 0 {
  959. var flag = false
  960. // 移除上方空列
  961. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  962. isDelete := true
  963. for _, v := range tableDataList[rowIndex] {
  964. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  965. isDelete = false
  966. flag = true
  967. break
  968. }
  969. }
  970. if flag {
  971. break
  972. }
  973. if isDelete {
  974. selfTableData.RemoveTopRow++
  975. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  976. }
  977. }
  978. // 移除下方空行
  979. flag = false
  980. //尾部
  981. deleteBottomRowIndexList := make([]int, 0)
  982. for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
  983. isDelete := true
  984. for _, v := range tableDataList[rowIndex] {
  985. if v.Monitor != `` {
  986. isDelete = false
  987. flag = true
  988. break
  989. }
  990. }
  991. if flag {
  992. break
  993. }
  994. if isDelete {
  995. deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
  996. selfTableData.RemoveBottomRow++
  997. }
  998. }
  999. if len(deleteBottomRowIndexList) > 0 {
  1000. deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
  1001. }
  1002. lenDeleteRow := len(deleteRowIndexList)
  1003. if lenDeleteRow > 0 {
  1004. for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
  1005. //表格数据
  1006. tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  1007. //表格高度
  1008. rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
  1009. }
  1010. }
  1011. }
  1012. //移除左右空列
  1013. deleteColumnIndexList := make([]int, 0)
  1014. if len(tableDataList) > 0 {
  1015. var flag = false
  1016. lenColumn := len(tableDataList[0])
  1017. // 移除左边空列
  1018. for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
  1019. isDelete := true
  1020. for _, v := range tableDataList {
  1021. //如果一列都没有,说明是上面几行是空行,没有数据
  1022. if len(v) <= 0 {
  1023. continue
  1024. }
  1025. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  1026. isDelete = false
  1027. flag = true
  1028. break
  1029. }
  1030. }
  1031. if flag {
  1032. break
  1033. }
  1034. if isDelete {
  1035. selfTableData.RemoveLeftColumn++
  1036. deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
  1037. }
  1038. }
  1039. // 移除右方空列
  1040. flag = false
  1041. //右边
  1042. deleteTailColumnIndexList := make([]int, 0)
  1043. for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
  1044. isDelete := true
  1045. for _, v := range tableDataList {
  1046. //如果一列都没有,说明是上面几行是空行,没有数据
  1047. if len(v) <= 0 {
  1048. continue
  1049. }
  1050. if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
  1051. isDelete = false
  1052. flag = true
  1053. break
  1054. }
  1055. }
  1056. if flag {
  1057. break
  1058. }
  1059. if isDelete {
  1060. deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
  1061. selfTableData.RemoveRightColumn++
  1062. }
  1063. }
  1064. if len(deleteTailColumnIndexList) > 0 {
  1065. deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
  1066. }
  1067. lenDeleteColumn := len(deleteColumnIndexList)
  1068. if lenDeleteColumn > 0 {
  1069. for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
  1070. //表格数据
  1071. for k, v := range tableDataList {
  1072. tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  1073. }
  1074. //表格宽度
  1075. rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
  1076. }
  1077. }
  1078. }
  1079. //处理合并单元格
  1080. tableDataMergeList := make([]TableDataMerge, 0)
  1081. for _, v := range item.Config.Merge {
  1082. indexRow := v.Row - selfTableData.RemoveTopRow
  1083. indexColumn := v.Column - selfTableData.RemoveLeftColumn
  1084. tableDataMerge := TableDataMerge{
  1085. StartRowIndex: indexRow,
  1086. StartColumnIndex: indexColumn,
  1087. MergeRowNum: v.Rs - 1,
  1088. MergeColumnNum: v.Cs - 1,
  1089. }
  1090. tableDataMergeList = append(tableDataMergeList, tableDataMerge)
  1091. }
  1092. // 表格数据
  1093. selfTableData.TableDataList = tableDataList
  1094. selfTableData.RowWidthList = rowWidthList
  1095. selfTableData.RowHeightList = rowHeightList
  1096. selfTableData.MergeList = tableDataMergeList
  1097. return
  1098. }
  1099. func (item *LuckySheetData) ToExcel2() (downloadFilePath string, err error) {
  1100. tableData, err := item.GetTableDataByLuckySheetDataStr(true)
  1101. downloadFilePath, err = getDownloadPath()
  1102. if err != nil {
  1103. return
  1104. }
  1105. f := excelize.NewFile()
  1106. // Create a new sheet.
  1107. sheetName := `Sheet1`
  1108. sheetIndex, err := f.NewSheet(sheetName)
  1109. if err != nil {
  1110. return
  1111. }
  1112. //设置列宽度
  1113. for k, v := range tableData.RowWidthList {
  1114. colName, tmpErr := excelize.ColumnNumberToName(k + 1)
  1115. if tmpErr != nil {
  1116. err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
  1117. err = tmpErr
  1118. return
  1119. }
  1120. err = f.SetColWidth(sheetName, colName, colName, v/10)
  1121. if err != nil {
  1122. err = errors.New("设置列宽度,ERR:" + tmpErr.Error())
  1123. return
  1124. }
  1125. }
  1126. // 写入数据
  1127. for index, v := range tableData.TableDataList {
  1128. //设置单元格高度
  1129. if tableData.RowHeightList[index] > 0 {
  1130. tmpErr := f.SetRowHeight(sheetName, index+1, tableData.RowHeightList[index]/2)
  1131. if tmpErr != nil {
  1132. err = errors.New("设置单元格高度失败,ERR:" + tmpErr.Error())
  1133. return
  1134. }
  1135. }
  1136. for colunmIndex, cellInfo := range v {
  1137. //f.NewStyle
  1138. defaultFill := excelize.Fill{
  1139. //Type: "solid",
  1140. Pattern: 1,
  1141. //Color: []string{cellInfo.Background},
  1142. Shading: 0,
  1143. }
  1144. if cellInfo.Background != `` {
  1145. defaultFill.Type = "solid"
  1146. defaultFill.Color = []string{cellInfo.Background}
  1147. }
  1148. cellStyle := &excelize.Style{
  1149. Border: []excelize.Border{
  1150. {Type: "left", Color: "000000", Style: 1},
  1151. {Type: "top", Color: "000000", Style: 1},
  1152. {Type: "bottom", Color: "000000", Style: 1},
  1153. {Type: "right", Color: "000000", Style: 1},
  1154. //{Type: "diagonalDown", Color: "A020F0", Style: 7},
  1155. //{Type: "diagonalUp", Color: "A020F0", Style: 8},
  1156. },
  1157. Fill: defaultFill,
  1158. Font: getExcelizeFontConf(cellInfo),
  1159. Alignment: getExcelizeAlignmentConf(cellInfo),
  1160. //Protection: nil,
  1161. //NumFmt: 0,
  1162. //DecimalPlaces: 0,
  1163. //CustomNumFmt: nil,
  1164. //Lang: "",
  1165. //NegRed: false,
  1166. }
  1167. cellStyleId, tmpErr := f.NewStyle(cellStyle)
  1168. if tmpErr != nil {
  1169. err = errors.New("new Style 失败,ERR:" + tmpErr.Error())
  1170. return
  1171. }
  1172. columnName, tmpErr := excelize.ColumnNumberToName(colunmIndex + 1)
  1173. if tmpErr != nil {
  1174. err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
  1175. return
  1176. }
  1177. cellName := columnName + strconv.Itoa(index+1)
  1178. tmpErr = f.SetCellStyle(sheetName, cellName, cellName, cellStyleId)
  1179. if tmpErr != nil {
  1180. err = errors.New("设置style失败,ERR:" + tmpErr.Error())
  1181. return
  1182. }
  1183. valueStr := cellInfo.Monitor
  1184. if valueStr == `` {
  1185. //valueStr = fmt.Sprint(cellInfo.Value)
  1186. if valueStr == `` && cellInfo.CellType.S != nil {
  1187. //不是设置在单元格上面,而是设置在文本上
  1188. for _, cellS := range cellInfo.CellType.S {
  1189. valueStr += fmt.Sprint(cellS.Value)
  1190. }
  1191. }
  1192. }
  1193. tmpErr = f.SetCellStr(sheetName, cellName, valueStr)
  1194. if tmpErr != nil {
  1195. err = errors.New("设置单元格数据失败,ERR:" + tmpErr.Error())
  1196. return
  1197. }
  1198. }
  1199. }
  1200. for _, v := range tableData.MergeList {
  1201. //开始列
  1202. startColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + 1)
  1203. if tmpErr != nil {
  1204. err = tmpErr
  1205. return
  1206. }
  1207. startCellName := startColumnName + strconv.Itoa(v.StartRowIndex+1)
  1208. //结束列
  1209. endColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + v.MergeColumnNum + 1)
  1210. if tmpErr != nil {
  1211. err = tmpErr
  1212. return
  1213. }
  1214. endCellName := endColumnName + strconv.Itoa(v.StartRowIndex+v.MergeRowNum+1)
  1215. err = f.MergeCell(sheetName, startCellName, endCellName)
  1216. if err != nil {
  1217. return
  1218. }
  1219. }
  1220. // Set active sheet of the workbook.
  1221. f.SetActiveSheet(sheetIndex)
  1222. // Save spreadsheet by the given path.
  1223. err = f.SaveAs(downloadFilePath)
  1224. return
  1225. }
  1226. // getExcelizeFontConf 获取表格字体配置
  1227. func getExcelizeFontConf(cellInfo LuckySheetDataValue) *excelize.Font {
  1228. fontSize := 10
  1229. familyName := ``
  1230. //single 单线 、double 双线
  1231. underlineStyle := ``
  1232. var isBold, isItalic, isUnderline bool
  1233. // 字体
  1234. {
  1235. if cellInfo.Bold == 1 {
  1236. isBold = true
  1237. }
  1238. if cellInfo.Italic == 1 {
  1239. isItalic = true
  1240. }
  1241. if cellInfo.CancelLine == 1 {
  1242. isUnderline = true
  1243. }
  1244. //if cellInfo.Fontsize != `` {
  1245. // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
  1246. // if convSize > 0 {
  1247. // fontSize = convSize
  1248. // }
  1249. //}
  1250. if cellInfo.Fontsize > 0 {
  1251. fontSize = cellInfo.Fontsize
  1252. }
  1253. tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
  1254. if !familyNameOk {
  1255. tmpFamilyName = "宋体"
  1256. }
  1257. familyName = tmpFamilyName
  1258. }
  1259. if isUnderline {
  1260. underlineStyle = "single"
  1261. }
  1262. //VertAlign取值:
  1263. //BASELINE、superscript、subscript
  1264. return &excelize.Font{
  1265. Bold: isBold,
  1266. Italic: isItalic,
  1267. Underline: underlineStyle,
  1268. Family: familyName,
  1269. Size: float64(fontSize),
  1270. Strike: isUnderline,
  1271. Color: cellInfo.FontColor,
  1272. VertAlign: "",
  1273. }
  1274. }
  1275. // getExcelizeAlignmentConf 获取表格单元格排列配置
  1276. func getExcelizeAlignmentConf(cellInfo LuckySheetDataValue) *excelize.Alignment {
  1277. //自动换行
  1278. isWrapText := false
  1279. if cellInfo.TextBeak == 2 {
  1280. isWrapText = true
  1281. }
  1282. rotateText := 0
  1283. //if cellInfo.RotateText != `` {
  1284. // convSize, _ := strconv.Atoi(cellInfo.RotateText)
  1285. // if convSize > 0 {
  1286. // rotateText = convSize
  1287. // }
  1288. //}
  1289. return &excelize.Alignment{
  1290. Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
  1291. //Indent: 0,
  1292. //JustifyLastLine: false,
  1293. //ReadingOrder: 0,
  1294. //RelativeIndent: 0,
  1295. //ShrinkToFit: false,
  1296. TextRotation: rotateText,
  1297. Vertical: LuckyVerticalMap[cellInfo.VerticalType],
  1298. WrapText: isWrapText,
  1299. }
  1300. }
  1301. // getColor 获取hex颜色
  1302. func getColor(bgStr string) string {
  1303. if strings.Contains(bgStr, "(") {
  1304. arr := strings.Split(bgStr, ",")
  1305. if len(arr) != 3 {
  1306. return bgStr
  1307. }
  1308. // 第一位
  1309. tmpFirstArr := strings.Split(arr[0], "(")
  1310. arr[0] = tmpFirstArr[len(tmpFirstArr)-1]
  1311. // 最后一位
  1312. tmpLastArr := strings.Split(arr[2], ")")
  1313. arr[2] = tmpLastArr[0]
  1314. rgbArr := make([]int64, 0)
  1315. for _, v := range arr {
  1316. tmpInt, err := strconv.Atoi(utils.TrimStr(v))
  1317. if err != nil {
  1318. return bgStr
  1319. }
  1320. rgbArr = append(rgbArr, int64(tmpInt))
  1321. }
  1322. rgb := RGB{
  1323. rgbArr[0],
  1324. rgbArr[1],
  1325. rgbArr[2],
  1326. }
  1327. bgStr = "" + rgb.Rgb2Hex().Str
  1328. }
  1329. return bgStr
  1330. }
  1331. // GetTableDataByCustomData 通过自定义表格数据获取表格数据
  1332. func GetTableDataByCustomData(excelType int, data request.TableDataReq, lang string) (selfTableData TableData, err error) {
  1333. tableDataList := make([][]LuckySheetDataValue, 0)
  1334. mergeList := make([]TableDataMerge, 0)
  1335. // 指标数
  1336. lenEdb := len(data.Data)
  1337. if lenEdb <= 0 {
  1338. return
  1339. }
  1340. // 日期数
  1341. lenCol := len(data.Data[0].Data)
  1342. if lenCol <= 0 {
  1343. return
  1344. }
  1345. // 指标列:1;指标行:2
  1346. if excelType == 1 {
  1347. mergeList = append(mergeList, TableDataMerge{
  1348. StartRowIndex: 0,
  1349. StartColumnIndex: 0,
  1350. MergeRowNum: 1,
  1351. MergeColumnNum: 0,
  1352. })
  1353. // 第一行
  1354. {
  1355. firstCol := make([]LuckySheetDataValue, 0)
  1356. dateStr := "日期"
  1357. switch lang {
  1358. case utils.EnLangVersion:
  1359. dateStr = "Date"
  1360. }
  1361. firstCol = append(firstCol, LuckySheetDataValue{
  1362. Value: dateStr,
  1363. Monitor: dateStr,
  1364. MergeCell: LuckySheetDataConfigMerge{
  1365. Row: 0, //行数
  1366. Column: 0, //列数
  1367. Rs: 2, //合并的行数
  1368. Cs: 1, //合并的列数
  1369. },
  1370. })
  1371. for _, v := range data.Data {
  1372. edbName := v.EdbName
  1373. if v.EdbAliasName != `` {
  1374. edbName = v.EdbAliasName
  1375. } else {
  1376. switch lang {
  1377. case utils.EnLangVersion:
  1378. edbName = v.EdbNameEn
  1379. }
  1380. }
  1381. firstCol = append(firstCol, LuckySheetDataValue{
  1382. Value: edbName,
  1383. Monitor: edbName,
  1384. MergeCell: LuckySheetDataConfigMerge{},
  1385. })
  1386. }
  1387. tableDataList = append(tableDataList, firstCol)
  1388. }
  1389. // 第二行
  1390. {
  1391. secondCol := make([]LuckySheetDataValue, 0)
  1392. secondCol = append(secondCol, LuckySheetDataValue{})
  1393. for _, v := range data.Data {
  1394. var name string
  1395. switch lang {
  1396. case utils.EnLangVersion:
  1397. name = v.UnitEn + " / " + utils.GetFrequencyEn(v.Frequency)
  1398. default:
  1399. name = v.Unit + " / " + v.Frequency
  1400. }
  1401. secondCol = append(secondCol, LuckySheetDataValue{
  1402. Value: name,
  1403. Monitor: name,
  1404. MergeCell: LuckySheetDataConfigMerge{},
  1405. })
  1406. }
  1407. tableDataList = append(tableDataList, secondCol)
  1408. }
  1409. // 开始数据了
  1410. {
  1411. for i := 0; i < lenCol; i++ {
  1412. dataCol := make([]LuckySheetDataValue, 0)
  1413. // 日期
  1414. dataCol = append(dataCol, LuckySheetDataValue{
  1415. Value: data.Data[0].Data[i].DataTime,
  1416. Monitor: data.Data[0].Data[i].DataTime,
  1417. MergeCell: LuckySheetDataConfigMerge{},
  1418. })
  1419. // 数据值
  1420. for _, v := range data.Data {
  1421. background := ``
  1422. if v.Data[i].DataType == 5 {
  1423. background = "#ffefdd"
  1424. }
  1425. dataCol = append(dataCol, LuckySheetDataValue{
  1426. Value: v.Data[i].Value,
  1427. Monitor: v.Data[i].ShowValue,
  1428. MergeCell: LuckySheetDataConfigMerge{},
  1429. Background: background,
  1430. })
  1431. }
  1432. tableDataList = append(tableDataList, dataCol)
  1433. }
  1434. }
  1435. // 开始文本行了
  1436. {
  1437. for _, textColList := range data.TextRowData {
  1438. dataCol := make([]LuckySheetDataValue, 0)
  1439. for _, v := range textColList {
  1440. dataCol = append(dataCol, LuckySheetDataValue{
  1441. Value: v.Value,
  1442. Monitor: v.ShowValue,
  1443. MergeCell: LuckySheetDataConfigMerge{},
  1444. })
  1445. }
  1446. tableDataList = append(tableDataList, dataCol)
  1447. }
  1448. }
  1449. } else {
  1450. // 指标行
  1451. mergeList = append(mergeList, TableDataMerge{
  1452. StartRowIndex: 0,
  1453. StartColumnIndex: 0,
  1454. MergeRowNum: 0,
  1455. MergeColumnNum: 1,
  1456. })
  1457. // 第一行
  1458. {
  1459. firstCol := make([]LuckySheetDataValue, 0)
  1460. dateStr := "日期"
  1461. switch lang {
  1462. case utils.EnLangVersion:
  1463. dateStr = "Date"
  1464. }
  1465. firstCol = append(firstCol, LuckySheetDataValue{
  1466. Value: dateStr,
  1467. Monitor: dateStr,
  1468. MergeCell: LuckySheetDataConfigMerge{
  1469. Row: 0, //行数
  1470. Column: 0, //列数
  1471. Rs: 1, //合并的行数
  1472. Cs: 2, //合并的列数
  1473. },
  1474. })
  1475. firstCol = append(firstCol, LuckySheetDataValue{})
  1476. // 日期列
  1477. for _, v := range data.Data[0].Data {
  1478. firstCol = append(firstCol, LuckySheetDataValue{
  1479. Value: v.DataTime,
  1480. Monitor: v.DataTime,
  1481. MergeCell: LuckySheetDataConfigMerge{},
  1482. })
  1483. }
  1484. // 文本列
  1485. for _, textColList := range data.TextRowData {
  1486. firstCol = append(firstCol, LuckySheetDataValue{
  1487. Value: textColList[0].Value,
  1488. Monitor: textColList[0].ShowValue,
  1489. MergeCell: LuckySheetDataConfigMerge{},
  1490. })
  1491. }
  1492. tableDataList = append(tableDataList, firstCol)
  1493. }
  1494. // 日期列+文本列+两列表头(日期这个文案表头)
  1495. //colLen := lenCol+2+len(data.TextRowData)
  1496. for i := 0; i < lenEdb; i++ {
  1497. dataCol := make([]LuckySheetDataValue, 0)
  1498. // 指标信息
  1499. tmpEdbInfo := data.Data[i]
  1500. // 指标名称
  1501. {
  1502. edbName := tmpEdbInfo.EdbName
  1503. if tmpEdbInfo.EdbAliasName != `` {
  1504. edbName = tmpEdbInfo.EdbAliasName
  1505. } else {
  1506. switch lang {
  1507. case utils.EnLangVersion:
  1508. edbName = tmpEdbInfo.EdbNameEn
  1509. }
  1510. }
  1511. dataCol = append(dataCol, LuckySheetDataValue{
  1512. Value: edbName,
  1513. Monitor: edbName,
  1514. MergeCell: LuckySheetDataConfigMerge{},
  1515. })
  1516. }
  1517. // 指标单位、频度
  1518. {
  1519. var name string
  1520. switch lang {
  1521. case utils.EnLangVersion:
  1522. name = tmpEdbInfo.UnitEn + " / " + utils.GetFrequencyEn(tmpEdbInfo.Frequency)
  1523. default:
  1524. name = tmpEdbInfo.Unit + " / " + tmpEdbInfo.Frequency
  1525. }
  1526. dataCol = append(dataCol, LuckySheetDataValue{
  1527. Value: name,
  1528. Monitor: name,
  1529. MergeCell: LuckySheetDataConfigMerge{},
  1530. })
  1531. }
  1532. // 指标数据列
  1533. for _, tmpData := range tmpEdbInfo.Data {
  1534. background := ``
  1535. if tmpData.DataType == 5 {
  1536. background = "#ffefdd"
  1537. }
  1538. dataCol = append(dataCol, LuckySheetDataValue{
  1539. Value: tmpData.Value,
  1540. Monitor: tmpData.ShowValue,
  1541. MergeCell: LuckySheetDataConfigMerge{},
  1542. Background: background,
  1543. })
  1544. }
  1545. // 文本列
  1546. for _, textColList := range data.TextRowData {
  1547. dataCol = append(dataCol, LuckySheetDataValue{
  1548. Value: textColList[i+1].Value,
  1549. Monitor: textColList[i+1].ShowValue,
  1550. MergeCell: LuckySheetDataConfigMerge{},
  1551. })
  1552. }
  1553. tableDataList = append(tableDataList, dataCol)
  1554. }
  1555. }
  1556. selfTableData.MergeList = mergeList
  1557. selfTableData.TableDataList = tableDataList
  1558. return
  1559. }
  1560. // GetTableDataByMixedTableData 通过混合表格数据获取表格数据
  1561. func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq, hideMerged bool, excelInfoId int) (selfTableData TableData, err error) {
  1562. tableDataList := make([][]LuckySheetDataValue, 0)
  1563. mergeList := make([]TableDataMerge, 0)
  1564. // 获取管理规则
  1565. excelRuleMappingList, err := excel.GetExcelRuleMappingByExcelInfoId(excelInfoId)
  1566. if err != nil {
  1567. return
  1568. }
  1569. excelRuleMap := make(map[int]*excel.ExcelInfoRuleMappingView)
  1570. for _, v := range excelRuleMappingList {
  1571. excelRuleMap[v.ExcelInfoRuleMappingId] = v
  1572. }
  1573. ruleScopeMap := generateRuleScopeIndexMap(excelRuleMappingList)
  1574. // 开始文本行了
  1575. {
  1576. for i, row := range config {
  1577. dataCol := make([]LuckySheetDataValue, 0)
  1578. for j, cell := range row {
  1579. tmp := LuckySheetDataValue{
  1580. Value: cell.Value,
  1581. Monitor: cell.ShowValue,
  1582. MergeCell: LuckySheetDataConfigMerge{},
  1583. }
  1584. if cell.DataType != request.CustomTextDT && tmp.Monitor == `` {
  1585. tmp.Monitor = "-"
  1586. cell.ShowFormatValue = "-"
  1587. }
  1588. // TODO: 混合表格/平衡表-合并单元格
  1589. // 前端需要隐藏被合并的单元格, 混合表格/平衡表通过这个字段判断, 不通过HandleTableCell方法隐藏
  1590. if cell.MerData != nil {
  1591. if hideMerged && cell.MerData.Type == "merged" {
  1592. continue
  1593. }
  1594. tmp.MergeCell.Rs = cell.MerData.Mer.Rowspan
  1595. tmp.MergeCell.Cs = cell.MerData.Mer.Colspan
  1596. tmp.MergeCell.Row = cell.MerData.Mer.Row
  1597. tmp.MergeCell.Column = cell.MerData.Mer.Col
  1598. }
  1599. if cell.ShowStyle != "" {
  1600. var styleConfig request.MixCellShowStyle
  1601. if err := json.Unmarshal([]byte(cell.ShowStyle), &styleConfig); err != nil {
  1602. utils.FileLog.Info("表格样式showStyle解析失败", err.Error())
  1603. }
  1604. if styleConfig.BackgroundColor != "" {
  1605. tmp.Background = styleConfig.BackgroundColor
  1606. }
  1607. if styleConfig.Color != "" {
  1608. tmp.FontColor = styleConfig.Color
  1609. }
  1610. switch styleConfig.Align {
  1611. case "center":
  1612. tmp.HorizontalType = 0
  1613. tmp.Ht = 0
  1614. case "left":
  1615. tmp.HorizontalType = 1
  1616. tmp.Ht = 1
  1617. case "right":
  1618. tmp.HorizontalType = 2
  1619. tmp.Ht = 2
  1620. }
  1621. showFormatValue := fmt.Sprintf("%v", cell.ShowFormatValue)
  1622. tmp.Monitor = showFormatValue
  1623. // 如果没有showValue, 则使用value
  1624. if cell.ShowValue == "" {
  1625. _, err := strconv.ParseFloat(cell.Value, 64)
  1626. if err == nil {
  1627. cell.ShowValue = cell.Value
  1628. }
  1629. }
  1630. _, err := strconv.ParseFloat(cell.Value, 64)
  1631. if err == nil {
  1632. hasPercent := false
  1633. if styleConfig.Nt == "percent" {
  1634. hasPercent = true
  1635. }
  1636. // 修复历史数据
  1637. if styleConfig.Pn != 0 {
  1638. if styleConfig.Decimal == nil {
  1639. styleConfig.Decimal = new(int)
  1640. }
  1641. *styleConfig.Decimal += styleConfig.Pn
  1642. if *styleConfig.Decimal < 0 {
  1643. *styleConfig.Decimal = 0
  1644. }
  1645. }
  1646. if styleConfig.Decimal != nil {
  1647. tmp.Monitor = utils.RoundNumber(cell.ShowValue, *styleConfig.Decimal, hasPercent)
  1648. } else {
  1649. if hasPercent {
  1650. numDecimal, _ := decimal.NewFromString(cell.ShowValue)
  1651. tmpStr := numDecimal.Mul(decimal.NewFromInt(100)).String()
  1652. tmp.Monitor = tmpStr + "%"
  1653. } else {
  1654. tmp.Monitor = cell.ShowValue
  1655. }
  1656. }
  1657. } else {
  1658. if cell.DataType == request.CustomTextDT {
  1659. tmp.Monitor = cell.Value
  1660. }
  1661. }
  1662. // switch styleConfig.Last {
  1663. // case "nt":
  1664. // // 先进行数字的百分比计算,然后保留小数点位数
  1665. // percent := tmpShowValue * 100
  1666. // if styleConfig.Decimal == nil {
  1667. // continue
  1668. // }
  1669. // factor := math.Pow(10, float64(*styleConfig.Decimal))
  1670. // rounded := math.Round(percent*factor) / factor
  1671. // tmp.Monitor = fmt.Sprintf("%g%%", rounded)
  1672. // case "decimal":
  1673. // // 先保留小数点位数,再进行百分比计算
  1674. // if styleConfig.Decimal == nil {
  1675. // continue
  1676. // }
  1677. // factor := math.Pow(10, float64(*styleConfig.Decimal))
  1678. // rounded := math.Round(tmpShowValue*factor) / factor
  1679. // if styleConfig.Nt == "percent" {
  1680. // percent := rounded * 100
  1681. // var precisionStr string
  1682. // if *styleConfig.Decimal > 2 {
  1683. // precision := *styleConfig.Decimal - 2
  1684. // precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
  1685. // } else {
  1686. // precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
  1687. // }
  1688. // tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
  1689. // } else {
  1690. // tmp.Monitor = fmt.Sprintf("%g", rounded)
  1691. // }
  1692. // }
  1693. // }
  1694. }
  1695. if ruleIds, ok := ruleScopeMap[i][j]; ok {
  1696. for _, ruleId := range ruleIds {
  1697. if checkCellRule(excelRuleMap[ruleId], cell.ShowValue, config) {
  1698. tmp.Background = strings.TrimSpace(excelRuleMap[ruleId].BackgroundColor)
  1699. tmp.FontColor = strings.TrimSpace(excelRuleMap[ruleId].FontColor)
  1700. }
  1701. }
  1702. }
  1703. dataCol = append(dataCol, tmp)
  1704. }
  1705. tableDataList = append(tableDataList, dataCol)
  1706. }
  1707. }
  1708. selfTableData.MergeList = mergeList
  1709. selfTableData.TableDataList = tableDataList
  1710. return
  1711. }