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
:
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.