|
@@ -6,6 +6,8 @@ import (
|
|
|
"fmt"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
// ConvToLuckySheet 普通的excel转luckySheet数据
|
|
@@ -23,6 +25,8 @@ func ConvToLuckySheet(filePath string) (err error) {
|
|
|
sheetDataList := make([]SimpleLuckySheetData, 0)
|
|
|
// 获取所有sheet
|
|
|
sheetList := f.GetSheetList()
|
|
|
+ fmt.Println("读取完成后", time.Now().Format(utils.FormatDateTime))
|
|
|
+
|
|
|
for sheetIndex, sheetName := range sheetList {
|
|
|
sheetData, tmpErr := getLuckySheetData(f, sheetIndex, sheetName)
|
|
|
if tmpErr != nil {
|
|
@@ -31,11 +35,13 @@ func ConvToLuckySheet(filePath string) (err error) {
|
|
|
}
|
|
|
sheetDataList = append(sheetDataList, sheetData)
|
|
|
}
|
|
|
+
|
|
|
byteS, err := json.Marshal(sheetDataList)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
utils.FileLog.Info(string(byteS))
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -80,19 +86,20 @@ type SimpleLuckySheetDataConfig struct {
|
|
|
|
|
|
// SimpleLuckySheetCellData 单个单元格数据
|
|
|
type SimpleLuckySheetCellData struct {
|
|
|
- Col int64 `json:"c" description:"列"`
|
|
|
- Row int64 `json:"r" description:"行"`
|
|
|
- Value SimpleLuckySheetDataValue `json:"v" description:"单元格内值的数据"`
|
|
|
+ Col int64 `json:"c" description:"列"`
|
|
|
+ Row int64 `json:"r" description:"行"`
|
|
|
+ //Value SimpleLuckySheetDataValue `json:"v" description:"单元格内值的数据"`
|
|
|
+ Value interface{} `json:"v" description:"单元格内值的数据"`
|
|
|
}
|
|
|
|
|
|
// SimpleLuckySheetDataValue 单元格内值的数据
|
|
|
type SimpleLuckySheetDataValue struct {
|
|
|
CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
|
|
|
Value interface{} `json:"v" description:"原始值"`
|
|
|
- Monitor string `json:"m" description:"显示值"`
|
|
|
+ //Monitor string `json:"m" description:"显示值"`
|
|
|
//Fontsize int `description:"字体大小,14"`
|
|
|
- TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
|
|
|
- Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
|
|
|
+ //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:"合并单元格信息"`
|
|
@@ -111,126 +118,251 @@ func getLuckySheetData(f *excelize.File, sheetIndex int, sheetName string) (shee
|
|
|
CellData: cellData,
|
|
|
Config: SimpleLuckySheetDataConfig{},
|
|
|
}
|
|
|
+ fmt.Println("开始读取sheet数据:", time.Now().Format(utils.FormatDateTime))
|
|
|
rows, tmpErr := f.GetRows(sheetName)
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
+ fmt.Println("读取完sheet数据:", time.Now().Format(utils.FormatDateTime))
|
|
|
+ lenRow := len(rows)
|
|
|
+ fmt.Println("总共:", lenRow, "条数据")
|
|
|
+ if lenRow <= 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
//sheetData.Row = len(rows)
|
|
|
//sheetData.Column = len(Column)
|
|
|
|
|
|
// 最大单元格数
|
|
|
maxColumnIndex := 0
|
|
|
- mergeCellList, err := f.GetMergeCells(sheetName)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
- for _, v := range mergeCellList {
|
|
|
- // 左上角单元格位置
|
|
|
- cStartIndex, rStartIndex, tmpErr := excelize.CellNameToCoordinates(v.GetStartAxis())
|
|
|
- if tmpErr != nil {
|
|
|
- err = tmpErr
|
|
|
- return
|
|
|
- }
|
|
|
- // 右下角单元格位置
|
|
|
- cEndIndex, rEndIndex, tmpErr := excelize.CellNameToCoordinates(v.GetEndAxis())
|
|
|
- if tmpErr != nil {
|
|
|
- err = tmpErr
|
|
|
+ wg := sync.WaitGroup{}
|
|
|
+ wg.Add(1)
|
|
|
+ // 协程处理合并单元格
|
|
|
+ go func() {
|
|
|
+ defer func() {
|
|
|
+ wg.Done()
|
|
|
+ }()
|
|
|
+ mergeCellList, err := f.GetMergeCells(sheetName)
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- //fmt.Println(v.GetEndAxis())
|
|
|
- tmpLuckySheetDataConfigMerge := LuckySheetDataConfigMerge{
|
|
|
- Row: rStartIndex - 1,
|
|
|
- Column: cStartIndex - 1,
|
|
|
- Rs: rEndIndex - rStartIndex + 1,
|
|
|
- Cs: cEndIndex - cStartIndex + 1,
|
|
|
- }
|
|
|
- mergeData[fmt.Sprint(rStartIndex-1, "_", cStartIndex-1)] = tmpLuckySheetDataConfigMerge
|
|
|
- }
|
|
|
- sheetData.Config.Merge = mergeData
|
|
|
-
|
|
|
- colWidthMap := make(map[string]float64)
|
|
|
- for rIndex, row := range rows {
|
|
|
- for cIndex, colCell := range row {
|
|
|
- if rIndex == 0 {
|
|
|
- //colName, tmpErr := excelize.ColumnNumberToName(cIndex + 1)
|
|
|
- //if tmpErr != nil {
|
|
|
- // err = tmpErr
|
|
|
- // return
|
|
|
- //}
|
|
|
- //colWidth, tmpErr := f.GetColWidth(sheetName, colName)
|
|
|
- //if tmpErr != nil {
|
|
|
- // err = tmpErr
|
|
|
- // return
|
|
|
- //}
|
|
|
- //colWidthMap[fmt.Sprint(cIndex)] = emuToPx(colWidth)
|
|
|
- }
|
|
|
- if maxColumnIndex < cIndex {
|
|
|
- maxColumnIndex = cIndex
|
|
|
- }
|
|
|
- cellName, tmpErr := excelize.CoordinatesToCellName(cIndex+1, rIndex+1)
|
|
|
+ for _, v := range mergeCellList {
|
|
|
+ // 左上角单元格位置
|
|
|
+ cStartIndex, rStartIndex, tmpErr := excelize.CellNameToCoordinates(v.GetStartAxis())
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- cellType, tmpErr := f.GetCellType(sheetName, cellName)
|
|
|
+ // 右下角单元格位置
|
|
|
+ cEndIndex, rEndIndex, tmpErr := excelize.CellNameToCoordinates(v.GetEndAxis())
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
|
}
|
|
|
- cellFormula, tmpErr := f.GetCellFormula(sheetName, cellName)
|
|
|
- if tmpErr != nil {
|
|
|
- err = tmpErr
|
|
|
- return
|
|
|
+ //fmt.Println(v.GetEndAxis())
|
|
|
+ tmpLuckySheetDataConfigMerge := LuckySheetDataConfigMerge{
|
|
|
+ Row: rStartIndex - 1,
|
|
|
+ Column: cStartIndex - 1,
|
|
|
+ Rs: rEndIndex - rStartIndex + 1,
|
|
|
+ Cs: cEndIndex - cStartIndex + 1,
|
|
|
}
|
|
|
+ mergeData[fmt.Sprint(rStartIndex-1, "_", cStartIndex-1)] = tmpLuckySheetDataConfigMerge
|
|
|
+ }
|
|
|
+ sheetData.Config.Merge = mergeData
|
|
|
+ }()
|
|
|
|
|
|
- fmt.Println(cellName, ": ", "类型:", cellType, ",公式:", cellFormula, ",值", colCell, "\t")
|
|
|
+ colWidthMap := make(map[string]float64)
|
|
|
|
|
|
- var colCellIntfac interface{}
|
|
|
- cellTypeT := "g"
|
|
|
- //colCell = utils.Tof
|
|
|
- tmpDec, tmpErr := decimal.NewFromString(colCell)
|
|
|
- if tmpErr != nil {
|
|
|
- colCellIntfac = colCell
|
|
|
- } else {
|
|
|
- colCellIntfac, _ = tmpDec.Float64()
|
|
|
- cellTypeT = "n"
|
|
|
- }
|
|
|
+ // 每次分割就是5000条
|
|
|
+ splitNum := 500
|
|
|
+ splitLen := lenRow / splitNum
|
|
|
+ residue := lenRow % splitNum
|
|
|
+ if residue > 0 {
|
|
|
+ splitLen += 1
|
|
|
+ }
|
|
|
|
|
|
- if cellFormula != `` {
|
|
|
- cellFormula = `=` + cellFormula
|
|
|
-
|
|
|
- calcChainList = append(calcChainList, CalcChain{
|
|
|
- Col: int64(cIndex),
|
|
|
- Row: int64(rIndex),
|
|
|
- Index: sheetIndex,
|
|
|
- Func: []interface{}{true, colCell, cellFormula},
|
|
|
- Color: "w",
|
|
|
- Times: 0,
|
|
|
- })
|
|
|
- }
|
|
|
+ for i := 0; i < splitLen; i++ {
|
|
|
+ wg.Add(1)
|
|
|
|
|
|
- cellData = append(cellData, SimpleLuckySheetCellData{
|
|
|
- Col: int64(cIndex),
|
|
|
- Row: int64(rIndex),
|
|
|
- Value: SimpleLuckySheetDataValue{
|
|
|
- CellType: LuckySheetDataCellType{
|
|
|
+ startRow := i * splitNum
|
|
|
+ endRow := (i + 1) * splitNum
|
|
|
+ if i == splitLen-1 && residue > 0 {
|
|
|
+ endRow = lenRow
|
|
|
+ }
|
|
|
+
|
|
|
+ go func(currStartRow, currEndRow int) {
|
|
|
+ defer func() {
|
|
|
+ wg.Done()
|
|
|
+ }()
|
|
|
+
|
|
|
+ for rIndex := currStartRow; rIndex < currEndRow; rIndex++ {
|
|
|
+ row := rows[rIndex]
|
|
|
+ for cIndex, colCell := range row {
|
|
|
+ if rIndex == 0 {
|
|
|
+ //colName, tmpErr := excelize.ColumnNumberToName(cIndex + 1)
|
|
|
+ //if tmpErr != nil {
|
|
|
+ // err = tmpErr
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ //colWidth, tmpErr := f.GetColWidth(sheetName, colName)
|
|
|
+ //if tmpErr != nil {
|
|
|
+ // err = tmpErr
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ //colWidthMap[fmt.Sprint(cIndex)] = emuToPx(colWidth)
|
|
|
+ }
|
|
|
+ if maxColumnIndex < cIndex {
|
|
|
+ maxColumnIndex = cIndex
|
|
|
+ }
|
|
|
+ cellName, tmpErr := excelize.CoordinatesToCellName(cIndex+1, rIndex+1)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //cellType, tmpErr := f.GetCellType(sheetName, cellName)
|
|
|
+ //if tmpErr != nil {
|
|
|
+ // err = tmpErr
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+ cellFormula, tmpErr := f.GetCellFormula(sheetName, cellName)
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //fmt.Println(cellName, ": ", "类型:", cellType, ",公式:", cellFormula, ",值", colCell, "\t")
|
|
|
+
|
|
|
+ var colCellIntfac interface{}
|
|
|
+ cellTypeT := "g"
|
|
|
+ //colCell = utils.Tof
|
|
|
+ tmpDec, tmpErr := decimal.NewFromString(colCell)
|
|
|
+ if tmpErr != nil {
|
|
|
+ colCellIntfac = colCell
|
|
|
+ } else {
|
|
|
+ colCellIntfac, _ = tmpDec.Float64()
|
|
|
+ cellTypeT = "n"
|
|
|
+ }
|
|
|
+
|
|
|
+ if cellFormula != `` {
|
|
|
+ cellFormula = `=` + cellFormula
|
|
|
+
|
|
|
+ calcChainList = append(calcChainList, CalcChain{
|
|
|
+ Col: int64(cIndex),
|
|
|
+ Row: int64(rIndex),
|
|
|
+ Index: sheetIndex,
|
|
|
+ Func: []interface{}{true, colCell, cellFormula},
|
|
|
+ Color: "w",
|
|
|
+ Times: 0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ //CellType LuckySheetDataCellType `json:"ct" description:"单元格值格式:文本、时间等 "`
|
|
|
+ //Value interface{} `json:"v" description:"原始值"`
|
|
|
+ ////Monitor string `json:"m" description:"显示值"`
|
|
|
+ ////Fontsize int `description:"字体大小,14"`
|
|
|
+ ////TextBeak int `description:"文本换行, 0 截断、1溢出、2 自动换行"`
|
|
|
+ ////Tb interface{} `json:"tb" description:"文本换行, 0 截断、1溢出、2 自动换行"`
|
|
|
+ ////Ps LuckySheetDataCellComment `json:"ps" description:"批注"`
|
|
|
+ //Function string `json:"f" description:"公式"`
|
|
|
+
|
|
|
+ valueMap := make(map[string]interface{})
|
|
|
+ valueMap["ct"] = LuckySheetDataCellType{
|
|
|
Fa: "General",
|
|
|
T: cellTypeT,
|
|
|
- //S: nil,
|
|
|
- },
|
|
|
- Value: colCellIntfac,
|
|
|
- Monitor: colCell,
|
|
|
- //Background: "",
|
|
|
- //FontFamily: 0,
|
|
|
- //FontColor: "",
|
|
|
- Function: cellFormula,
|
|
|
- //MergeCell: LuckySheetDataConfigMerge{},
|
|
|
- },
|
|
|
- })
|
|
|
- }
|
|
|
+ }
|
|
|
+ valueMap["v"] = colCellIntfac
|
|
|
+ if cellFormula != `` {
|
|
|
+ valueMap["f"] = cellFormula
|
|
|
+ }
|
|
|
+ cellData = append(cellData, SimpleLuckySheetCellData{
|
|
|
+ Col: int64(cIndex),
|
|
|
+ Row: int64(rIndex),
|
|
|
+ Value: valueMap,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }(startRow, endRow)
|
|
|
}
|
|
|
+ wg.Wait()
|
|
|
+
|
|
|
+ fmt.Println("解析完sheet数据:", time.Now().Format(utils.FormatDateTime))
|
|
|
+ //for rIndex, row := range rows {
|
|
|
+ // for cIndex, colCell := range row {
|
|
|
+ // if rIndex == 0 {
|
|
|
+ // //colName, tmpErr := excelize.ColumnNumberToName(cIndex + 1)
|
|
|
+ // //if tmpErr != nil {
|
|
|
+ // // err = tmpErr
|
|
|
+ // // return
|
|
|
+ // //}
|
|
|
+ // //colWidth, tmpErr := f.GetColWidth(sheetName, colName)
|
|
|
+ // //if tmpErr != nil {
|
|
|
+ // // err = tmpErr
|
|
|
+ // // return
|
|
|
+ // //}
|
|
|
+ // //colWidthMap[fmt.Sprint(cIndex)] = emuToPx(colWidth)
|
|
|
+ // }
|
|
|
+ // if maxColumnIndex < cIndex {
|
|
|
+ // maxColumnIndex = cIndex
|
|
|
+ // }
|
|
|
+ // cellName, tmpErr := excelize.CoordinatesToCellName(cIndex+1, rIndex+1)
|
|
|
+ // if tmpErr != nil {
|
|
|
+ // err = tmpErr
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // //cellType, tmpErr := f.GetCellType(sheetName, cellName)
|
|
|
+ // //if tmpErr != nil {
|
|
|
+ // // err = tmpErr
|
|
|
+ // // return
|
|
|
+ // //}
|
|
|
+ // cellFormula, tmpErr := f.GetCellFormula(sheetName, cellName)
|
|
|
+ // if tmpErr != nil {
|
|
|
+ // err = tmpErr
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // //fmt.Println(cellName, ": ", "类型:", cellType, ",公式:", cellFormula, ",值", colCell, "\t")
|
|
|
+ //
|
|
|
+ // var colCellIntfac interface{}
|
|
|
+ // cellTypeT := "g"
|
|
|
+ // //colCell = utils.Tof
|
|
|
+ // tmpDec, tmpErr := decimal.NewFromString(colCell)
|
|
|
+ // if tmpErr != nil {
|
|
|
+ // colCellIntfac = colCell
|
|
|
+ // } else {
|
|
|
+ // colCellIntfac, _ = tmpDec.Float64()
|
|
|
+ // cellTypeT = "n"
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // if cellFormula != `` {
|
|
|
+ // cellFormula = `=` + cellFormula
|
|
|
+ //
|
|
|
+ // calcChainList = append(calcChainList, CalcChain{
|
|
|
+ // Col: int64(cIndex),
|
|
|
+ // Row: int64(rIndex),
|
|
|
+ // Index: sheetIndex,
|
|
|
+ // Func: []interface{}{true, colCell, cellFormula},
|
|
|
+ // Color: "w",
|
|
|
+ // Times: 0,
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // cellData = append(cellData, SimpleLuckySheetCellData{
|
|
|
+ // Col: int64(cIndex),
|
|
|
+ // Row: int64(rIndex),
|
|
|
+ // Value: SimpleLuckySheetDataValue{
|
|
|
+ // CellType: LuckySheetDataCellType{
|
|
|
+ // Fa: "General",
|
|
|
+ // T: cellTypeT,
|
|
|
+ // },
|
|
|
+ // Value: colCellIntfac,
|
|
|
+ // Monitor: colCell,
|
|
|
+ // Function: cellFormula,
|
|
|
+ // //MergeCell: LuckySheetDataConfigMerge{},
|
|
|
+ // },
|
|
|
+ // })
|
|
|
+ // }
|
|
|
+ //}
|
|
|
|
|
|
sheetData.Config.Columnlen = colWidthMap
|
|
|
|