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