PowerShell select range of cells from Excel file and convert to CSV

后端 未结 1 845
傲寒
傲寒 2021-01-27 00:47

I want to extract a range of cells from an Excel sheet and output them in CSV form for further use. So far I have written this:

script.ps1:

         


        
1条回答
  •  后悔当初
    2021-01-27 01:11

    Try this as an alternative method. I haven't tried this out in your environment, but something similar worked for me. Hope this helps.

    $excel = New-Object -ComObject Excel.Application
    $WB = 
      $excel.Workbooks.Open('c:\users\me\desktop\temp\nouveau dossier\superstore.xls')
    $WS = $WB.Sheets.Item(1)
    $data = $WS.Range("A1", "E10")
    $XLcsv = 6       #  Parameter to tell SaveAs to produce a CSV format
    $data.worksheet.SaveAs('c:\users\me\desktop\temp\nouveau dossier\YATry.csv', $XLcsv)
    $excel.Quit()
    

    SaveAs is a method in the Excel application that does what the interactive user does by clicking 'Save As'. The second parameter tells SaveAs what format to generate, in this case CSV.

    The tricky part is figuring out that you have to say $data.worksheet instead of $data in order to gain access to the SaveAs method.

    0 讨论(0)
提交回复
热议问题