0

好的,所以我们有一个通过 PL/SQL Developer 运行的手动过程来运行查询,然后导出到 csv。

由于我们在 Windows 环境中工作,因此我正在尝试使用 powershell 自动化该过程。

我创建了两个似乎与自动和手动过程完全相同的文件,但它们的工作方式不同,所以我假设我缺少一些隐藏字符,但我找不到它们或弄清楚如何删除它们。

它们工作方式不同的最明显示例是在 excel 中打开它们。手动文件在 excel 中打开,自动将每一列放在它自己的单独列中。自动文件将所有内容放在一列中。

任何人都可以解释一下吗?我希望通过解决这个问题或至少获得一些信息将有助于解决无法正确处理的更大问题。

谢谢。

例如一列 "rownum","year","month","batch","facility","transfer_facility","trans_dt","meter","ticket","trans_product","trans","shipper" “客户”、“供应商”、“经纪人”、“原产地”、“目的地”、“数量”

ex单独的列“”,“ROWNUM”,“RPT_YR”,“RPT_MO”,“BATCH_NBR”,“FACILITY_CD”,“TRANSFER_FACILITY_CD”,“TRANS_DT”,“METER_NBR”,“TKT_NBR”,“TRANS_PRODUCT_CD”,“TRANS_CD”, "SHIPPER_CD","CUSTOMER_NBR","SUPPLIER_NBR","BROKER_CD","ORIGIN_CD","DESTINATION_CD","NET_QTY"

$connectionstring = "Data Source=database;User Id=user;Password=password"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionstring)
$command = New-Object System.Data.OracleClient.OracleCommand($query, $connection)
$connection.Open()
Write-Host -ForegroundColor Black " Opening Oracle Connection"
Start-Sleep -Seconds 2

#Getting data from oracle
Write-Host
Write-Host -ForegroundColor Black "Getting data from Oracle"
$Oracle_data=$command.ExecuteReader()
Start-Sleep -Seconds 2

if ($Oracle_data.read()){
Write-Host -ForegroundColor Green "Connection Success"
while ($Oracle_data.read()) {

#Variables for recordset
$rownum = $Oracle_data.GetDecimal(0)
$rpt_yr = $Oracle_data.GetDecimal(1)
$rpt_mo = $Oracle_data.GetDecimal(2)
$batch_nbr = $Oracle_data.GetString(3)
$facility_cd = $Oracle_data.GetString(4)
$transfer_facility_cd = $Oracle_data.GetString(5)
$trans_dt = $Oracle_data.GetDateTime(6)
$meter_nbr = $Oracle_data.GetString(7)
$tkt_nbr = $Oracle_data.GetString(8)
$trans_product_cd = $Oracle_data.GetString(9)
$trans_cd = $Oracle_data.GetString(10)
$shipper_cd = $Oracle_data.GetString(11)
$customer_nbr = $Oracle_data.GetString(12)
$supplier_nbr = $Oracle_data.GetString(13)
$broker_cd = $Oracle_data.GetString(14)
$origin_cd = $Oracle_data.GetString(15)
$destination_cd = $Oracle_data.GetString(16)
$net_qty = $Oracle_data.GetDecimal(17)

#Define new file
$filename = "Pipeline" #Get-Date -UFormat "%b%Y"
$filename = $filename + ".csv"
$fileLocation = $newdir + "\" + $filename 
$fileExists = Test-Path $fileLocation

    #Create object to hold record
    $obj = new-object psobject -prop @{
        rownum = $rownum
        year = $rpt_yr
        month = $rpt_mo
        batch = $batch_nbr
        facility = $facility_cd
        transfer_facility = $transfer_facility_cd
        trans_dt = $trans_dt
        meter = $meter_nbr
        ticket = $tkt_nbr
        trans_product = $trans_product_cd
        trans = $trans_cd
        shipper = $shipper_cd
        customer = $customer_nbr
        supplier = $supplier_nbr
        broker = $broker_cd
        origin = $origin_cd
        destination = $destination_cd
        quantity = $net_qty
    }
    $records += $obj

}

}else {
Write-Host -ForegroundColor Red " Connection Failed"
}

#Write records to file with headers
$records | Select-Object      rownum,year,month,batch,facility,transfer_facility,trans_dt,meter,ticket,trans_product,trans,shipper,customer,supplier,broker,origin,destination,quantity |
ConvertTo-Csv | 
Select -Skip 1|
Out-File $fileLocation
4

1 回答 1

1

为什么要跳过第一行(通常是标题)?另外,请尝试改用 Export-CSV:

#Write records to file with headers
$records | Select-Object rownum, year, month, batch, facility, transfer_facility, trans_dt, meter, ticket, trans_product, trans, shipper, customer, supplier, broker, origin, destination, quantity |
Export-Csv $fileLocation -NoTypeInformation
于 2013-01-29T19:21:36.637 回答