func ReadXlsx(c []CmdbTest, SheetName string) error {
//打开文件,如果文件不存在创建,存在就打开
path := "./cmdb_test.xlsx"
b, err := PathExists(path)
if err != nil {
fmt.Sprintln(err)
}
if !b {
files := xlsx.NewFile()
//第一次创建文件时创建sheet
alert := []string{"项目名称", "测试接口名称", "测试参数", "测试条数", "总耗时", "评均耗时", "最大耗时", "最小耗时"}
sheet, _ := files.AddSheet(SheetName)
row := sheet.AddRow()
for _, a := range alert {
row.AddCell().SetString(a)
}
err := files.Save(path)
if err != nil {
fmt.Println("创建xlsx文件发生错误")
return err
}
}
file, err := xlsx.OpenFile(path)
if err != nil {
fmt.Println("打开xlsx文件发生异常")
return err
}
//判断结构体sheetName是否存在。
br, err := SheetExists(path, SheetName)
if err != nil {
fmt.Println(err)
return err
}
if !br {
//追加创建新sheet
alert := []string{"项目名称", "测试接口名称", "测试参数", "测试条数", "总耗时", "评均耗时", "最大耗时", "最小耗时"}
sheet, _ := file.AddSheet(SheetName)
row := sheet.AddRow()
for _, a := range alert {
row.AddCell().SetString(a)
}
err := file.Save(path)
if err != nil {
fmt.Println(err)
return err
}
}
//数据录入
first, _ := file.Sheet[SheetName]
for _, v := range c {
ls := []string{v.PMName, v.InterfaceName, v.Type, v.TestNumber, v.SumTime, v.MeanTime, v.MaxTime, v.MinTime}
row := first.AddRow()
for _, r := range ls {
cell := row.AddCell()
cell.Value = r
err = file.Save(path)
if err != nil {
fmt.Println("写入信息发生异常,文件正在使用")
return err
}
}
}
return nil
}