【问题标题】:Export specific columns from Excel to .csv Powershell将特定列从 Excel 导出到 .csv Powershell
【发布时间】:2014-08-07 14:11:32
【问题描述】:

我有这样一个代码,用于将带有两个工作表的 Excel 文件导出到两个 csv 文件中。问题是我目前正在导出整个工作表,并且我只想从循环中导出这 3 列。如何保存它们?它们必须是有序的,因为我想稍后将其导入 AD。

Function ExportWSToCSV ($excelFileName , $csvLoc){

#Sample use in a console:  ExportWSToCSV -excelFileName "Test_Peoplesoft.xls" -csvLoc "y:\Application Data\CSVFiles\"

$CultureOld = [System.Threading.Thread]::CurrentThread.CurrentCulture
#Original culture info 
$CultureUS = [System.Globalization.CultureInfo]'en-US'                            
#US culture info
$excelFile = "y:\Application Data\Test_Peoplesoft.xls"                            
#Loc of Excel file .xls    , #csvLov - Loc of output files in format .csv
[System.Threading.Thread]::CurrentThread.CurrentCulture = $CultureUS
$E = New-Object -ComObject Excel.Application
$E.Visible = $false                                                               
$E.DisplayAlerts = $false                                                   
$wb = $E.Workbooks.Open($excelFile)
$intRow = 2  
$intRowMax =($ws.UsedRange.Rows).count 
$elements = $email -or $costcode -or $leader 



Do{
foreach($ws in $wb.sheets.item("Inactive")){
if($elements -ne $null ){
$email = $ws.Cells.Item($intRow, 4).Value()
$costcode = $ws.Cells.Item($intRow, 15).Value()
$leader = $ws.Cells.Item($intRow, 20).Value()
}else{Write-Host "Null Value in one of the attributes"}

}
<#
 foreach($ws in $wb.sheets.item("Inactive")){
$email = $ws.Cells.Item($intRow, 4).Value()
$costcode = $ws.Cells.Item($intRow, 15).Value()
$leader = $ws.Cells.Item($intRow, 20).Value()
}
#>
$user = $email + "_" + $costcode + "_" + $leader

write-host $intRow " " $user 

$intRow++    

}While ($ws.Cells.Item($intRow,1).Value() -ne $null)


foreach ($ws in $wb.Worksheets)
{
    Write-Host "Processing Worksheet: " $ws.Name
    $n = $csvLoc + $excelFileName + "_" + $ws.Name                               
    #Name variable - Output file loc + excel file name + worksheet name             
    $ws.SaveAs($n + ".csv", 6)                                                   
    #Saving file to .csv       
}



$E.Quit()
[System.Threading.Thread]::CurrentThread.CurrentCulture = $CultureOld

}

【问题讨论】:

    标签: excel powershell csv windows-xp export-to-csv


    【解决方案1】:

    这样的事情应该可以工作:

    首先,设置一个数组来包含我们的导出用户列表,如下所示:

    $exportList = @()
    

    (在开始循环行之前放置)

    然后使用 do while 循环遍历工作表上的行,并添加一个用户对象以像这样添加导出列表

    # Create userObj with the required properties
    $userObj = New-Object System.Object
    $userObj | Add-Member -Type NoteProperty -Name Email -Value $email
    $userObj | Add-Member -Type NoteProperty -Name CostCode -Value $costcode
    $userObj | Add-Member -Type NoteProperty -Name Leader -Value $leader
    
    # Add $userObj to our list to be exported
    $exportList += $userObj
    

    当然也可以将其导出为 .csv

    $exportList | Export-Csv $pathToCsvFile
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2015-02-15
      • 2016-03-29
      相关资源
      最近更新 更多