Golang中国
func w_excel(output_file string, ip string) {
    var tstr []string
    var tmpstr string
    var xlsxFile *xlsx.File
    var sheet *xlsx.Sheet
    xlsxFile = xlsx.NewFile()
    sheet, _ = xlsxFile.AddSheet("Sheet1")
    input_file := "idlist.txt"
    tstr = read_file(input_file)
    if len(tstr) == 0 {
        fmt.Println("File is empty...")
        os.Exit(3)
    } else {
        tmpstr = strings.Join(tstr[1:], ",")
    }
    tab_arr := get_tab(ip)
    for i := range tab_arr {
        tsql := bytes.Buffer{}
        tsql.WriteString("SELECT * FROM " + tab_arr[i] + " where id in (")
        tsql.WriteString(tmpstr)
        tsql.WriteString(")")
        myDB = SetDB(ip)
        defer myDB.Close()
        rows, err := myDB.Query(tsql.String())
        defer rows.Close()
        if err != nil {
            fmt.Println(err.Error())
            continue
        }
        columns, err := rows.Columns()
        if err != nil {
            fmt.Println(err.Error())
            return
        }
        values := make([]sql.RawBytes, len(columns))
        scanArgs := make([]interface{}, len(values))
        for i := range values {
            scanArgs[i] = &values[i]
        }
        for rows.Next() {
            err = rows.Scan(scanArgs...)
            if err != nil {
                fmt.Println(err.Error())
                return
            }
            row := sheet.AddRow()
            var value string
            for _, col := range values {
                if col == nil {
                    value = "NULL"
                } else {
                    value = string(col)
                }
                cell := row.AddCell()
                cell.Value = value
                err = xlsxFile.Save(output_file)
                if err != nil {
                    fmt.Println(err.Error())
                    return
                }
            }
        }
        if err = rows.Err(); err != nil {
            fmt.Println(err.Error())
            return
        }
    }

func main() {
    ips := []string{"xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx"}
    output_file := "userinfo"
    runtime.GOMAXPROCS(runtime.NumCPU())
    const (
        GOROUTINE_COUNT = 20
        TASK_COUNT      = 20
    )
    chReq := make(chan string, GOROUTINE_COUNT)
    chRes := make(chan int, GOROUTINE_COUNT)
    for i := 0; i < GOROUTINE_COUNT; i++ {
        go func() {
            for {
                url := <-chReq
                fmt.Println(url)
                w_excel(output_file+url+".xlsx", url)
                chRes <- 0
            }
        }()
    }
    go func() {
        urls := make([]string, TASK_COUNT)
        for i := 0; i < TASK_COUNT; i++ {
            urls[i] = ips[i]
        }
        for i := 0; i < TASK_COUNT; i++ {
            chReq <- urls[i]
        }
    }()
    for i := 0; i < TASK_COUNT; i++ {
        d := <-chRes
        _ = d
    }
}

想要实现多个数据库并行导出excel,但是遇到问题:
提示表不存在,但表确实是存在的,是什么原因


jetora 于 2016-10-28 13:44 修改
1 回复
jetora
#1 jetora • 2016-11-02 12:56

问题已解决,是变量作用域的问题

需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。