我正在尝试将一些Excel(.xlsx)和Access(.accdb)转换为CSV文件。

我很快找到了一种使用Excel进行此操作的方法,但现在找不到关于转换.accdb文件的任何有用的文档。

到目前为止,我有:

        $adOpenStatic = 3
        $adLockOptimistic = 3

        $objConnection = New-Object -com "ADODB.Connection"
        $objRecordSet = New-Object -com "ADODB.Recordset"


        $objConnection.Open("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + $Filepath)
        $objRecordset.Open("Select * From TableName",$objConnection,$adOpenStatic, $adLockOptimistic)

#Here I need some way to either saveas .csv or loop through
#each row and pass to csv.

        $objRecordSet.Close()
        $objConnection.Close()

有任何想法吗?

如果有人知道的话,我愿意用另一种语言(VB,Java,PHP)来做。

最佳答案

如果要坚持使用ADODB COM对象:

# loop through all records - do work on each record to convert it to CSV
$objRecordset.Open("Select * FROM Tablename", $objConnection,$adOpenStatic,$adLockOptimistic)

$objRecordset.MoveFirst()

do {
# do your work to get each field and convert this item to CSV
# fields available thru: $objRecordset.Fields['fieldname'].Value
$objRecordset.MoveNext()

} while ($objRecordset.EOF -eq $false)

$objRecordset.Close()

09-25 18:23