function Export-Excel {
try {
if ($list.Items.Count -eq 0) { return }
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$ws.Name = "Rapport $(Get-Date -Format 'dd-MM-yyyy')"
# Remplacez ceci par les en-têtes de votre liste
$headers = @("Colonne 1", "Colonne 2", "Colonne 3")
$headerRange = $ws.Range("A1", "C1")
$headerRange.Value2 = $headers
$headerRange.Font.Bold = $true
$headerRange.Font.Size = 12
$rowCount = $list.Items.Count
$colCount = $headers.Count
$data = New-Object 'object[,]' $rowCount, $colCount
for ($r = 0; $r -lt $rowCount; $r++) {
for ($c = 0; $c -lt $colCount; $c++) {
$data[$r, $c] = $list.Items[$r].SubItems[$c].Text
}
}
$dataRange = $ws.Range($ws.Cells.Item(2, 1), $ws.Cells.Item($rowCount + 1, $colCount))
$dataRange.Value2 = $data
$ws.Columns.AutoFit() | Out-Null
$excel.Visible = $true
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ws) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($wb) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
}
catch {
[System.Windows.Forms.MessageBox]::Show("Erreur lors de l'export : $($_.Exception.Message)", "Rapport")
}
}