lucky_sheet.go 46 KB

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