package services import ( "context" "encoding/json" "eta/eta_crawler/models" "eta/eta_crawler/services/alarm_msg" "eta/eta_crawler/utils" "fmt" "github.com/xuri/excelize/v2" "time" ) func RefreshMeetingProbabilities(cont context.Context) (err error) { defer func() { if err != nil { fmt.Println("RefreshMeetingProbabilities Err:" + err.Error()) msg := "失败提醒" + "RefreshMeetingProbabilities ErrMsg:" + err.Error() go alarm_msg.SendAlarmMsg(msg, 3) //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCoalJsm ErrMsg:"+err.Error(), utils.EmailSendToUsers) } }() templatePath := "./static/%s.xlsx" //templatePath := "/Users/xi/Desktop/%s.xlsx" templatePath = fmt.Sprintf(templatePath, time.Now().Format(utils.FormatDate)) fmt.Println(templatePath) f, tmpErr := excelize.OpenFile(templatePath) if tmpErr != nil { fmt.Println("打开文件失败,Err:" + tmpErr.Error()) err = tmpErr return } defer func() { // 关闭工作簿 if err := f.Close(); err != nil { fmt.Println(err) } }() // 获取工作表中指定单元格的值 cell, err := f.GetCellValue("MEETING PROBABILITIES", "B5") if err != nil { fmt.Println("GetCellValue err:" +err.Error()) return } fmt.Println(cell) // 获取 Sheet1 上所有单元格 rows, err := f.GetRows("MEETING PROBABILITIES") if err != nil { fmt.Println("GetRows err:" +err.Error()) fmt.Println(err) return } minCowLength := 60 minRowlength := 84 realCowLength := minCowLength realRowLength := minRowlength data := make([][]interface{}, 0) var null []struct{} cellData := make([]models.ISheetCellData, 0) var tmpCt models.ISheetDataCt tmpCt.Fa = "General" tmpCt.T = "g" for r, row := range rows { lenCow := len(row) var dataRow []interface{} for c, colCell := range row { if colCell == "" { dataRow = append(dataRow, null) } else { var tmp models.ISheetData tmp.M = colCell tmp.Ct = tmpCt tmp.V = colCell dataRow = append(dataRow, tmp) var tmpS models.ISheetCellData tmpS.R = r tmpS.C = c tmpS.V = tmp cellData = append(cellData, tmpS) } fmt.Print(colCell, "\t") } if lenCow < minCowLength { lastCow := minCowLength - lenCow for i := 0; i < lastCow; i++ { dataRow = append(dataRow, null) } } realCowLength = len(dataRow) data = append(data, dataRow) } lenRow := len(rows) //补充剩余的单元格 if lenRow < minRowlength { lastRow := minRowlength - lenRow for i := 0; i < lastRow; i++ { var dataRow []interface{} for j := 0; j < minCowLength; j++ { dataRow = append(dataRow, null) } data = append(data, dataRow) } } realRowLength = len(data) //visibleDataRow var visibleDataRow []int tmpVdr := 0 for i := 1; i <= realRowLength; i++ { tmpVdr = i * 20 visibleDataRow = append(visibleDataRow, tmpVdr) } var visibleDataColumn []int tmpVdr = 0 for i := 1; i <= realCowLength; i++ { tmpVdr = i * 74 visibleDataColumn = append(visibleDataColumn, tmpVdr) } sheet := new(models.ISheet) sheet.ScrollTop = 0 sheet.ScrollLeft = 0 sheet.Index = "Sheet_pekSifat0Kci_1694244624851" sheet.Status = 1 sheet.JfGirdSelectSave = []struct{}{} sheet.LuckySheetSelectSave = []struct{}{} sheet.Data = data sheet.Config = make(map[string]struct{}, 0) sheet.VisibleDataRow = visibleDataRow sheet.VisibleDataColumn = visibleDataColumn sheet.ChWidth = 4560 sheet.RhHeight = 1760 sheet.LuckySheetSelectionRange = make([]int, 0) sheet.ZoomRatio = 1 sheet.CellData = cellData var content []byte content, err = json.Marshal(sheet) if err != nil { return } var req models.AddExcelInfoReq excelInfo := &models.MeetingProbabilities{ //MeetingInfoId: 0, Content: string(content), ExcelImage: req.ExcelImage, DateTime: time.Now().Format(utils.FormatDate), IsDelete: 0, ModifyTime: time.Now(), CreateTime: time.Now(), } err = models.AddMeetingProbabilities(excelInfo) return }