123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701 |
- package excel
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage/excel/request"
- "eta/eta_api/utils"
- "fmt"
- "github.com/tealeg/xlsx"
- "github.com/xuri/excelize/v2"
- "os"
- "reflect"
- "strconv"
- "strings"
- "time"
- )
- type LuckySheetDataBak struct {
- CalcChain []interface{} `json:"calcChain" description:"公式链"`
- CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
- ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
- Config struct {
- BorderInfo []struct {
- BorderType string `json:"borderType" description:""`
- Color string `json:"color" description:""`
- Range []struct {
- Column []int64 `json:"column" description:""`
- Row []int64 `json:"row" description:""`
- } `json:"range" description:""`
- RangeType string `json:"rangeType" description:""`
- Style string `json:"style" description:""`
- Value struct {
- B struct {
- Color string `json:"color" description:""`
- Style string `json:"style" description:""`
- } `json:"b" description:""`
- ColIndex int64 `json:"col_index" description:""`
- L struct {
- Color string `json:"color" description:""`
- Style string `json:"style" description:""`
- } `json:"l" description:""`
- R struct {
- Color string `json:"color" description:""`
- Style string `json:"style" description:""`
- } `json:"r" description:""`
- RowIndex int64 `json:"row_index" description:""`
- T struct {
- Color string `json:"color" description:""`
- Style string `json:"style" description:""`
- } `json:"t" description:""`
- } `json:"value" description:"" description:""`
- } `json:"borderInfo" description:""`
- Colhidden struct{} `json:"colhidden" description:""`
- Columnlen map[string]float64 `json:"columnlen" description:""`
- CustomHeight struct {
- Zero int64 `json:"0"`
- } `json:"customHeight" description:""`
- CustomWidth struct {
- Two int64 `json:"2" description:""`
- } `json:"customWidth" description:""`
- Merge struct{} `json:"merge" description:""`
- Rowlen map[string]float64 `json:"rowlen" description:""`
- } `json:"config" description:""`
- Data [][]struct {
- Ct struct {
- Fa string `json:"fa"`
- T string `json:"t"`
- } `json:"ct"`
- M string `json:"m"`
- V interface{} `json:"v"`
- } `json:"data" description:""`
- DataVerification struct{} `json:"dataVerification" description:""`
- Filter interface{} `json:"filter" description:""`
- FilterSelect interface{} `json:"filter_select" description:""`
- Hyperlink struct{} `json:"hyperlink" description:""`
- Images struct{} `json:"images" description:""`
- Index string `json:"index" description:""`
- JfgirdSelectSave []interface{} `json:"jfgird_select_save" description:""`
- LuckysheetAlternateformatSave []interface{} `json:"luckysheet_alternateformat_save" description:""`
- LuckysheetConditionformatSave []interface{} `json:"luckysheet_conditionformat_save" description:""`
- LuckysheetSelectSave []struct {
- Column []int64 `json:"column" description:""`
- ColumnFocus int64 `json:"column_focus" description:""`
- Height int64 `json:"height" description:""`
- HeightMove int64 `json:"height_move" description:""`
- Left int64 `json:"left" description:""`
- LeftMove int64 `json:"left_move" description:""`
- Row []int64 `json:"row" description:""`
- RowFocus int64 `json:"row_focus" description:""`
- Top int64 `json:"top" description:""`
- TopMove int64 `json:"top_move" description:""`
- Width int64 `json:"width" description:""`
- WidthMove int64 `json:"width_move" description:""`
- } `json:"luckysheet_select_save" description:"" description:""`
- LuckysheetSelectionRange []struct {
- Column []int64 `json:"column" description:""`
- Row []int64 `json:"row" description:""`
- } `json:"luckysheet_selection_range" description:""`
- RhHeight float64 `json:"rh_height" description:""`
- ScrollLeft float64 `json:"scrollLeft" description:""`
- ScrollTop float64 `json:"scrollTop" description:""`
- Status int64 `json:"status" description:""`
- Visibledatacolumn []int64 `json:"visibledatacolumn" description:""`
- Visibledatarow []int64 `json:"visibledatarow" description:""`
- ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
- }
- // LuckySheetData sheet表格数据
- type LuckySheetData struct {
- Name string `json:"name" description:"工作表名称"`
- Index interface{} `json:"index" description:"工作表索引"`
- Row int `json:"row" description:"行数"`
- Column int `json:"column" description:"列数"`
- CellData []LuckySheetCellData `json:"celldata" description:"单元格数据"`
- ChWidth int64 `json:"ch_width" description:"工作表区域的宽度"`
- Config LuckySheetDataConfig `json:"config" description:""`
- //Index int `json:"index" description:"工作表索引"`
- RhHeight float64 `json:"rh_height" description:"工作表区域的高度"`
- ScrollLeft float64 `json:"scrollLeft" description:"左右滚动条位置"`
- ScrollTop float64 `json:"scrollTop" description:"上下滚动条位置"`
- CalcChain []interface{} `json:"calcChain" description:"公式链"`
- Status interface{} `json:"status" description:"激活状态"`
- VisibleDataColumn []int64 `json:"visibledatacolumn" description:"所有列的位置信息,递增的列位置数据,初始化无需设置"`
- VisibleDataRow []int64 `json:"visibledatarow" description:"所有行的位置信息,递增的行位置数据,初始化无需设置"`
- ZoomRatio float64 `json:"zoomRatio" description:"sheet缩放比例"`
- }
- // LuckySheetDataConfig sheet表单的配置
- type LuckySheetDataConfig struct {
- BorderInfo []LuckySheetDataConfigBorderInfo `json:"borderInfo" description:"边框"`
- Colhidden map[string]int64 `json:"colhidden" description:"隐藏列,示例值:\"colhidden\":{\"30\":0,\"31\":0}"`
- Columnlen map[string]float64 `json:"columnlen" description:"每个单元格的列宽"`
- //CustomHeight struct {
- // Zero int64 `json:"0"`
- //} `json:"customHeight" description:""`
- //CustomWidth struct {
- // Two int64 `json:"2" description:""`
- //} `json:"customWidth" description:""`
- Merge map[string]LuckySheetDataConfigMerge `json:"merge" description:"合并单元格"`
- Rowlen map[string]float64 `json:"rowlen" description:"每个单元格的行高"`
- }
- // LuckySheetDataConfigMerge 合并单元格设置
- type LuckySheetDataConfigMerge struct {
- Row int `json:"r" description:"行数"`
- Column int `json:"c" description:"列数"`
- Rs int `json:"rs" description:"合并的行数"`
- Cs int `json:"cs" description:"合并的列数"`
- }
- // LuckySheetDataConfigBorderInfo 单元格边框信息
- type LuckySheetDataConfigBorderInfo struct {
- 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"`
- Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
- Range []struct {
- Column []int64 `json:"column" description:"行"`
- Row []int64 `json:"row" description:"列"`
- } `json:"range" description:"选区范围 range: 行列信息数组"`
- RangeType string `json:"rangeType" description:"选区 rangeType: range | cell "`
- 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"`
- Value LuckySheetDataConfigBorderInfoCellValue `json:"value" description:"" description:"范围类型分单个单元格的数据"`
- }
- // LuckySheetDataConfigBorderInfoCellValue 单元格边框信息(范围类型为:单个单元格)
- type LuckySheetDataConfigBorderInfoCellValue struct {
- B LuckySheetDataConfigBorderInfoCell `json:"b" description:"下边框"`
- L LuckySheetDataConfigBorderInfoCell `json:"l" description:"左边框"`
- R LuckySheetDataConfigBorderInfoCell `json:"r" description:"右边框"`
- T LuckySheetDataConfigBorderInfoCell `json:"t" description:"上边框"`
- ColIndex int64 `json:"col_index" description:"第几行"`
- RowIndex int64 `json:"row_index" description:"第几列"`
- }
- // LuckySheetDataConfigBorderInfoCell 单元格边框信息(cell类型)
- type LuckySheetDataConfigBorderInfoCell struct {
- Color string `json:"color" description:"边框颜色 color: 16进制颜色值"`
- 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"`
- 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"`
- }
- // LuckySheetCellData 单个单元格数据
- type LuckySheetCellData struct {
- Col int64 `json:"c" description:"列"`
- Row int64 `json:"r" description:"行"`
- Value LuckySheetDataValue `json:"v" description:"单元格内值的数据"`
- }
- // LuckySheetDataValue 单元格内值的数据
- type LuckySheetDataValue struct {
- CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
- Value interface{} `json:"v" description:"原始值"`
- Monitor string `json:"m" description:"显示值"`
- Background string `json:"bg" description:"背景色,实例值:#fff000"`
- 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 华文隶书 "`
- 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 华文隶书 "`
- FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
- Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
- Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
- Fontsize int `description:"字体大小,14"`
- CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
- HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
- VerticalType int ` description:"垂直对齐, 0 中间、1 上、2下"`
- Fs interface{} `json:"fs" description:"字体大小,14"`
- Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
- Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
- Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
- //TextRotate string `json:"tr" description:"竖排文字, 3"`
- //RotateText string `json:"rt" description:"文字旋转角度, 介于0~180之间的整数,包含0和180"`
- TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
- Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
- Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
- Function string `json:"f" description:"公式"`
- MergeCell LuckySheetDataConfigMerge `json:"mc" description:"合并单元格信息"`
- }
- // LuckySheetDataCellType 单元格值格式:文本、时间等
- type LuckySheetDataCellType struct {
- Fa string `json:"fa" description:"格式名称,例如:“General”为自动格式"`
- T string `json:"t" description:"格式类型,例如:“n”为数字类型"`
- S []struct {
- 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 华文隶书 "`
- 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 华文隶书 "`
- FontColor string `json:"fc" description:"字体颜色,示例值:#fff000" `
- Fontsize int `description:"字体大小,14"`
- CancelLine int ` description:"删除线, 0 常规 、 1 删除线"`
- HorizontalType int `description:"水平对齐, 0 居中、1 左、2右"`
- VerticalType int `description:"垂直对齐, 0 中间、1 上、2下"`
- Fs interface{} `json:"fs" description:"字体大小,14"`
- Cl interface{} `json:"cl" description:"删除线, 0 常规 、 1 删除线"`
- Ht interface{} `json:"ht" description:"水平对齐, 0 居中、1 左、2右"`
- Vt interface{} `json:"vt" description:"垂直对齐, 0 中间、1 上、2下"`
- Un int `json:"un" description:""`
- Bold int `json:"bl" description:"粗体,0 常规 、 1加粗 "`
- Italic int `json:"it" description:"斜体,0 常规 、 1 斜体"`
- Value interface{} `json:"v" description:"原始值"`
- } `json:"s"`
- }
- // LuckySheetDataCellComment 批注
- type LuckySheetDataCellComment struct {
- Left int `json:"left" description:"批注框距离左边工作表边缘位置"`
- Top int `json:"top" description:"批注框距离上边工作表边缘位置"`
- Width int `json:"width" description:"批注框宽度"`
- Height int `json:"height" description:"批注框高度"`
- Value string `json:"value" description:"批注内容"`
- IsShow bool `json:"isshow" description:"是否显示批注"`
- }
- // GetLuckySheetData 获取LuckySheetData的结构体
- func GetLuckySheetData(jsonStr string) (item *LuckySheetData, err error) {
- err = json.Unmarshal([]byte(jsonStr), &item)
- for k, v := range item.CellData {
- value := v.Value
- value.Fontsize = getIntValueByInterface(value.Fs)
- value.CancelLine = getIntValueByInterface(value.Cl)
- value.HorizontalType = getIntValueByInterface(value.Ht)
- value.VerticalType = getIntValueByInterface(value.Vt)
- value.FontFamily = getIntValueByInterface(value.FF)
- value.TextBeak = getIntValueByInterface(value.Tb)
- if len(value.CellType.S) > 0 {
- for kk, vv := range value.CellType.S {
- vv.Fontsize = getIntValueByInterface(vv.Fs)
- vv.CancelLine = getIntValueByInterface(vv.Cl)
- vv.HorizontalType = getIntValueByInterface(vv.Ht)
- vv.VerticalType = getIntValueByInterface(vv.Vt)
- vv.FontFamily = getIntValueByInterface(vv.FF)
- value.CellType.S[kk] = vv
- }
- }
- item.CellData[k].Value = value
- }
- if len(item.Config.BorderInfo) > 0 {
- for k, v := range item.Config.BorderInfo {
- v.Value.T.Style = getIntValueByInterface(v.Value.T.Style)
- v.Value.B.Style = getIntValueByInterface(v.Value.B.Style)
- v.Value.L.Style = getIntValueByInterface(v.Value.L.Style)
- v.Value.R.Style = getIntValueByInterface(v.Value.R.Style)
- item.Config.BorderInfo[k] = v
- }
- }
- return
- }
- func getIntValueByInterface(valInterface interface{}) (val int) {
- if valInterface == nil {
- return
- }
- switch reflect.TypeOf(valInterface).Kind() {
- case reflect.String:
- tmpValue := reflect.ValueOf(valInterface).String()
- tmpValInt, err := strconv.Atoi(tmpValue)
- if err != nil {
- val = 0
- } else {
- val = tmpValInt
- }
- case reflect.Int, reflect.Int32, reflect.Int64:
- tmpValue := reflect.ValueOf(valInterface).Int()
- val = int(tmpValue)
- }
- return
- }
- // TableData 表格数据
- type TableData struct {
- TableDataList [][]LuckySheetDataValue
- RowWidthList []float64
- RowHeightList []float64
- RemoveTopRow int `description:"移除表格上方的行数"`
- RemoveBottomRow int `description:"移除表格下方的行数"`
- RemoveLeftColumn int `description:"移除表格左侧的列数"`
- RemoveRightColumn int `description:"移除表格右侧的列数"`
- MergeList []TableDataMerge `description:"合并数据列"`
- }
- // TableDataMerge 表格数据合并单元格配置
- type TableDataMerge struct {
- StartRowIndex int `json:"start_row_index" description:"开始的行下标"`
- StartColumnIndex int `json:"start_column" description:"开始的列下标"`
- MergeRowNum int `json:"merge_row_num" description:"合并的行数"`
- MergeColumnNum int `json:"merge_column_num" description:"合并的列数"`
- }
- // ToExcel 通过 TableData生成excel表格
- func (tableData TableData) ToExcel() (downloadFilePath string, err error) {
- //dir, err := os.Executable()
- //exPath := filepath.Dir(dir)
- //downloadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- downloadFilePath, err = getDownloadPath()
- if err != nil {
- return
- }
- xlsxFile := xlsx.NewFile()
- if err != nil {
- return
- }
- // 将单个sheet的数据写入到excel
- err = tableData.WriteExcelSheetData(xlsxFile, "sheet1")
- if err != nil {
- return
- }
- //return
- err = xlsxFile.Save(downloadFilePath)
- if err != nil {
- return
- }
- //randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- //downloadFileName := "即将到期客户数据_" + randStr + ".xlsx"
- //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
- return
- }
- // WriteExcelSheetData 通过 TableData生成excel表格数据
- func (tableData TableData) WriteExcelSheetData(xlsxFile *xlsx.File, sheetName string) (err error) {
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet(sheetName)
- if err != nil {
- return
- }
- for k, v := range tableData.RowWidthList {
- err = sheet.SetColWidth(k, k, v/10)
- if err != nil {
- return
- }
- }
- // 单元格高度配置列表
- lenRowHeight := len(tableData.RowHeightList)
- for index, v := range tableData.TableDataList {
- tmpRow := sheet.AddRow()
- //设置单元格高度
- if index < lenRowHeight && tableData.RowHeightList[index] > 0 {
- tmpRow.SetHeight(tableData.RowHeightList[index] / 2)
- }
- for _, cellInfo := range v {
- tmpStyle := new(xlsx.Style)
- //xlsx.Style{
- // Fill: xlsx.Fill{},
- // ApplyBorder: false,
- // ApplyFill: false,
- // ApplyFont: false,
- // NamedStyleIndex: nil,
- //}
- //fill := *NewFill("solid", "FF000000", "00FF0000")
- defaultFill := xlsx.DefaultFill()
- if cellInfo.Background != `` {
- defaultFill.PatternType = "solid"
- backgroundColor := cellInfo.Background
- // 这么做是为了避免传入的是RGB的格式(rgb(255, 255, 255))
- backgroundColor = getColor(backgroundColor)
- defaultFill.BgColor = strings.TrimPrefix(backgroundColor, "#")
- defaultFill.FgColor = strings.TrimPrefix(backgroundColor, "#")
- }
- tmpStyle.Fill = *defaultFill
- // 获取表格字体配置
- tmpStyle.Font = getExcelFontConf(cellInfo)
- //获取表格单元格排列配置
- tmpStyle.ApplyAlignment = true
- tmpStyle.Alignment = getExcelAlignmentConf(cellInfo)
- //边框配置
- tmpStyle.Border = xlsx.Border{
- Left: "thin",
- LeftColor: "000000",
- Right: "thin",
- RightColor: "000000",
- Top: "thin",
- TopColor: "000000",
- Bottom: "thin",
- BottomColor: "000000",
- }
- //tmpStyle.ApplyAlignment = true
- //tmpStyle.Alignment.WrapText = true
- tmpRow := tmpRow.AddCell()
- tmpRow.SetStyle(tmpStyle)
- valueStr := cellInfo.Monitor
- if valueStr == `` {
- //valueStr = fmt.Sprint(cellInfo.Value)
- if valueStr == `` && cellInfo.CellType.S != nil {
- //不是设置在单元格上面,而是设置在文本上
- for _, cellS := range cellInfo.CellType.S {
- valueStr += fmt.Sprint(cellS.Value)
- }
- }
- }
- //tmpRow.SetString(valueStr)
- switch cellInfo.CellType.Fa {
- case "General":
- if cellInfo.CellType.S != nil {
- tmpRow.SetString(valueStr)
- } else {
- tmpRow.SetValue(cellInfo.Value)
- }
- case "@":
- tmpRow.SetString(valueStr)
- default:
- tmpRow.SetString(valueStr)
- }
- if cellInfo.Function != `` {
- //xlsxFile.
- //xlsxFile.SetCellFormula
- tmpRow.SetFormula(cellInfo.Function)
- }
- //if cellInfo.Function != `` {
- // tmpRow.SetFormula(cellInfo.Function)
- //}
- }
- }
- for _, v := range tableData.MergeList {
- for k, cell := range sheet.Row(v.StartRowIndex).Cells {
- if v.StartColumnIndex == k {
- cell.Merge(v.MergeColumnNum, v.MergeRowNum)
- }
- }
- }
- return
- }
- // GetTableDataByLuckySheetDataStr 通过LuckySheet的string数据获取表格数据
- func (item *LuckySheetData) GetTableDataByLuckySheetDataStr(isRemoveBlankCell bool) (selfTableData TableData, err error) {
- luckySheetCellDataList := item.CellData
- // 表格数据
- tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
- // 最大行,最大列
- var maxRow, maxCol int64
- for _, v := range luckySheetCellDataList {
- //fmt.Println("row:", v.Row, "=====col:", v.Col)
- if v.Row > maxRow { //最大行
- maxRow = v.Row
- }
- if v.Col > maxCol { //最大列
- maxCol = v.Col
- }
- var tmpRow map[int64]LuckySheetDataValue
- tmpRow, ok := tableDataMap[v.Row]
- if ok {
- tmpRow[v.Col] = v.Value
- } else {
- tmpRow = make(map[int64]LuckySheetDataValue)
- tmpRow[v.Col] = v.Value
- }
- tableDataMap[v.Row] = tmpRow
- }
- tableDataList := make([][]LuckySheetDataValue, 0)
- var i int64
- // 单元格宽度
- configColumnConf := item.Config.Columnlen
- rowWidthMap := make(map[int]float64)
- rowWidthList := make([]float64, 0) //
- // 单元格高度
- configRowConf := item.Config.Rowlen
- rowHeightList := make([]float64, 0) //
- for i = 0; i <= maxRow; i++ {
- //列
- tmpTableColDataList := make([]LuckySheetDataValue, 0)
- // 每个单元格的高度
- tmpHeight, ok := configRowConf[fmt.Sprint(i)]
- if !ok {
- tmpHeight = 0
- }
- rowHeightList = append(rowHeightList, tmpHeight)
- tmpRowData, ok := tableDataMap[i]
- // 如果没有该行数据,那么就默认添加空行数据处理
- if !ok {
- tmpRowData = make(map[int64]LuckySheetDataValue)
- }
- var j int64
- for j = 0; j <= maxCol; j++ {
- tmpTableColData, ok := tmpRowData[j]
- if !ok {
- tmpTableColData = LuckySheetDataValue{}
- }
- //单元格显示的数据处理
- tmpTableColData.Monitor = handleCellVal(tmpTableColData)
- tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
- // 每个单元格的宽度
- tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
- if !ok {
- tmpWidth = 0
- }
- rowIndex := len(tmpTableColDataList) - 1
- if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
- rowWidthList = append(rowWidthList, tmpWidth)
- }
- rowWidthMap[rowIndex] = tmpWidth
- }
- tableDataList = append(tableDataList, tmpTableColDataList)
- }
- tableDataMergeList := make([]TableDataMerge, 0)
- tableRemoveNum := TableRemoveNum{}
- // 数据处理,移除上下左右空行空列
- if isRemoveBlankCell {
- tableDataList, tableRemoveNum, rowHeightList, rowWidthList, tableDataMergeList = handleTableDataList(tableDataList, item.Config.Merge, rowHeightList, rowWidthList)
- }
- // 表格数据
- {
- // 移除空白单元格信息
- selfTableData.RemoveTopRow = tableRemoveNum.RemoveTopRow
- selfTableData.RemoveBottomRow = tableRemoveNum.RemoveBottomRow
- selfTableData.RemoveLeftColumn = tableRemoveNum.RemoveLeftColumn
- selfTableData.RemoveRightColumn = tableRemoveNum.RemoveRightColumn
- }
- selfTableData.TableDataList = tableDataList
- selfTableData.RowWidthList = rowWidthList
- selfTableData.RowHeightList = rowHeightList
- selfTableData.MergeList = tableDataMergeList
- return
- }
- // TableRemoveNum 上下左右移除的空行空列数量
- type TableRemoveNum struct {
- RemoveTopRow int `description:"移除表格上方的行数"`
- RemoveBottomRow int `description:"移除表格下方的行数"`
- RemoveLeftColumn int `description:"移除表格左侧的列数"`
- RemoveRightColumn int `description:"移除表格右侧的列数"`
- }
- // handleTableDataList 表格数据处理(移除上下左右的空行空列)
- func handleTableDataList(tableDataList [][]LuckySheetDataValue, luckySheetDataConfigMergeList map[string]LuckySheetDataConfigMerge, rowHeightList, rowWidthList []float64) ([][]LuckySheetDataValue, TableRemoveNum, []float64, []float64, []TableDataMerge) {
- var removeTopRow, removeBottomRow, removeLeftColumn, removeRightColumn int //上下左右需要移除的空行空列
- tableDataMergeList := make([]TableDataMerge, 0) //待合并的单元格信息
- //总共多少行
- lenRow := len(tableDataList)
- //移除上下空行
- deleteRowIndexList := make([]int, 0)
- if lenRow > 0 {
- var flag = false
- // 移除上方空列
- for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
- isDelete := true
- for _, v := range tableDataList[rowIndex] {
- if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- removeTopRow++
- deleteRowIndexList = append(deleteRowIndexList, rowIndex)
- }
- }
- // 移除下方空行
- flag = false
- //尾部
- deleteBottomRowIndexList := make([]int, 0)
- // 数据要大于1行才会处理
- if len(tableDataList) > 1 {
- for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
- isDelete := true
- for _, v := range tableDataList[rowIndex] {
- if v.Monitor != `` {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
- removeBottomRow++
- }
- }
- }
- if len(deleteBottomRowIndexList) > 0 {
- deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
- }
- lenDeleteRow := len(deleteRowIndexList)
- if lenDeleteRow > 0 {
- for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
- //表格数据
- tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
- //表格高度
- rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
- }
- }
- }
- //移除左右空列
- deleteColumnIndexList := make([]int, 0)
- if len(tableDataList) > 0 {
- var flag = false
- lenColumn := len(tableDataList[0])
- // 移除左边空列
- for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
- isDelete := true
- for _, v := range tableDataList {
- //如果一列都没有,说明是上面几行是空行,没有数据
- if len(v) <= 0 {
- continue
- }
- if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- removeLeftColumn++
- deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
- }
- }
- // 移除右方空列
- flag = false
- //右边
- deleteTailColumnIndexList := make([]int, 0)
- // 数据要大于1列才会处理
- if lenColumn > 1 {
- for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
- isDelete := true
- for _, v := range tableDataList {
- //如果一列都没有,说明是上面几行是空行,没有数据
- if len(v) <= 0 {
- continue
- }
- if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
- removeRightColumn++
- }
- }
- }
- if len(deleteTailColumnIndexList) > 0 {
- deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
- }
- lenDeleteColumn := len(deleteColumnIndexList)
- if lenDeleteColumn > 0 {
- for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
- //表格数据
- for k, v := range tableDataList {
- tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
- }
- //表格宽度
- rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
- }
- }
- }
- //处理合并单元格
- for _, v := range luckySheetDataConfigMergeList {
- indexRow := v.Row - removeTopRow
- indexColumn := v.Column - removeLeftColumn
- tableDataMerge := TableDataMerge{
- StartRowIndex: indexRow,
- StartColumnIndex: indexColumn,
- MergeRowNum: v.Rs - 1,
- MergeColumnNum: v.Cs - 1,
- }
- tableDataMergeList = append(tableDataMergeList, tableDataMerge)
- }
- return tableDataList, TableRemoveNum{
- RemoveTopRow: removeTopRow,
- RemoveBottomRow: removeBottomRow,
- RemoveLeftColumn: removeLeftColumn,
- RemoveRightColumn: removeRightColumn,
- }, rowHeightList, rowWidthList, tableDataMergeList
- }
- // ToExcel 通过 luckySheetData生成excel表格
- func (item *LuckySheetData) ToExcel() (downloadFilePath string, err error) {
- tableData, err := item.GetTableDataByLuckySheetDataStr(true)
- if err != nil {
- return
- }
- downloadFilePath, err = tableData.ToExcel()
- return
- }
- // getDownloadPath 获取下载地址
- func getDownloadPath() (fpath string, err error) {
- dateDir := time.Now().Format("20060102")
- uploadDir := "static/xls/" + dateDir
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- return
- }
- randStr := utils.GetRandStringNoSpecialChar(28)
- fileName := randStr + ".xlsx"
- fpath = uploadDir + "/" + fileName
- return
- }
- // getExcelFontConf 获取表格字体配置
- func getExcelFontConf(cellInfo LuckySheetDataValue) xlsx.Font {
- fontSize := 10
- familyName := ``
- var isBold, isItalic, isUnderline bool
- // 字体
- {
- if cellInfo.Bold == 1 {
- isBold = true
- }
- if cellInfo.Italic == 1 {
- isItalic = true
- }
- if cellInfo.CancelLine == 1 {
- isUnderline = true
- }
- //if cellInfo.Fontsize != `` {
- // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
- // if convSize > 0 {
- // fontSize = convSize
- // }
- //}
- if cellInfo.Fontsize > 0 {
- fontSize = cellInfo.Fontsize
- }
- tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
- if !familyNameOk {
- tmpFamilyName = "宋体"
- }
- familyName = tmpFamilyName
- }
- return xlsx.Font{
- Size: fontSize,
- Name: familyName,
- //Family: v2.FontFamily,
- //Charset: 0,
- Color: strings.TrimPrefix(cellInfo.FontColor, "#"),
- Bold: isBold,
- Italic: isItalic,
- Underline: isUnderline,
- }
- }
- // getExcelAlignmentConf 获取表格单元格排列配置
- func getExcelAlignmentConf(cellInfo LuckySheetDataValue) xlsx.Alignment {
- //自动换行
- isWrapText := false
- if cellInfo.TextBeak == 2 {
- isWrapText = true
- }
- rotateText := 0
- //if cellInfo.RotateText != `` {
- // convSize, _ := strconv.Atoi(cellInfo.RotateText)
- // if convSize > 0 {
- // rotateText = convSize
- // }
- //}
- return xlsx.Alignment{
- Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
- //Indent: 0,
- //ShrinkToFit: false,
- TextRotation: rotateText,
- Vertical: LuckyVerticalMap[cellInfo.VerticalType],
- WrapText: isWrapText,
- }
- }
- // getExcelBorderConf 获取表格边框配置
- //func getExcelBorderConf(list []LuckySheetDataConfigBorderInfo) (v xlsx.Border) {
- // // 字体
- // for _, v := range list {
- // //v.BorderType
- // //v.Style
- //
- // if v.RangeType == "cell" {
- //
- // xlsx.Border{
- // Left: LuckyBorderMap[v.Value.L.Style],
- // LeftColor: strings.TrimPrefix(v.Value.L.Color, "#"),
- // Right: LuckyBorderMap[v.Value.R.Style],
- // RightColor: strings.TrimPrefix(v.Value.R.Color, "#"),
- // Top: LuckyBorderMap[v.Value.T.Style],
- // TopColor: strings.TrimPrefix(v.Value.T.Color, "#"),
- // Bottom: LuckyBorderMap[v.Value.B.Style],
- // BottomColor: strings.TrimPrefix(v.Value.B.Color, "#"),
- // }
- // } else {
- // xlsx.Border{
- // Left: LuckyBorderMap[v.Style],
- // LeftColor: strings.TrimPrefix(v.Color, "#"),
- // Right: LuckyBorderMap[v.Style],
- // RightColor: strings.TrimPrefix(v.Color, "#"),
- // Top: LuckyBorderMap[v.Style],
- // TopColor: strings.TrimPrefix(v.Color, "#"),
- // Bottom: LuckyBorderMap[v.Style],
- // BottomColor: strings.TrimPrefix(v.Color, "#"),
- // }
- // }
- // //v.Value.T.Style
- //
- // }
- //
- // return
- //}
- // LuckyFontFamilyMap 字体map
- var LuckyFontFamilyMap = map[int]string{
- 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: "华文隶书",
- }
- // LuckyBorderMap 边框map
- var LuckyBorderMap = map[string]string{
- "": "Thin",
- "1": "Thin",
- "2": "Hair",
- "3": "Dotted",
- "4": "Dashed",
- "5": "DashDot", //宋体(Song)、
- "6": "DashDotDot", // 黑体(ST Heiti)
- "7": "Double", //楷体(ST Kaiti),
- "8": "Medium", //仿宋(ST FangSong),
- "9": "MediumDashed", //新宋体(ST Song),
- "10": "MediumDashDot",
- "11": "MediumDashDotDot",
- "12": "SlantedDashDot",
- "13": "Thick",
- }
- // LuckyHorizontalMap 水平对齐map
- var LuckyHorizontalMap = map[int]string{
- //"": "general",
- 0: "center",
- 1: "left",
- 2: "right",
- }
- // LuckyVerticalMap 垂直对齐map
- var LuckyVerticalMap = map[int]string{
- //"": "center",
- 0: "center",
- 1: "top",
- //2: "bottom",
- }
- // GetColumnName Return the ColumnName
- func GetColumnName(rowNumber int, colNumber int) (colName string, err error) {
- if rowNumber < 1 || colNumber < 1 {
- err = errors.New("Row and Column Number can not be less than 1")
- return
- }
- dividend := colNumber
- var modulo int
- for dividend > 0 {
- modulo = (dividend - 1) % 26
- // Convert int to char
- colName = string(65+modulo) + colName
- dividend = (int)((dividend - modulo) / 26)
- }
- colName = colName + strconv.Itoa(rowNumber)
- return
- }
- // GetTableDataByLuckySheetDataStrBak 通过LuckySheet的string数据获取表格数据(备份:2022-08-23 10:30:32)
- func (item *LuckySheetData) GetTableDataByLuckySheetDataStrBak() (selfTableData TableData, err error) {
- luckySheetCellDataList := item.CellData
- // 表格数据
- tableDataMap := make(map[int64]map[int64]LuckySheetDataValue)
- // 最大行,最大列
- var maxRow, maxCol int64
- for _, v := range luckySheetCellDataList {
- //fmt.Println("row:", v.Row, "=====col:", v.Col)
- if v.Row > maxRow { //最大行
- maxRow = v.Row
- }
- if v.Col > maxCol { //最大列
- maxCol = v.Col
- }
- var tmpRow map[int64]LuckySheetDataValue
- tmpRow, ok := tableDataMap[v.Row]
- if ok {
- tmpRow[v.Col] = v.Value
- } else {
- tmpRow = make(map[int64]LuckySheetDataValue)
- tmpRow[v.Col] = v.Value
- }
- tableDataMap[v.Row] = tmpRow
- }
- tableDataList := make([][]LuckySheetDataValue, 0)
- var i int64
- // 单元格宽度
- configColumnConf := item.Config.Columnlen
- rowWidthMap := make(map[int]float64)
- rowWidthList := make([]float64, 0) //
- // 单元格高度
- configRowConf := item.Config.Rowlen
- rowHeightList := make([]float64, 0) //
- for i = 0; i <= maxRow; i++ {
- //列
- tmpTableColDataList := make([]LuckySheetDataValue, 0)
- // 每个单元格的高度
- tmpHeight, ok := configRowConf[fmt.Sprint(i)]
- if !ok {
- tmpHeight = 0
- }
- rowHeightList = append(rowHeightList, tmpHeight)
- tmpRowData, ok := tableDataMap[i]
- // 如果没有该行数据,那么就默认添加空行数据处理
- if !ok {
- tmpRowData = make(map[int64]LuckySheetDataValue)
- }
- var j int64
- for j = 0; j <= maxCol; j++ {
- tmpTableColData, ok := tmpRowData[j]
- if !ok {
- tmpTableColData = LuckySheetDataValue{}
- }
- tmpTableColDataList = append(tmpTableColDataList, tmpTableColData)
- // 每个单元格的宽度
- tmpWidth, ok := configColumnConf[fmt.Sprint(j)]
- if !ok {
- tmpWidth = 0
- }
- rowIndex := len(tmpTableColDataList) - 1
- if _, ok2 := rowWidthMap[rowIndex]; !ok2 {
- rowWidthList = append(rowWidthList, tmpWidth)
- }
- rowWidthMap[rowIndex] = tmpWidth
- }
- tableDataList = append(tableDataList, tmpTableColDataList)
- }
- //总共多少行
- lenRow := len(tableDataList)
- //移除上下空行
- deleteRowIndexList := make([]int, 0)
- if lenRow > 0 {
- var flag = false
- // 移除上方空列
- for rowIndex := 0; rowIndex < lenRow; rowIndex++ {
- isDelete := true
- for _, v := range tableDataList[rowIndex] {
- if v.Monitor != `` || (v.MergeCell.Row != rowIndex && v.MergeCell.Row != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- selfTableData.RemoveTopRow++
- deleteRowIndexList = append(deleteRowIndexList, rowIndex)
- }
- }
- // 移除下方空行
- flag = false
- //尾部
- deleteBottomRowIndexList := make([]int, 0)
- for rowIndex := lenRow - 1; rowIndex >= 0; rowIndex-- {
- isDelete := true
- for _, v := range tableDataList[rowIndex] {
- if v.Monitor != `` {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- deleteBottomRowIndexList = append(deleteBottomRowIndexList, rowIndex)
- selfTableData.RemoveBottomRow++
- }
- }
- if len(deleteBottomRowIndexList) > 0 {
- deleteRowIndexList = append(deleteRowIndexList, utils.RevSlice(deleteBottomRowIndexList)...)
- }
- lenDeleteRow := len(deleteRowIndexList)
- if lenDeleteRow > 0 {
- for rowIndex := lenDeleteRow - 1; rowIndex >= 0; rowIndex-- {
- //表格数据
- tableDataList = append(tableDataList[:deleteRowIndexList[rowIndex]], tableDataList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
- //表格高度
- rowHeightList = append(rowHeightList[:deleteRowIndexList[rowIndex]], rowHeightList[deleteRowIndexList[rowIndex]+1:]...) // 删除开头N个元素
- }
- }
- }
- //移除左右空列
- deleteColumnIndexList := make([]int, 0)
- if len(tableDataList) > 0 {
- var flag = false
- lenColumn := len(tableDataList[0])
- // 移除左边空列
- for columnIndex := 0; columnIndex < lenColumn; columnIndex++ {
- isDelete := true
- for _, v := range tableDataList {
- //如果一列都没有,说明是上面几行是空行,没有数据
- if len(v) <= 0 {
- continue
- }
- if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- selfTableData.RemoveLeftColumn++
- deleteColumnIndexList = append(deleteColumnIndexList, columnIndex)
- }
- }
- // 移除右方空列
- flag = false
- //右边
- deleteTailColumnIndexList := make([]int, 0)
- for columnIndex := lenColumn - 1; columnIndex >= 0; columnIndex-- {
- isDelete := true
- for _, v := range tableDataList {
- //如果一列都没有,说明是上面几行是空行,没有数据
- if len(v) <= 0 {
- continue
- }
- if v[columnIndex].Monitor != `` || (v[columnIndex].MergeCell.Column != columnIndex && v[columnIndex].MergeCell.Column != 0) {
- isDelete = false
- flag = true
- break
- }
- }
- if flag {
- break
- }
- if isDelete {
- deleteTailColumnIndexList = append(deleteTailColumnIndexList, columnIndex)
- selfTableData.RemoveRightColumn++
- }
- }
- if len(deleteTailColumnIndexList) > 0 {
- deleteColumnIndexList = append(deleteColumnIndexList, utils.RevSlice(deleteTailColumnIndexList)...)
- }
- lenDeleteColumn := len(deleteColumnIndexList)
- if lenDeleteColumn > 0 {
- for columnIndex := lenDeleteColumn - 1; columnIndex >= 0; columnIndex-- {
- //表格数据
- for k, v := range tableDataList {
- tableDataList[k] = append(v[:deleteColumnIndexList[columnIndex]], v[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
- }
- //表格宽度
- rowWidthList = append(rowWidthList[:deleteColumnIndexList[columnIndex]], rowWidthList[deleteColumnIndexList[columnIndex]+1:]...) // 删除开头N个元素
- }
- }
- }
- //处理合并单元格
- tableDataMergeList := make([]TableDataMerge, 0)
- for _, v := range item.Config.Merge {
- indexRow := v.Row - selfTableData.RemoveTopRow
- indexColumn := v.Column - selfTableData.RemoveLeftColumn
- tableDataMerge := TableDataMerge{
- StartRowIndex: indexRow,
- StartColumnIndex: indexColumn,
- MergeRowNum: v.Rs - 1,
- MergeColumnNum: v.Cs - 1,
- }
- tableDataMergeList = append(tableDataMergeList, tableDataMerge)
- }
- // 表格数据
- selfTableData.TableDataList = tableDataList
- selfTableData.RowWidthList = rowWidthList
- selfTableData.RowHeightList = rowHeightList
- selfTableData.MergeList = tableDataMergeList
- return
- }
- func (item *LuckySheetData) ToExcel2() (downloadFilePath string, err error) {
- tableData, err := item.GetTableDataByLuckySheetDataStr(true)
- downloadFilePath, err = getDownloadPath()
- if err != nil {
- return
- }
- f := excelize.NewFile()
- // Create a new sheet.
- sheetName := `Sheet1`
- sheetIndex, err := f.NewSheet(sheetName)
- if err != nil {
- return
- }
- //设置列宽度
- for k, v := range tableData.RowWidthList {
- colName, tmpErr := excelize.ColumnNumberToName(k + 1)
- if tmpErr != nil {
- err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
- err = tmpErr
- return
- }
- err = f.SetColWidth(sheetName, colName, colName, v/10)
- if err != nil {
- err = errors.New("设置列宽度,ERR:" + tmpErr.Error())
- return
- }
- }
- // 写入数据
- for index, v := range tableData.TableDataList {
- //设置单元格高度
- if tableData.RowHeightList[index] > 0 {
- tmpErr := f.SetRowHeight(sheetName, index+1, tableData.RowHeightList[index]/2)
- if tmpErr != nil {
- err = errors.New("设置单元格高度失败,ERR:" + tmpErr.Error())
- return
- }
- }
- for colunmIndex, cellInfo := range v {
- //f.NewStyle
- defaultFill := excelize.Fill{
- //Type: "solid",
- Pattern: 1,
- //Color: []string{cellInfo.Background},
- Shading: 0,
- }
- if cellInfo.Background != `` {
- defaultFill.Type = "solid"
- defaultFill.Color = []string{cellInfo.Background}
- }
- cellStyle := &excelize.Style{
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- //{Type: "diagonalDown", Color: "A020F0", Style: 7},
- //{Type: "diagonalUp", Color: "A020F0", Style: 8},
- },
- Fill: defaultFill,
- Font: getExcelizeFontConf(cellInfo),
- Alignment: getExcelizeAlignmentConf(cellInfo),
- //Protection: nil,
- //NumFmt: 0,
- //DecimalPlaces: 0,
- //CustomNumFmt: nil,
- //Lang: "",
- //NegRed: false,
- }
- cellStyleId, tmpErr := f.NewStyle(cellStyle)
- if tmpErr != nil {
- err = errors.New("new Style 失败,ERR:" + tmpErr.Error())
- return
- }
- columnName, tmpErr := excelize.ColumnNumberToName(colunmIndex + 1)
- if tmpErr != nil {
- err = errors.New("获取单元格列名失败,ERR:" + tmpErr.Error())
- return
- }
- cellName := columnName + strconv.Itoa(index+1)
- tmpErr = f.SetCellStyle(sheetName, cellName, cellName, cellStyleId)
- if tmpErr != nil {
- err = errors.New("设置style失败,ERR:" + tmpErr.Error())
- return
- }
- valueStr := cellInfo.Monitor
- if valueStr == `` {
- //valueStr = fmt.Sprint(cellInfo.Value)
- if valueStr == `` && cellInfo.CellType.S != nil {
- //不是设置在单元格上面,而是设置在文本上
- for _, cellS := range cellInfo.CellType.S {
- valueStr += fmt.Sprint(cellS.Value)
- }
- }
- }
- tmpErr = f.SetCellStr(sheetName, cellName, valueStr)
- if tmpErr != nil {
- err = errors.New("设置单元格数据失败,ERR:" + tmpErr.Error())
- return
- }
- }
- }
- for _, v := range tableData.MergeList {
- //开始列
- startColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + 1)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startCellName := startColumnName + strconv.Itoa(v.StartRowIndex+1)
- //结束列
- endColumnName, tmpErr := excelize.ColumnNumberToName(v.StartColumnIndex + v.MergeColumnNum + 1)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- endCellName := endColumnName + strconv.Itoa(v.StartRowIndex+v.MergeRowNum+1)
- err = f.MergeCell(sheetName, startCellName, endCellName)
- if err != nil {
- return
- }
- }
- // Set active sheet of the workbook.
- f.SetActiveSheet(sheetIndex)
- // Save spreadsheet by the given path.
- err = f.SaveAs(downloadFilePath)
- return
- }
- // getExcelizeFontConf 获取表格字体配置
- func getExcelizeFontConf(cellInfo LuckySheetDataValue) *excelize.Font {
- fontSize := 10
- familyName := ``
- //single 单线 、double 双线
- underlineStyle := ``
- var isBold, isItalic, isUnderline bool
- // 字体
- {
- if cellInfo.Bold == 1 {
- isBold = true
- }
- if cellInfo.Italic == 1 {
- isItalic = true
- }
- if cellInfo.CancelLine == 1 {
- isUnderline = true
- }
- //if cellInfo.Fontsize != `` {
- // convSize, _ := strconv.Atoi(cellInfo.Fontsize)
- // if convSize > 0 {
- // fontSize = convSize
- // }
- //}
- if cellInfo.Fontsize > 0 {
- fontSize = cellInfo.Fontsize
- }
- tmpFamilyName, familyNameOk := LuckyFontFamilyMap[cellInfo.FontFamily]
- if !familyNameOk {
- tmpFamilyName = "宋体"
- }
- familyName = tmpFamilyName
- }
- if isUnderline {
- underlineStyle = "single"
- }
- //VertAlign取值:
- //BASELINE、superscript、subscript
- return &excelize.Font{
- Bold: isBold,
- Italic: isItalic,
- Underline: underlineStyle,
- Family: familyName,
- Size: float64(fontSize),
- Strike: isUnderline,
- Color: cellInfo.FontColor,
- VertAlign: "",
- }
- }
- // getExcelizeAlignmentConf 获取表格单元格排列配置
- func getExcelizeAlignmentConf(cellInfo LuckySheetDataValue) *excelize.Alignment {
- //自动换行
- isWrapText := false
- if cellInfo.TextBeak == 2 {
- isWrapText = true
- }
- rotateText := 0
- //if cellInfo.RotateText != `` {
- // convSize, _ := strconv.Atoi(cellInfo.RotateText)
- // if convSize > 0 {
- // rotateText = convSize
- // }
- //}
- return &excelize.Alignment{
- Horizontal: LuckyHorizontalMap[cellInfo.HorizontalType],
- //Indent: 0,
- //JustifyLastLine: false,
- //ReadingOrder: 0,
- //RelativeIndent: 0,
- //ShrinkToFit: false,
- TextRotation: rotateText,
- Vertical: LuckyVerticalMap[cellInfo.VerticalType],
- WrapText: isWrapText,
- }
- }
- // getColor 获取hex颜色
- func getColor(bgStr string) string {
- if strings.Contains(bgStr, "(") {
- arr := strings.Split(bgStr, ",")
- if len(arr) != 3 {
- return bgStr
- }
- // 第一位
- tmpFirstArr := strings.Split(arr[0], "(")
- arr[0] = tmpFirstArr[len(tmpFirstArr)-1]
- // 最后一位
- tmpLastArr := strings.Split(arr[2], ")")
- arr[2] = tmpLastArr[0]
- rgbArr := make([]int64, 0)
- for _, v := range arr {
- tmpInt, err := strconv.Atoi(utils.TrimStr(v))
- if err != nil {
- return bgStr
- }
- rgbArr = append(rgbArr, int64(tmpInt))
- }
- rgb := RGB{
- rgbArr[0],
- rgbArr[1],
- rgbArr[2],
- }
- bgStr = "" + rgb.Rgb2Hex().Str
- }
- return bgStr
- }
- // GetTableDataByCustomData 通过自定义表格数据获取表格数据
- func GetTableDataByCustomData(excelType int, data request.TableDataReq) (selfTableData TableData, err error) {
- tableDataList := make([][]LuckySheetDataValue, 0)
- mergeList := make([]TableDataMerge, 0)
- // 指标数
- lenEdb := len(data.Data)
- if lenEdb <= 0 {
- return
- }
- // 日期数
- lenCol := len(data.Data[0].Data)
- if lenCol <= 0 {
- return
- }
- // 指标列:1;指标行:2
- if excelType == 1 {
- mergeList = append(mergeList, TableDataMerge{
- StartRowIndex: 0,
- StartColumnIndex: 0,
- MergeRowNum: 1,
- MergeColumnNum: 0,
- })
- // 第一行
- {
- firstCol := make([]LuckySheetDataValue, 0)
- firstCol = append(firstCol, LuckySheetDataValue{
- Value: "日期",
- Monitor: "日期",
- MergeCell: LuckySheetDataConfigMerge{
- Row: 0, //行数
- Column: 0, //列数
- Rs: 2, //合并的行数
- Cs: 1, //合并的列数
- },
- })
- for _, v := range data.Data {
- edbName := v.EdbName
- if v.EdbAliasName != `` {
- edbName = v.EdbAliasName
- }
- firstCol = append(firstCol, LuckySheetDataValue{
- Value: edbName,
- Monitor: edbName,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, firstCol)
- }
- // 第二行
- {
- secondCol := make([]LuckySheetDataValue, 0)
- secondCol = append(secondCol, LuckySheetDataValue{})
- for _, v := range data.Data {
- name := v.Unit + " / " + v.Frequency
- secondCol = append(secondCol, LuckySheetDataValue{
- Value: name,
- Monitor: name,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, secondCol)
- }
- // 开始数据了
- {
- for i := 0; i < lenCol; i++ {
- dataCol := make([]LuckySheetDataValue, 0)
- // 日期
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: data.Data[0].Data[i].DataTime,
- Monitor: data.Data[0].Data[i].DataTime,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- // 数据值
- for _, v := range data.Data {
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: v.Data[i].Value,
- Monitor: v.Data[i].ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, dataCol)
- }
- }
- // 开始文本行了
- {
- for _, textColList := range data.TextRowData {
- dataCol := make([]LuckySheetDataValue, 0)
- for _, v := range textColList {
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: v.Value,
- Monitor: v.ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, dataCol)
- }
- }
- } else {
- // 指标行
- mergeList = append(mergeList, TableDataMerge{
- StartRowIndex: 0,
- StartColumnIndex: 0,
- MergeRowNum: 0,
- MergeColumnNum: 1,
- })
- // 第一行
- {
- firstCol := make([]LuckySheetDataValue, 0)
- firstCol = append(firstCol, LuckySheetDataValue{
- Value: "日期",
- Monitor: "日期",
- MergeCell: LuckySheetDataConfigMerge{
- Row: 0, //行数
- Column: 0, //列数
- Rs: 1, //合并的行数
- Cs: 2, //合并的列数
- },
- })
- firstCol = append(firstCol, LuckySheetDataValue{})
- // 日期列
- for _, v := range data.Data[0].Data {
- firstCol = append(firstCol, LuckySheetDataValue{
- Value: v.DataTime,
- Monitor: v.DataTime,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- // 文本列
- for _, textColList := range data.TextRowData {
- firstCol = append(firstCol, LuckySheetDataValue{
- Value: textColList[0].Value,
- Monitor: textColList[0].ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, firstCol)
- }
- // 日期列+文本列+两列表头(日期这个文案表头)
- //colLen := lenCol+2+len(data.TextRowData)
- for i := 0; i < lenEdb; i++ {
- dataCol := make([]LuckySheetDataValue, 0)
- // 指标信息
- tmpEdbInfo := data.Data[i]
- // 指标名称
- {
- edbName := tmpEdbInfo.EdbName
- if tmpEdbInfo.EdbAliasName != `` {
- edbName = tmpEdbInfo.EdbAliasName
- }
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: edbName,
- Monitor: edbName,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- // 指标单位、频度
- {
- name := tmpEdbInfo.Unit + " / " + tmpEdbInfo.Frequency
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: name,
- Monitor: name,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- // 指标数据列
- for _, tmpData := range tmpEdbInfo.Data {
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: tmpData.Value,
- Monitor: tmpData.ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- // 文本列
- for _, textColList := range data.TextRowData {
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: textColList[i+1].Value,
- Monitor: textColList[i+1].ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, dataCol)
- }
- }
- selfTableData.MergeList = mergeList
- selfTableData.TableDataList = tableDataList
- return
- }
- // GetTableDataByMixedTableData 通过混合表格数据获取表格数据
- func GetTableDataByMixedTableData(config [][]request.MixedTableCellDataReq) (selfTableData TableData, err error) {
- tableDataList := make([][]LuckySheetDataValue, 0)
- mergeList := make([]TableDataMerge, 0)
- // 开始文本行了
- {
- for _, row := range config {
- dataCol := make([]LuckySheetDataValue, 0)
- for _, cell := range row {
- dataCol = append(dataCol, LuckySheetDataValue{
- Value: cell.Value,
- Monitor: cell.ShowValue,
- MergeCell: LuckySheetDataConfigMerge{},
- })
- }
- tableDataList = append(tableDataList, dataCol)
- }
- }
- selfTableData.MergeList = mergeList
- selfTableData.TableDataList = tableDataList
- return
- }
|