需求:
把exel表格中的内容,导入到sqlite3数据中,就行数据的求
- 交集
- 差集
- 并集
最后在保存到excel表格中;
数据库表结
package main
import (
"database/sql"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
_ "github.com/mattn/go-sqlite3"
"log"
"time"
)
//定义数据库的全局变量信息
const (
dbDriverName = "sqlite3"
dbname = "./datacheck.db"
)
//读取与写入的数据放在一块----写入数据库文件
func exceTosqliteChckdata(db *sql.DB, ExcelName string) {
//f, err := excelize.OpenFile("./sx1.xlsx")
f, err := excelize.OpenFile(ExcelName)
if err != nil {
fmt.Println("打开excel文件错误", err)
return
}
rows := f.GetRows("ck")
for index, row := range rows {
//indiex-->是行号 1,2,3,。。。
if index == 0 { //第一行‘表头’数据不写入数据库
continue
}
//fmt.Println("inex=", index)
//fmt.Println(row) //[4000027 三层南走廊东 4000027 hkGxwgL6u03f]
Gbid := row[0] //gb id 4000027
devicename := row[1] //设备名称 --三层南走廊东
fmt.Println(Gbid, devicename) //录入数据内容
sql := `insert into checkdata (gbid, devicename) values(?,?)`
stmt, _ := db.Prepare(sql)
_, err := stmt.Exec(Gbid, devicename)
if err != nil {
log.Fatalln("插入数据失败")
}
}
}
func exceTosqliteNetwl(db *sql.DB, ExcelName string) {
//f, err := excelize.OpenFile("./sx1.xlsx")
f, err := excelize.OpenFile(ExcelName)
if err != nil {
fmt.Println("打开excel文件错误!", err)
return
}
rows := f.GetRows("data")
for index, row := range rows {
if index == 0 { //第一行‘表头’数据不写入数据库--跳过第一行数据,不读
continue
}
//fmt.Println(row)
Gbid := row[5] //gb id -表格的第五列是gbid
devicename := row[0] //设备名称 --三层南走廊东
fmt.Println(Gbid, devicename) //录入数据内容
sql := `insert into netwl (gbid, devicename) values(?,?)`
stmt, _ := db.Prepare(sql)
_, err := stmt.Exec(Gbid, devicename)
if err != nil {
log.Fatalln("插入数据失败")
}
}
}
func main() {
//初始化数据sqlite3db
db, err := sql.Open(dbDriverName, dbname)
if err != nil {
fmt.Println(err)
}
//调用函数保存文件到sqlite3数据库
exceTosqliteChckdata(db, "dly.xlsx")
exceTosqliteNetwl(db, "sx1.xlsx")
time.Sleep(time.Second * 2)
println("数据录入完成")
}