找回密码
 加入
搜索
查看: 2436|回复: 2

[系统综合] au3中如何在已经打开的excel中引入外部的CSV文件?

[复制链接]
发表于 2017-3-27 21:26:07 | 显示全部楼层 |阅读模式
au3中如何在已经打开的excel中引入外部的CSV文件?
下面的代码是先创建excel对象,然后设置单元个的格式为文本,因为里面有的列是长的数码文本列,ActiveSheet.QueryTables.Add执行没有成功,不知au3如何书写?
        $filename = "c:\lhq.csv"
        $oExcel = ObjCreate("Excel.Application")
        $oExcel.Visible = True
        $oExcel.DisplayAlerts = True
        $oWorkbook_o = $oExcel.Workbooks.Add
        $oExcel.Columns.Select()
        $oExcel.Selection.NumberFormatLocal = "@"
        $oExcel.ActiveSheet.QueryTables.Add('Connection:= "TEXT;' & $filename & '"', 'Destination:=Range("A1")')
通过excel的宏录制引入文本文件如下:
Sub Macro1()
'
' Macro1 Macro
' 宏由 Administrator 录制,时间: 2017-3-27
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Documents and Settings\Administrator\桌面\lhq.csv", Destination:=Range _
        ("A1"))
        .Name = "lhq"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

End Sub
发表于 2017-3-28 09:35:40 | 显示全部楼层
假设将文件保存到c:\mytest.xlsx中去
$filename = "c:\lhq.csv"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = True
$oExcel.DisplayAlerts = True
$oWorkbook_o = $oExcel.Workbooks.Add
$oWorkbook_o.activesheet.Columns.Select
$oWorkbook_o.activesheet.Selection.NumberFormatLocal = "@"
Local $aArray=[1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
With $oWorkbook_o.ActiveSheet.QueryTables.Add("TEXT;" &  $filename , $oWorkbook_o.activesheet.Range("A1"))
        .Name = "lhq"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = 1; xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = 1;xlDelimited
        .TextFileTextQualifier = 1 ;xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes =$aArray; Array(1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh(False)
EndWith
$oWorkbook_o.saveas("c:\mytest.xls",51)
$oWorkbook_o.close
$oExcel.quit
发表于 2017-4-2 02:03:35 | 显示全部楼层
这个是什么代码
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-23 16:21 , Processed in 0.111837 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表