lucky_sheet.go 48 KB

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