Passage en paramètre ComObject (Excel) + conversion XLSX
Bonjour,
Voilà, je dois faire quelques modif' automatiques dans un fichier excel (je récupère un listing CSV, je le convertis en XLSX, et je voudrais après faire quelques modifs.
Alors j'ai réussi à faire fonctionner le premier script de conversion :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
#$Excel.Visible = $True
$input_file="C:\Users\metalman\Desktop\fab\test.csv"
$output_file="C:\Users\metalman\Desktop\fab\test.xlsx"
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$wbk = $Excel.Workbooks.Open("$input_file")
$Excel.ActiveWorkbook.SaveAs("$output_file", $xlFixedFormat)
$Excel.Workbooks.Close()
$Excel.Quit() |
1) Je voudrais être sûr du format : [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault génèrera TOUJOURS du XLSX ? Ou c'est parce que je l'ai configuré ainsi dans mon profil ?
2) Je voudrais rendre l'ensemble un peu plus... structuré ! En faisant une fonction, amis ça ne fonctionne pas :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| function MyConvert([System.Object]$excel, [string]$filein, [string]$fileout, [Microsoft.Office.Interop.Excel.XlFileFormat]$format)
{
$wbk = $excel.Workbooks.Open("$filein")
#$b = $a.Workbooks.Add()
#$c = $b.Worksheets.Item(1)
#
#$c.Cells.Item(1,1) = "A value in cell A1."
$excel.ActiveWorkbook.SaveAs("$fileout", $format)
$excel.Workbooks.Close()
}
$my_excel = New-Object -comobject Excel.Application
$my_excel.Visible = $False
#$excel.Visible = $True
$input_file="C:\Users\metalman\Desktop\fab\test.csv"
$output_file="C:\Users\metalman\Desktop\fab\test.xlsx"
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
MyConvert($my_excel, $input_file, $output_file, $xlFixedFormat)
$my_excel.Quit() |
Me crache :
Citation:
PS S:\> C:\Users\metalman\Desktop\fab\ExcelPowerShell.ps1
Impossible d’appeler une méthode dans une expression Null.
Au caractère C:\Users\metalman\Desktop\fab\ExcelPowerShell.ps1:3 : 5
+ $wbk = $excel.Workbooks.Open("$filein")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Impossible d’appeler une méthode dans une expression Null.
Au caractère C:\Users\metalman\Desktop\fab\ExcelPowerShell.ps1:10 : 5
+ $excel.ActiveWorkbook.SaveAs("$fileout", $format)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Impossible d’appeler une méthode dans une expression Null.
Au caractère C:\Users\metalman\Desktop\fab\ExcelPowerShell.ps1:12 : 5
+ $excel.Workbooks.Close()
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation : (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Alors je pense que c'est le passage du $excel qui fait tout foirer, mais pourquoi ?
Comment puis-je n'ouvrir qu'une seul fois Excel, lancer ma procédure de conversion, et revenir dans le code principal ?
Merci à tous ! :)