;Thinkai@2015-10-25
Gui, Add, Tab, x0 y0 w800 h500 vtab
Gui, Show, , 输出excel数据到GUI
FileSelectFile, file, , , 选择一个表格, Excel文件(*.xls;*.xlsx)
IfNotExist % file
ExitApp
conn := ComObjCreate("ADODB.connection") ;初始化COM
conn.Open("Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" file) ;打开连接
;conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=Yes';Data Source=" file) ;打开连接 2003方式
;通过OpenSchema方法获取表信息
rs := conn.OpenSchema(20) ;SchemaEnum 参考 http://www.w3school.com.cn/ado/app_schemaenum.asp
table_info := []
rs.MoveFirst()
while !rs.EOF ;有效Sheet
{
t_name := RegExReplace(rs.("TABLE_NAME").value,"\$$","")
q := conn.Execute("select top 1 * from [" t_name "$]")
if (q.Fields(0).Name="F1" && q.Fields.Count=1) ;排除空表格
{
rs.MoveNext()
continue
}
table_info[t_name] := []
for field in q.Fields ;获取按顺序排列的字段
table_info[t_name].insert(field.Name)
q.close()
rs.MoveNext()
}
;生成Listview
for t,c in table_info
{
;创建tab及listview
GuiControl, , tab, % t
Gui, Tab, % A_index
cols =
for k,v in c
cols .= cols ? "|" v : v
Gui, Add, ListView, % "x10 y30 w780 h460 vlv" A_Index, % cols
Gui, ListView, % "lv" A_Index
;获取表格数据
data := GetTable("select * from [" t "$]")
for k,v in data
LV_Add("",v*)
LV_ModifyCol() ;自动调整列宽
}
rs.close()
conn.close()
return
GuiClose:
ExitApp
GetTable(sql){ ;Adodb通用的获取数据数组的函数
global conn
t := []
query := conn.Execute(sql)
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
}
60 queries in 1.370 seconds |