1

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:

$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")
$data | select text | Export-Csv 'YATry.csv' -NoTypeInformation -Delimiter ';'
$excel.Quit()

but what I get is this:

"Text"  
"Row ID"  
"Order ID"  
"Order Date"  
"Ship Date"  
"Ship Mode"  
"1"  
"CA-2016-152156"  
"2016-11-08"   
"2016-11-11"  
"Second Class"  
"2"  
"CA-2016-152156"  
"2016-11-08"  
"2016-11-11"  
"Second Class"  
"3"  
"CA-2016-138688"  
"2016-06-12"  
"2016-06-16"  
"Second Class"
[...]

What am I missing?

8
  • Have you tried doing this in Excel? Sep 25, 2019 at 18:24
  • I have been hand copy-pasting the select range of cells into a temp sheet in a new workbook, and then exporting that workbook as *csv, and then opening that file in notepad++ to get to the data. I want to automate the conversion.
    – Sassinak
    Sep 25, 2019 at 20:38
  • What is the purpose of 'select text' in your code? Sep 26, 2019 at 18:25
  • What you have shown us is a CSV file with only one field. The name of the field is "Text". This appears as aheader in the first row.. The next few rows are the names of the column headers in the table you chose in the Excel workbook. This, presumably, is what you would have expected to see, separated by semicolons, in the first record of your CSV. This isn't an answer, but I hope it helps. Sep 26, 2019 at 20:08
  • Select text uses only the values in the range wanted, or else a lot of overhead is included.
    – Sassinak
    Sep 30, 2019 at 13:09

2 Answers 2

1

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.

6
  • I get an error because $data is not a workbook per se, and doesn't have the method. This is a PS script, so I'll look into transforming it into a WB first.
    – Sassinak
    Sep 30, 2019 at 13:06
  • it doesn't work. It says (translated) cant call a method in Null object. Yet, my
    – Sassinak
    Sep 30, 2019 at 16:00
  • it doesn't work. It says (translated) cant call a method in Null object. Yet, my line: $data |select value2 | Export-csv 'YA.csv' -notypeinformation -delimiter ';' still yields a single column of quoted values. I've changed the Range for ("A1:E10") with no difference. I tried copy-pasting to another worksheet and saveas from there, but I got nothing.
    – Sassinak
    Sep 30, 2019 at 16:06
  • I am sorry. I said 'workbook' where 'worksheet' would have been correct. I'm going to edit my answer. Sep 30, 2019 at 16:24
  • it doesn't respect the range asked, but rather exports the whole sheet.
    – Sassinak
    Sep 30, 2019 at 16:43
0
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open("d:\Test.xlsx")
$workbook.sheets.item(1).activate()
$workbook.Worksheets.Item(1).Range("A1", "C3") | foreach { $_.Text}
$workbook.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.