Powershell脚本可在MS Access DB上运行查询并将数据导出到Excel文件

时间:2018-10-29 21:11:31

标签: excel powershell ms-access

您好,我正在尝试编写Powershell脚本以在MS Access数据库上运行查询,然后将查询生成的表导出到excel文件。

这是我当前的脚本,但是我遇到的问题是所有数据都被导出到一个单元格中。我需要将每个字段项目导出到其自己的列。

$DBFilePath ="D:\SFET_BuildVersion\CurrencyReport\ClientBuildVersions.accdb" 
$adOpenStatic = 3 
$adLockOptimistic = 3 
$infoColl = @() 

$conn=New-Object -com "ADODB.Connection" 
$rs = New-Object -com "ADODB.Recordset" 
$conn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$DBFilePath; Persist Security Info=False;") 
$rs.Open("SELECT * from SFClient",$conn,$adOpenStatic,$adLockOptimistic) 
$rs.MoveFirst() 
$myOuput = While ( ! $rs.EOF ) { 
    New-Object -TypeName PSObject -Property @{ 
        Customer = $rs.Fields.Item('Customer').Value 
        HHRR = $rs.Fields.Item('HHRR').Value 
        System = $rs.Fields.Item('System').Value 
        Release_Level = $rs.Fields.Item('Release Level').Value 
        TNTAL_version = $rs.Fields.Item('TNTAL version').Value 
        HF_Level = $rs.Fields.Item('HF Level').Value 
        Createdate = $rs.Fields.Item('Createdate').Value 
        CLAIMS_version = $rs.Fields.Item('CLAIMS version').Value 
        CreatedateC = $rs.Fields.Item('CreatedateC').Value 
        PayerRules_version = $rs.Fields.Item('PayerRules version').Value 
        ECV_Version = $rs.Fields.Item('ECV Version').Value 
        Edits = $rs.Fields.Item('Edits').Value 
        REGs_version = $rs.Fields.Item('REGs version').Value 
        BBM = $rs.Fields.Item('BBM version').Value 
        OMS_version = $rs.Fields.Item('OMS version').Value 
        Runtime = $rs.Fields.Item('Runtime').Value 
        HASvr = $rs.Fields.Item('HASvr').Value 
        EOS = $rs.Fields.Item('EOS').Value 
        CurrencyDays = $rs.Fields.Item('Currency Days').Value 
    } 

    $rs.MoveNext() 
} 
$conn.Close 
$rs.Close 

$myOuput | FT -a 
$myOuput | Out-GridView 
$myOuput | Export-csv D:\SFET_BuildVersion\CurrencyReport\SFClient.xlsx -NoTypeInformation 

0 个答案:

没有答案