lucky_sheet.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage/excel/request"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "math"
  9. "os"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/tealeg/xlsx"
  15. "github.com/xuri/excelize/v2"
  16. )
  17. type LuckySheetDataBak struct {
  18. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  19. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  20. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  21. Config struct {
  22. BorderInfo []struct {
  23. BorderType string `json:"borderType" description:""`
  24. Color string `json:"color" description:""`
  25. Range []struct {
  26. Column []int64 `json:"column" description:""`
  27. Row []int64 `json:"row" description:""`
  28. } `json:"range" description:""`
  29. RangeType string `json:"rangeType" description:""`
  30. Style string `json:"style" description:""`
  31. Value struct {
  32. B struct {
  33. Color string `json:"color" description:""`
  34. Style string `json:"style" description:""`
  35. } `json:"b" description:""`
  36. ColIndex int64 `json:"col_index" description:""`
  37. L struct {
  38. Color string `json:"color" description:""`
  39. Style string `json:"style" description:""`
  40. } `json:"l" description:""`
  41. R struct {
  42. Color string `json:"color" description:""`
  43. Style string `json:"style" description:""`
  44. } `json:"r" description:""`
  45. RowIndex int64 `json:"row_index" description:""`
  46. T struct {
  47. Color string `json:"color" description:""`
  48. Style string `json:"style" description:""`
  49. } `json:"t" description:""`
  50. } `json:"value" description:"" description:""`
  51. } `json:"borderInfo" description:""`
  52. Colhidden struct{} `json:"colhidden" description:""`
  53. Columnlen map[string]float64 `json:"columnlen" description:""`
  54. CustomHeight struct {
  55. Zero int64 `json:"0"`
  56. } `json:"customHeight" description:""`
  57. CustomWidth struct {
  58. Two int64 `json:"2" description:""`
  59. } `json:"customWidth" description:""`
  60. Merge struct{} `json:"merge" description:""`
  61. Rowlen map[string]float64 `json:"rowlen" description:""`
  62. } `json:"config" description:""`
  63. Data [][]struct {
  64. Ct struct {
  65. Fa string `json:"fa"`
  66. T string `json:"t"`
  67. } `json:"ct"`
  68. M string `json:"m"`
  69. V interface{} `json:"v"`
  70. } `json:"data" description:""`
  71. DataVerification struct{} `json:"dataVerification" description:""`
  72. Filter interface{} `json:"filter" description:""`
  73. FilterSelect interface{} `json:"filter_select" description:""`
  74. Hyperlink struct{} `json:"hyperlink" description:""`
  75. Images struct{} `json:"images" description:""`
  76. Index string `json:"index" description:""`
  77. JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
  78. LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
  79. LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
  80. LuckysheetSelectSave []struct {
  81. Column []int64 `json:"column" description:""`
  82. ColumnFocus int64 `json:"column_focus" description:""`
  83. Height int64 `json:"height" description:""`
  84. HeightMove int64 `json:"height_move" description:""`
  85. Left int64 `json:"left" description:""`
  86. LeftMove int64 `json:"left_move" description:""`
  87. Row []int64 `json:"row" description:""`
  88. RowFocus int64 `json:"row_focus" description:""`
  89. Top int64 `json:"top" description:""`
  90. TopMove int64 `json:"top_move" description:""`
  91. Width int64 `json:"width" description:""`
  92. WidthMove int64 `json:"width_move" description:""`
  93. } `json:"luckysheet_select_save" description:"" description:""`
  94. LuckysheetSelectionRange []struct {
  95. Column []int64 `json:"column" description:""`
  96. Row []int64 `json:"row" description:""`
  97. } `json:"luckysheet_selection_range" description:""`
  98. RhHeight float64 `json:"rh_height" description:""`
  99. ScrollLeft float64 `json:"scrollLeft" description:""`
  100. ScrollTop float64 `json:"scrollTop" description:""`
  101. Status int64 `json:"status" description:""`
  102. Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
  103. Visibledatarow []int64 `json:"visibledatarow" description:""`
  104. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  105. }
  106. // LuckySheetData sheet表格数据
  107. type LuckySheetData struct {
  108. Name string `json:"name" description:"工作表名称"`
  109. Index interface{} `json:"index" description:"工作表索引"`
  110. Row int `json:"row" description:"行数"`
  111. Column int `json:"column" description:"列数"`
  112. CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
  113. ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
  114. Config LuckySheetDataConfig `json:"config" description:""`
  115. //Index int `json:"index" description:"工作表索引"`
  116. RhHeight float64 `json:"rh_height" description:"工作表区域的高度"`
  117. ScrollLeft float64 `json:"scrollLeft" description:"左右滚动条位置"`
  118. ScrollTop float64 `json:"scrollTop" description:"上下滚动条位置"`
  119. CalcChain []interface{} `json:"calcChain" description:"公式链"`
  120. Status interface{} `json:"status" description:"激活状态"`
  121. VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
  122. VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
  123. ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
  124. }
  125. // LuckySheetDataConfig sheet表单的配置
  126. type LuckySheetDataConfig struct {
  127. BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
  128. Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
  129. Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
  130. //CustomHeight struct {
  131. // Zero int64 `json:"0"`
  132. //} `json:"customHeight" description:""`
  133. //CustomWidth struct {
  134. // Two int64 `json:"2" description:""`
  135. //} `json:"customWidth" description:""`
  136. Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
  137. Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
  138. }
  139. // LuckySheetDataConfigMerge 合并单元格设置
  140. type LuckySheetDataConfigMerge struct {
  141. Row int `json:"r" description:"行数"`
  142. Column int `json:"c" description:"列数"`
  143. Rs int `json:"rs" description:"合并的行数"`
  144. Cs int `json:"cs" description:"合并的列数"`
  145. }
  146. // LuckySheetDataConfigBorderInfo 单元格边框信息
  147. type LuckySheetDataConfigBorderInfo struct {
  148. BorderType string `json:"borderType" description:"边框类型 border-left | border-right | border-top | border-bottom | border-all | border-outside | border-inside | border-horizontal | border-vertical | border-none"`
  149. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  150. Range []struct {
  151. Column []int64 `json:"column" description:"行"`
  152. Row []int64 `json:"row" description:"列"`
  153. } `json:"range" description:"选区范围 range: 行列信息数组"`
  154. RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
  155. Style string `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  156. Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
  157. }
  158. // LuckySheetDataConfigBorderInfoCellValue 单元格边框信息(范围类型为:单个单元格)
  159. type LuckySheetDataConfigBorderInfoCellValue struct {
  160. B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
  161. L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
  162. R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
  163. T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
  164. ColIndex int64 `json:"col_index" description:"第几行"`
  165. RowIndex int64 `json:"row_index" description:"第几列"`
  166. }
  167. // LuckySheetDataConfigBorderInfoCell 单元格边框信息(cell类型)
  168. type LuckySheetDataConfigBorderInfoCell struct {
  169. Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
  170. Style int `description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  171. Sl interface{} `json:"style" description:"边框粗细 style: 1 Thin | 2 Hair | 3 Dotted | 4 Dashed | 5 DashDot | 6 DashDotDot | 7 Double | 8 Medium | 9 MediumDashed | 10 MediumDashDot | 11 MediumDashDotDot | 12 SlantedDashDot | 13 Thick ,和aspose.cells的getLineStyle()的值对应的话,需要自己做个转换,参考 aspose.cells"`
  172. }
  173. // LuckySheetCellData 单个单元格数据
  174. type LuckySheetCellData struct {
  175. Col int64 `json:"c" description:"列"`
  176. Row int64 `json:"r" description:"行"`
  177. Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
  178. }
  179. // LuckySheetDataValue 单元格内值的数据
  180. type LuckySheetDataValue struct {
  181. CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
  182. Value interface{} `json:"v" description:"原始值"`
  183. Monitor string `json:"m" description:"显示值"`
  184. Background string `json:"bg" description:"背景色,实例值:#fff000"`
  185. FontFamily int `description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  186. FF interface{} `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  187. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  188. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  189. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  190. Fontsize int `description:"字体大小,14"`
  191. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  192. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  193. VerticalType int ` description:"垂直对齐, 0 中间、1 上、2下"`
  194. Fs interface{} `json:"fs" description:"字体大小,14"`
  195. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  196. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  197. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  198. //TextRotate string `json:"tr" description:"竖排文字, 3"`
  199. //RotateText string `json:"rt" description:"文字旋转角度, 介于0~180之间的整数,包含0和180"`
  200. TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
  201. Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
  202. Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
  203. Function string `json:"f" description:"公式"`
  204. MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
  205. }
  206. // LuckySheetDataCellType 单元格值格式:文本、时间等
  207. type LuckySheetDataCellType struct {
  208. Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
  209. T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
  210. S []struct {
  211. FontFamily int `description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  212. FF interface{} `json:"ff" description:"字体,0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书 "`
  213. FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
  214. Fontsize int `description:"字体大小,14"`
  215. CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
  216. HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
  217. VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
  218. Fs interface{} `json:"fs" description:"字体大小,14"`
  219. Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
  220. Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
  221. Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
  222. Un int `json:"un" description:""`
  223. Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
  224. Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
  225. Value interface{} `json:"v" description:"原始值"`
  226. } `json:"s"`
  227. }
  228. // LuckySheetDataCellComment 批注
  229. type LuckySheetDataCellComment struct {
  230. Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
  231. Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
  232. Width int `json:"width" description:"批注框宽度"`
  233. Height int `json:"height" description:"批注框高度"`
  234. Value string `json:"value" description:"批注内容"`
  235. IsShow bool `json:"isshow" description:"是否显示批注"`
  236. }
  237. // GetLuckySheetData 获取LuckySheetData的结构体
  238. func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
  239. err = json.Unmarshal([]byte(jsonStr), &item)
  240. for k, v := range item.CellData {
  241. value := v.Value
  242. value.Fontsize = getIntValueByInterface(value.Fs)
  243. value.CancelLine = getIntValueByInterface(value.Cl)
  244. value.HorizontalType = getIntValueByInterface(value.Ht)
  245. value.VerticalType = getIntValueByInterface(value.Vt)
  246. value.FontFamily = getIntValueByInterface(value.FF)
  247. value.TextBeak = getIntValueByInterface(value.Tb)
  248. if len(value.CellType.S) > 0 {
  249. for kk, vv := range value.CellType.S {
  250. vv.Fontsize = getIntValueByInterface(vv.Fs)
  251. vv.CancelLine = getIntValueByInterface(vv.Cl)
  252. vv.HorizontalType = getIntValueByInterface(vv.Ht)
  253. vv.VerticalType = getIntValueByInterface(vv.Vt)
  254. vv.FontFamily = getIntValueByInterface(vv.FF)
  255. value.CellType.S[kk] = vv
  256. }
  257. }
  258. item.CellData[k].Value = value
  259. }
  260. if len(item.Config.BorderInfo) > 0 {
  261. for k, v := range item.Config.BorderInfo {
  262. v.Value.T.Style = getIntValueByInterface(v.Value.T.Style)
  263. v.Value.B.Style = getIntValueByInterface(v.Value.B.Style)
  264. v.Value.L.Style = getIntValueByInterface(v.Value.L.Style)
  265. v.Value.R.Style = getIntValueByInterface(v.Value.R.Style)
  266. item.Config.BorderInfo[k] = v
  267. }
  268. }
  269. return
  270. }
  271. func getIntValueByInterface(valInterface interface{}) (val int) {
  272. if valInterface == nil {
  273. return
  274. }
  275. switch reflect.TypeOf(valInterface).Kind() {
  276. case reflect.String:
  277. tmpValue := reflect.ValueOf(valInterface).String()
  278. tmpValInt, err := strconv.Atoi(tmpValue)
  279. if err != nil {
  280. val = 0
  281. } else {
  282. val = tmpValInt
  283. }
  284. case reflect.Int, reflect.Int32, reflect.Int64:
  285. tmpValue := reflect.ValueOf(valInterface).Int()
  286. val = int(tmpValue)
  287. }
  288. return
  289. }
  290. // TableData 表格数据
  291. type TableData struct {
  292. TableDataList [][]LuckySheetDataValue
  293. RowWidthList []float64
  294. RowHeightList []float64
  295. RemoveTopRow int `description:"移除表格上方的行数"`
  296. RemoveBottomRow int `description:"移除表格下方的行数"`
  297. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  298. RemoveRightColumn int `description:"移除表格右侧的列数"`
  299. MergeList []TableDataMerge `description:"合并数据列"`
  300. }
  301. // TableDataMerge 表格数据合并单元格配置
  302. type TableDataMerge struct {
  303. StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
  304. StartColumnIndex int `json:"start_column" description:"开始的列下标"`
  305. MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
  306. MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
  307. }
  308. // ToExcel 通过 TableData生成excel表格
  309. func (tableData TableData) ToExcel() (downloadFilePath string, err error) {
  310. //dir, err := os.Executable()
  311. //exPath := filepath.Dir(dir)
  312. //downloadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  313. downloadFilePath, err = getDownloadPath()
  314. if err != nil {
  315. return
  316. }
  317. xlsxFile := xlsx.NewFile()
  318. if err != nil {
  319. return
  320. }
  321. // 将单个sheet的数据写入到excel
  322. err = tableData.WriteExcelSheetData(xlsxFile, "sheet1")
  323. if err != nil {
  324. return
  325. }
  326. //return
  327. err = xlsxFile.Save(downloadFilePath)
  328. if err != nil {
  329. return
  330. }
  331. //randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  332. //downloadFileName := "即将到期客户数据_" + randStr + ".xlsx"
  333. //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  334. return
  335. }
  336. // WriteExcelSheetData 通过 TableData生成excel表格数据
  337. func (tableData TableData) WriteExcelSheetData(xlsxFile *xlsx.File, sheetName string) (err error) {
  338. style := xlsx.NewStyle()
  339. alignment := xlsx.Alignment{
  340. Horizontal: "center",
  341. Vertical: "center",
  342. WrapText: true,
  343. }
  344. style.Alignment = alignment
  345. style.ApplyAlignment = true
  346. sheet, err := xlsxFile.AddSheet(sheetName)
  347. if err != nil {
  348. return
  349. }
  350. for k, v := range tableData.RowWidthList {
  351. err = sheet.SetColWidth(k, k, v/10)
  352. if err != nil {
  353. return
  354. }
  355. }
  356. // 单元格高度配置列表
  357. lenRowHeight := len(tableData.RowHeightList)
  358. for index, v := range tableData.TableDataList {
  359. tmpRow := sheet.AddRow()
  360. //设置单元格高度
  361. if index < lenRowHeight && tableData.RowHeightList[index] > 0 {
  362. tmpRow.SetHeight(tableData.RowHeightList[index] / 2)
  363. }
  364. for _, cellInfo := range v {
  365. tmpStyle := new(xlsx.Style)
  366. //xlsx.Style{
  367. // Fill: xlsx.Fill{},
  368. // ApplyBorder: false,
  369. // ApplyFill: false,
  370. // ApplyFont: false,
  371. // NamedStyleIndex: nil,
  372. //}
  373. //fill := *NewFill("solid", "FF000000", "00FF0000")
  374. defaultFill := xlsx.DefaultFill()
  375. if cellInfo.Background != `` {
  376. defaultFill.PatternType = "solid"
  377. backgroundColor := cellInfo.Background
  378. // 这么做是为了避免传入的是RGB的格式(rgb(255, 255, 255))
  379. backgroundColor = getColor(backgroundColor)
  380. defaultFill.BgColor = strings.TrimPrefix(backgroundColor, "#")
  381. defaultFill.FgColor = strings.TrimPrefix(backgroundColor, "#")
  382. }
  383. tmpStyle.Fill = *defaultFill
  384. // 获取表格字体配置
  385. tmpStyle.Font = getExcelFontConf(cellInfo)
  386. //获取表格单元格排列配置
  387. tmpStyle.ApplyAlignment = true
  388. tmpStyle.Alignment = getExcelAlignmentConf(cellInfo)
  389. //边框配置
  390. tmpStyle.Border = xlsx.Border{
  391. Left: "thin",
  392. LeftColor: "000000",
  393. Right: "thin",
  394. RightColor: "000000",
  395. Top: "thin",
  396. TopColor: "000000",
  397. Bottom: "thin",
  398. BottomColor: "000000",
  399. }
  400. //tmpStyle.ApplyAlignment = true
  401. //tmpStyle.Alignment.WrapText = true
  402. tmpRow := tmpRow.AddCell()
  403. tmpRow.SetStyle(tmpStyle)
  404. valueStr := cellInfo.Monitor
  405. if valueStr == `` {
  406. //valueStr = fmt.Sprint(cellInfo.Value)
  407. if valueStr == `` && cellInfo.CellType.S != nil {
  408. //不是设置在单元格上面,而是设置在文本上
  409. for _, cellS := range cellInfo.CellType.S {
  410. valueStr += fmt.Sprint(cellS.Value)
  411. }
  412. }
  413. }
  414. //tmpRow.SetString(valueStr)
  415. switch cellInfo.CellType.Fa {
  416. case "General":
  417. if cellInfo.CellType.S != nil {
  418. tmpRow.SetString(valueStr)
  419. } else {
  420. tmpRow.SetValue(cellInfo.Value)
  421. }
  422. case "@":
  423. tmpRow.SetString(valueStr)
  424. default:
  425. tmpRow.SetString(valueStr)
  426. }
  427. if cellInfo.Function != `` {
  428. //xlsxFile.
  429. //xlsxFile.SetCellFormula
  430. tmpRow.SetFormula(cellInfo.Function)
  431. }
  432. //if cellInfo.Function != `` {
  433. // tmpRow.SetFormula(cellInfo.Function)
  434. //}
  435. }
  436. }
  437. for _, v := range tableData.MergeList {
  438. for k, cell := range sheet.Row(v.StartRowIndex).Cells {
  439. if v.StartColumnIndex == k {
  440. cell.Merge(v.MergeColumnNum, v.MergeRowNum)
  441. }
  442. }
  443. }
  444. return
  445. }
  446. // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
  447. func (item *LuckySheetData) GetTableDataByLuckySheetDataStr(isRemoveBlankCell bool) (selfTableData TableData, err error) {
  448. luckySheetCellDataList := item.CellData
  449. // 表格数据
  450. tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
  451. // 最大行,最大列
  452. var maxRow, maxCol int64
  453. for _, v := range luckySheetCellDataList {
  454. //fmt.Println("row:", v.Row, "=====col:", v.Col)
  455. if v.Row > maxRow { //最大行
  456. maxRow = v.Row
  457. }
  458. if v.Col > maxCol { //最大列
  459. maxCol = v.Col
  460. }
  461. var tmpRow map[int64]LuckySheetDataValue
  462. tmpRow, ok := tableDataMap[v.Row]
  463. if ok {
  464. tmpRow[v.Col] = v.Value
  465. } else {
  466. tmpRow = make(map[int64]LuckySheetDataValue)
  467. tmpRow[v.Col] = v.Value
  468. }
  469. tableDataMap[v.Row] = tmpRow
  470. }
  471. tableDataList := make([][]LuckySheetDataValue, 0)
  472. var i int64
  473. // 单元格宽度
  474. configColumnConf := item.Config.Columnlen
  475. rowWidthMap := make(map[int]float64)
  476. rowWidthList := make([]float64, 0) //
  477. // 单元格高度
  478. configRowConf := item.Config.Rowlen
  479. rowHeightList := make([]float64, 0) //
  480. for i = 0; i <= maxRow; i++ {
  481. //列
  482. tmpTableColDataList := make([]LuckySheetDataValue, 0)
  483. // 每个单元格的高度
  484. tmpHeight, ok := configRowConf[fmt.Sprint(i)]
  485. if !ok {
  486. tmpHeight = 0
  487. }
  488. rowHeightList = append(rowHeightList, tmpHeight)
  489. tmpRowData, ok := tableDataMap[i]
  490. // 如果没有该行数据,那么就默认添加空行数据处理
  491. if !ok {
  492. tmpRowData = make(map[int64]LuckySheetDataValue)
  493. }
  494. var j int64
  495. for j = 0; j <= maxCol; j++ {
  496. tmpTableColData, ok := tmpRowData[j]
  497. if !ok {
  498. tmpTableColData = LuckySheetDataValue{}
  499. }
  500. //单元格显示的数据处理
  501. tmpTableColData.Monitor = handleCellVal(tmpTableColData)
  502. tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
  503. // 每个单元格的宽度
  504. tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
  505. if !ok {
  506. tmpWidth = 0
  507. }
  508. rowIndex := len(tmpTableColDataList) - 1
  509. if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
  510. rowWidthList = append(rowWidthList, tmpWidth)
  511. }
  512. rowWidthMap[rowIndex] = tmpWidth
  513. }
  514. tableDataList = append(tableDataList, tmpTableColDataList)
  515. }
  516. tableDataMergeList := make([]TableDataMerge, 0)
  517. tableRemoveNum := TableRemoveNum{}
  518. // 数据处理,移除上下左右空行空列
  519. if isRemoveBlankCell {
  520. tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList = handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
  521. }
  522. // 表格数据
  523. {
  524. // 移除空白单元格信息
  525. selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
  526. selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
  527. selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
  528. selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
  529. }
  530. selfTableData.TableDataList = tableDataList
  531. selfTableData.RowWidthList = rowWidthList
  532. selfTableData.RowHeightList = rowHeightList
  533. selfTableData.MergeList = tableDataMergeList
  534. return
  535. }
  536. // TableRemoveNum 上下左右移除的空行空列数量
  537. type TableRemoveNum struct {
  538. RemoveTopRow int `description:"移除表格上方的行数"`
  539. RemoveBottomRow int `description:"移除表格下方的行数"`
  540. RemoveLeftColumn int `description:"移除表格左侧的列数"`
  541. RemoveRightColumn int `description:"移除表格右侧的列数"`
  542. }
  543. // handleTableDataList 表格数据处理(移除上下左右的空行空列)
  544. func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
  545. var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
  546. tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
  547. //总共多少行
  548. lenRow := len(tableDataList)
  549. //移除上下空行
  550. deleteRowIndexList := make([]int, 0)
  551. if lenRow > 0 {
  552. var flag = false
  553. // 移除上方空列
  554. for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
  555. isDelete := true
  556. for _, v := range tableDataList[rowIndex] {
  557. if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
  558. isDelete = false
  559. flag = true
  560. break
  561. }
  562. }
  563. if flag {
  564. break
  565. }
  566. if isDelete {
  567. removeTopRow++
  568. deleteRowIndexList = append(deleteRowIndexList, rowIndex)
  569. }
  570. }
  571. // 移除下方空行
  572. flag = false
  573. //尾部
  574. deleteBottomRowIndexList := make([]int, 0)
  575. // 数据要大于1行才会处理
  576. 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. tmpShowValue, err := strconv.ParseFloat(cell.Value, 64)
  1598. if err == nil {
  1599. switch styleConfig.Last {
  1600. case "nt":
  1601. // 先进行数字的百分比计算,然后保留小数点位数
  1602. percent := tmpShowValue * 100
  1603. if styleConfig.Decimal == nil {
  1604. continue
  1605. }
  1606. factor := math.Pow(10, float64(*styleConfig.Decimal))
  1607. rounded := math.Round(percent*factor) / factor
  1608. tmp.Monitor = fmt.Sprintf("%g%%", rounded)
  1609. case "decimal":
  1610. // 先保留小数点位数,再进行百分比计算
  1611. if styleConfig.Decimal == nil {
  1612. continue
  1613. }
  1614. factor := math.Pow(10, float64(*styleConfig.Decimal))
  1615. rounded := math.Round(tmpShowValue*factor) / factor
  1616. if styleConfig.Nt == "percent" {
  1617. percent := rounded * 100
  1618. var precisionStr string
  1619. if *styleConfig.Decimal > 2 {
  1620. precision := *styleConfig.Decimal - 2
  1621. precisionStr = strconv.FormatFloat(rounded, 'f', precision, 64)
  1622. } else {
  1623. precisionStr = strconv.FormatFloat(math.Round(percent), 'f', -1, 64)
  1624. }
  1625. tmp.Monitor = fmt.Sprintf("%s%%", precisionStr)
  1626. } else {
  1627. tmp.Monitor = fmt.Sprintf("%g", rounded)
  1628. }
  1629. }
  1630. }
  1631. }
  1632. dataCol = append(dataCol, tmp)
  1633. }
  1634. tableDataList = append(tableDataList, dataCol)
  1635. }
  1636. }
  1637. selfTableData.MergeList = mergeList
  1638. selfTableData.TableDataList = tableDataList
  1639. return
  1640. }