lucky_sheet.go 52 KB

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