lucky_sheet.go 57 KB

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