/**
* Author:飘~
* [email protected]
* Date:2019/7/15
* Description:
*/
package main
import (
"bufio"
"fmt"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"github.com/tealeg/xlsx"
"log"
"os"
"strconv"
"strings"
"time"
)
type N_users struct {
Id int
A1 string
A2 string
A3 int
A4 string
A5 string
A6 string
A7 string
A8 int
A9 int
A10 int
A11 string
A12 int
A13 string
A14 string
A15 string
}
// go run main.go 1.xmlx Sheet1
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/excel?charset=utf8", 30)
orm.RegisterModel(new(N_users))
orm.RunSyncdb("default", false, true)
}
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: xlsx pathname sheetname")
os.Exit(1)
}
xlsxFile, err := xlsx.OpenFile(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sheet := xlsxFile.Sheet[os.Args[2]]
if sheet == nil {
fmt.Println("表单名不存在")
os.Exit(1)
}
cols := len(sheet.Cols)
sleet := make([][]string, cols)
oksleet := make([][]string, len(sheet.Rows)-1)
log.Println("列数:", cols, " 行数:", len(sheet.Rows))
var line int
//rows := len(sheet.Rows)
//for i := 0; i < cols; i++ {
// sleet[i] = append(sleet[i], []string)
//}
lname := []string{"栏目1", "栏目2", "栏目3", "栏目4", "栏目5", "栏目6", "栏目7", "栏目8"}
for _, title := range lname {
//title := getStdinInput("请输入列名:")
//if title == "" {
// fmt.Println(title)
// continue
//}
titleColIndex := findColByTitle(sheet, title)
if titleColIndex == -1 {
fmt.Println("列名不存在")
continue
}
rowLen := len(sheet.Rows)
result := []string{}
for rowIndex := 1; rowIndex < rowLen; rowIndex++ {
content := sheet.Cell(rowIndex, titleColIndex).String()
result = append(result, content)
sleet[line] = append(sleet[line], content)
//fmt.Println(sleet)
//if line == 8 {
// fmt.Println(sleet)
//}
}
log.Println("line:", line)
log.Println("--------------", cols)
if line == cols-1 {
log.Println("+++++++++++++")
for i := 0; i < len(sheet.Rows)-1; i++ {
for j := 0; j < cols; j++ {
oksleet[i] = append(oksleet[i], sleet[j][i])
//log.Println(oksleet[i])
}
}
//fmt.Println(oksleet)
file, err := os.OpenFile(strconv.Itoa(int(time.Now().Unix()))+"失败记录.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println("创建结果文件失败,代码:", err)
return
}
defer file.Close()
for _, v := range oksleet {
msg := N_users{}
msg.A1 = strings.Trim(v[1], " ")
msg.A2 = strings.Trim(v[2], " ")
msg.A3 = strings.Trim(v[0], " ") //大战区
msg.A4 = strings.Trim(v[3], " ")
msg.A5 = strings.Trim(v[4], " ")
msg.A6 = strings.Trim(v[5], " ")
msg.A7 = strings.Trim(v[6], " ")
msg.A8 = strings.Trim(v[7], " ")
msg.A9 = "excel6"
msg.A10 = 0
msg.A11 = 0
msg.A12 = int(time.Now().Unix())
//插入数据表
o := orm.NewOrm()
id, err := o.Insert(&msg)
if err == nil {
fmt.Println("写入成功,ID为:", id)
} else {
//fmt.Println("失败!", v[0], v[1], v[2], v[3], v[4])
file.WriteString(fmt.Sprintf("%s %s %s %s %s\n", v[0], v[1], v[3], v[4], v[5]))
}
}
}
line++
log.Println(line)
}
}
func getStdinInput(hint string) string {
fmt.Print(hint)
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return scanner.Text()
}
return ""
}
func findColByTitle(sheet *xlsx.Sheet, title string) int {
titleRow := sheet.Rows[0]
for titleIndex, col := range titleRow.Cells {
if col.String() == title {
return titleIndex
}
}
return -1
}