golang读出excel并倒入到txt文件
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"os"
)
func ReadExcel(filename string) []map[string]string {
ret := []map[string]string{}
f, err := excelize.OpenFile(filename)
if err != nil {
fmt.Println("读取excel文件出错", err.Error())
return ret
}
sheets := f.GetSheetMap()
//fmt.Println(sheets)
sheet1 := sheets[1]
//fmt.Println("第一个工作表", sheet1)
rows := f.GetRows(sheet1)
cols := []string{}
for i, row := range rows {
if i == 0 { //取得第一行的所有数据---execel表头
for _, colCell := range row {
cols = append(cols, colCell)
}
//fmt.Println("列信息", cols)
} else {
theRow := map[string]string{}
for j, colCell := range row {
k := cols[j]
theRow[k] = colCell
}
ret = append(ret, theRow)
}
}
return ret
}
func main() {
//fruits.xlsx读取excel文件的名字
excel := ReadExcel("fruits.xlsx")
///Users/abc/GolandProjects/test/bb.txt生成txt文件的目录
file, err := os.OpenFile("/Users/abc/GolandProjects/test/bb.txt", os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Println("open file failed,err:", err)
return
}
for id := range excel {
curl := "curl http://acd/dd/dd/ddd -d '{\"id\": " + excel[id]["id"] + "'\r\n"
file.Write([]byte(curl)) //写入字节切片数据
//file.WriteString(curl) //直接写入字符串数据
}
}
项目目录结构
excel格式