多sheet数据插入sql server一个表 Autohotkey

本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。

thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。

 

source := "src.xls" ;源文件
target := "[a].[dbo].[test_table]" ;数据库目标表
has_create_tab := 0 ;是否已创建表
xlsdb := new exceldb() ;创建excel adodb连接,获取数据表信息
xlsdb.open(source)
sheet := xlsdb.GetTableInfo()
;数据库连接
conn := ComObjCreate("ADODB.connection")
conn.Open("driver={SQL Server};server=192.168.8.2;uid=thinkai;pwd=02EdDd68F5CC83__;database=a") ;打开连接
 
;遍历有效sheet
for k,v in sheet
{
    fields := v
    if !has_create_tab ;尝试创建表
    {
        f := ""
        for x,y in v
            f .= f ? ",[" y "] nvarchar(255) NULL" : "[" y "] nvarchar(255) NULL"
        try
            conn.Execute("CREATE TABLE " target " (" f ");")
    }
 
    field := "" ;生成字段串 注意表格里面的字段名称应和数据库中的字段一致
    for x,y in v
            field .= field ? ",[" y "]" : "[" y "]"
 
    tmp_result := xlsdb.GetTable("SELECT * FROM [" (InStr(k,"$") ? k : k "$") "];") ;获取单个sheet的全部数据
    for row,vaules in tmp_result
    {
        tmp_str := ""
        for k,v in vaules
            tmp_str .= tmp_str ? ",'" v "'" : "'" v "'"
        conn.Execute("INSERT INTO " target " VALUES (" tmp_str ")") ;插入语句
    }
}
MsgBox, OK
 
 
getto(str){
    o := []
    Loop, Parse, str, `n, `r
    {
        IfInString, A_LoopField, `t
        {
            t := StrSplit(A_LoopField,"`t")
            o[t[1]] := t[2]
        }
    }
    return o
}
 
class exceldb
{
    ;static conn
 
    __New() ;新建
    {
    this.conn:= ComObjCreate("ADODB.connection") ;初始化COM
    }
 
    open(file) ;打开文件
    {
        IfExist % file
            this.conn.Open("Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" file) ;打开连接
            ;this.conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=Yes';Data Source=" file) ;打开连接 2003方式
    }
 
    close() ;关闭文件
    {
        this.conn.Close()
    }
 
 
    GetTableInfo() ;获取所有Sheet及字段信息
    {
        ;通过OpenSchema方法获取表信息
        rs := this.conn.OpenSchema(20) ;SchemaEnum 参考 http://www.w3school.com.cn/ado/app_schemaenum.asp
        t := []
        rs.MoveFirst()
        while !rs.EOF
        {
            t_name := RegExReplace(rs.("TABLE_NAME").value,"^'*(.*)\$'*$","$1")
            q := this.conn.Execute("select top 1 * from [" t_name "$]")
            if (q.Fields(0).Name="F1" && q.Fields.Count=1) ;排除空表格
            {
                rs.MoveNext()
                continue
            }
            t[t_name] := []
            for field in q.Fields  ;获取按顺序排列的字段
                t[t_name].insert(field.Name)
            q.close()
            rs.MoveNext()
        }
        return t
    }
 
    GetTable(sql)
    {
        t := []
        query := this.conn.Execute(sql)
        if RegExMatch(sql,"i)^select*")
        {
            fetchedArray := query.GetRows() ;取出数据(二维数组)
            colSize := fetchedArray.MaxIndex(1) + 1 ;列最大值 tips:从0开始 所以要+1
            rowSize := fetchedArray.MaxIndex(2) + 1 ;行最大值 tips:从0开始 所以要+1
            loop, % rowSize
            {
                i := (y := A_index) - 1
                t[y] := []
                loop, % colSize
                {
                    j := (x := A_index) - 1
                    t[y][x] := fetchedArray[j,i] ;取出二维数组内值
                }
            }
            query.Close()
            return t
        }
    }
}

 

给TA捐赠
共{{data.count}}人
人已捐赠
其他教程

GetToolTipText 获取当前tooltip内的文字 AutoHotkey

2020-2-20 8:28:43

其他教程案例

多进程任务管理示例 Autohotkey

2020-2-20 8:31:43

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索