golang xlsx

package main

import (
    "net/http"
    "io/ioutil"
    "fmt"
    //"encoding/json"
    "net/url"
    "git.oschina.net/xujiang/rongapi-common/utils"
    "github.com/tealeg/xlsx"
    "encoding/json"
    "time"
)

const (
    RemoteUrl = "http://api.jkcredit.com:8080/mobile?"
    Appid     = "jiaokeyuan"
    Key       = "bedb9297c7bde73d74364ec3d9b4d3ae"
)

type request struct {
    Name   string //姓名
    IdCode string //身份证号
    Phone  string //手机号
}

func main() {
    uns := time.Now().UnixNano()
    fmt.Println(uns ) //纳秒
    fmt.Println(uns / 1e6) //纳秒转毫秒
    fmt.Println(uns / 1e9) //纳秒转秒
    newfile := xlsx.NewFile()
    newfileSheet, _ := newfile.AddSheet("Sheet1")
    newfileSheetRow := newfileSheet.AddRow()
    newfileSheetRowCell := newfileSheetRow.AddCell()
    newfileSheetRowCell.Value = "idcard"
    newfileSheetRowCell.Row.AddCell().Value = "mobile"
    newfileSheetRowCell.Row.AddCell().Value = "name"
    newfileSheetRowCell.Row.AddCell().Value = "code"
    newfileSheetRowCell.Row.AddCell().Value = "message"
    newfileSheetRowCell.Row.AddCell().Value = "流水号"
    newfileSheetRowCell.Row.AddCell().Value = "耗时"


    xlsxFile, _ := xlsx.OpenFile("E:\\apihub\\接口文档\\运营商三要素核查接口文档内部对外\\测试0514.xlsx")

    for _, sheet := range xlsxFile.Sheets {
        for _, row := range sheet.Rows {
            if row.Cells[0].String() == "idcard" || row.Cells[1].String() == "mobile" || row.Cells[2].String() == "name" {
                continue
            }
            fmt.Println(row.Cells[0].String(), row.Cells[1].String(), row.Cells[2].String())
            r := request{row.Cells[2].String(), row.Cells[0].String(), row.Cells[1].String()}
            codeMessage := requesturl(&r)
            newfileSheetRow = newfileSheet.AddRow()
            newfileSheetRowCell = newfileSheetRow.AddCell()
            newfileSheetRowCell.Value = codeMessage.Request.IdCode
            newfileSheetRowCell.Row.AddCell().Value = codeMessage.Request.Phone
            newfileSheetRowCell.Row.AddCell().Value = codeMessage.Request.Name
            newfileSheetRowCell.Row.AddCell().Value = codeMessage.ResultCode
            newfileSheetRowCell.Row.AddCell().Value = codeMessage.ResultMsg
            newfileSheetRowCell.Row.AddCell().Value = codeMessage.TransactionID
            newfileSheetRowCell.Row.AddCell().Value = fmt.Sprint(codeMessage.DTime)

        }
    }
    err := newfile.Save("E:\\apihub\\接口文档\\运营商三要素核查接口文档内部对外\\MyXLSXFile.xlsx")
    fmt.Println(err)
}

func requesturl(r *request) *CodeMessage {
    startTime := time.Now().UnixNano()
    //r := request{}
    //r.Name = "冯伟强"
    //r.IdCode = "410182199403080330"
    //r.Phone = "17698083596"
    values := make(url.Values, 0)
    values.Add("Appid", Appid)
    values.Add("Name", r.Name)
    values.Add("Phone", r.Phone)
    values.Add("IdCode", r.IdCode)
    md5 := utils.Md5(r.IdCode + r.Name + r.Phone + Key)
    values.Add("Sign", md5)

    if resp, err := http.Post(RemoteUrl+values.Encode(),
        "", nil); err == nil {
        defer resp.Body.Close()
        resultbyte, _ := ioutil.ReadAll(resp.Body)
        endTime := time.Now().UnixNano()
        //log.Println(string(resultbyte))
        fmt.Println(string(resultbyte))

        codeMessage := new(CodeMessage)
        if err = json.Unmarshal(resultbyte, codeMessage); err == nil {
            codeMessage.DTime = endTime - startTime
            codeMessage.Request = *r
            return codeMessage
        }

    } else {
        return nil
    }
    return nil
}

type CodeMessage struct {
    ResultCode    string
    ResultMsg     string
    TransactionID string
    DTime         int64 //差值
    Request       request
}

你可能感兴趣的:(golang xlsx)