package main import ( "errors" "github.com/sirupsen/logrus" "github.com/xuri/excelize/v2" "os" "strconv" "strings" "time" ) var ( log = logrus.WithField("module", "main") excelHandle *excelize.File configFile = "config.txt" ids = []string{"20251204", "20251205", "20251208"} startId = 16127287 endId = 16127485 ) func init() { logrus.SetReportCaller(true) logrus.SetFormatter(&logrus.TextFormatter{ //以下设置只是为了使输出更美观 DisableColors: false, DisableTimestamp: false, TimestampFormat: "2006-01-02 15:04:05", }) //logger := &lumberjack.Logger{ // Filename: "./pest.log", // MaxSize: 500, // 日志文件大小,单位是 MB // MaxBackups: 3, // 最大过期日志保留个数 // MaxAge: 28, // 保留过期文件最大时间,单位 天 // LocalTime: true, // 使用本地时间 // Compress: true, // 是否压缩日志,默认是不压缩。这里设置为true,压缩日志 //} //logrus.SetOutput(logger) // logrus 设置日志的输出方式 file, err := os.ReadFile(configFile) if err != nil { log.Fatalf("Read config file error: %s", err.Error()) } lines := strings.Split(string(file), "\r\n") //for i := startId; i <= endId; i++ { // ids = append(ids, strconv.Itoa(i)) //} for _, line := range lines { if len(line) < 8 { continue } ids = append(ids, strings.TrimSpace(line)) } } func addSheetHeader(id string, row string) error { var tmp []interface{} tmp = append(tmp, "id") for i := 0; i < 21; i++ { tmp = append(tmp, "通用数据-"+strconv.Itoa(i)) } tmp = append(tmp, "更新时间") if err := excelHandle.SetSheetRow(id, row, &tmp); err != nil { log.Errorf("插入表格错误(%s):%v", id, err) return err } return nil } func putDataToSheet(id string, row string, data []SDData) error { var tmp []interface{} if len(data) != 21 { return errors.New("数据长度不对") } tmp = append(tmp, id) for _, d := range data { tmp = append(tmp, d.Data) } tmp = append(tmp, data[0].Time) if err := excelHandle.SetSheetRow(id, row, &tmp); err != nil { log.Errorf("插入表格错误(%s):%v", id, err) return err } return nil } func main() { var err error log.Info("start") //excelHandle, err = excelize.OpenFile("real.xlsx") //if err != nil { // log.Errorf("打开Excel失败", err) //} log.Info("新建 excel 表格") excelHandle = excelize.NewFile() defer excelHandle.Close() err = excelHandle.SaveAs(time.Now().Format("20060102150405") + ".xlsx") if err != nil { log.Errorf("保存文件失败%v", err) } //新建所有sheet for _, id := range ids { if _, err := excelHandle.NewSheet(id); err != nil { log.Errorf("新建表格错误(%s):%v", id, err) continue } if err := addSheetHeader(id, "A1"); err != nil { log.Errorf("增加表头错误(%s):%v", id, err) } } err = excelHandle.DeleteSheet("Sheet1") if err != nil { log.Error("删除默认sheet 失败", err) } //base := "http://dataupload.nongtt.com/devdata/GetSensorData?key=Nongtt0020251204" base := "http://dataupload.nongtt.com/devdata/GetSensorData?key=Nongtt00" col := 2 for true { for _, id := range ids { url := base + id log.WithField("id", id).Info("获取数据") data, err := FetchAndParseData(url) if err != nil { log.Errorf("获取数据失败%v", err) continue } log.WithField("id", id).Info("获取完成, 准备插入数据") //for i, datum := range data.Data { // var tmp []interface{} // tmp = append(tmp, datum.Key, datum.Addr, datum.Code, datum.Name, datum.Data, datum.Time) // // if err := excelHandle.SetSheetRow(id, "A"+strconv.Itoa(i+1), &tmp); err != nil { // log.Errorf("插入表格错误(%s):%v", id, err) // } //} if err := putDataToSheet(id, "A"+strconv.Itoa(col), data.Data); err != nil { log.Error("插入表格错误(%s):%v", id, err) continue } log.WithField("id", id).Info("插入数据 完成") } if err := excelHandle.Save(); err != nil { log.Error("保存文件失败%v", err) continue } log.Info("保存 完成", col) time.Sleep(time.Minute) col++ } }