lucky_sheet.go 58 KB

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