lucky_sheet.go 59 KB

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