I'm new to PowerShell, and can't seem to figure out why the first method in the code below does not work, while the second method (commented out) does.
I'm trying to write the result of a database recordset to an Excel file, so i need a dynamically expandable array. I know how to do that with the first type of array, but not with the second type.
My problem would be solved if:
- you know how to dynamically expand the array of the second type
Edit: As mentioned here http://powershell.com/cs/blogs/tips/archive/2008/12/05/multidimensional-arrays.aspx this isn't possible. - know how to write the array of the first type to the range
- Edit: or, you know an efficient method to convert the array of the first type to the second type.
The code:
#need this to work around bug if you have a non us-locale and English excel: http://support.microsoft.com/default.aspx?scid=kb;en-us;320369
[System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"
# This doesn't work
$values = @(('test1','test2'),('test3','test4'))
# This works
<#
$values = New-Object 'object[,]' 2,2
$values[0,0] = 'test1'
$values[0,1] = 'test2'
$values[1,0] = 'test3'
$values[1,1] = 'test4'
#>
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $False
$xls_workbook = $excel.Workbooks.Add()
$range = $xls_workbook.sheets.item(1).Range("A1:B2")
$range.Value2 = $values
$xls_workbook.SaveAs($MyInvocation.MyCommand.Definition.Replace($MyInvocation.MyCommand.Name, "") + "test.xlsx")
$excel.Quit()