這裏是我的查詢來執行sql服務器並將結果存儲在Excel中。格式化SQL服務器結果
我什麼不能做到的,是
1)製作標題的字體大小16和大膽。
2)標題背景顏色爲藍色。
3)所有的記錄都是有界的。
4)有一個名爲Status的列顯示成功,失敗。我想在顏色上假設失敗紅色,成功綠色
$query = "Select [Job Name],(SUBSTRING(run_duration, 1, 2) + ':' + SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2)) AS run_duration,run_date ,
(SUBSTRING(run_Time, 1, 2) + ':' + SUBSTRING(run_Time, 3, 2) + ':' +
SUBSTRING(run_Time, 5, 2)) AS [Job _Started Time]
from (
Select k.name as [Job Name], run_date,
[run_duration] = ((RIGHT('000000' + CONVERT(varchar(6), run_duration), 6))),
((RIGHT('000000' + CONVERT(varchar(6), run_Time), 6))) as
Run_Time from Msdb..sysjobhistory j inner join msdb ..sysjobs k
on K.job_ID=j.Job_ID
Where run_date=CONVERT(VARCHAR(10), GETDATE(), 112) and step_id=0
) a "
Get-process *Excel* |Stop-process
$csvFilePath = "c:\Powershell\Jobs.csv"
$excelFilePath = "c:\Powershell\Jobs1.xls"
write-host "Executing query against server: Localhost "
$results = Invoke-Sqlcmd -Query $query
write-host "Saving Query Results in CSV format..."
$results | export-csv $csvFilePath -NoTypeInformation
write-host "Converting CSV output to Excel..."
$excel = New-Object -ComObject excel.application
$excel.visible = $true
$excel.displayalerts=$False
$workbook = $excel.Workbooks.Open($csvFilePath)
$workSheet = $workbook.worksheets.Item(1)
$resize = $workSheet.UsedRange
$resize.EntireColumn.AutoFit() | Out-Null
$xlExcel8 = 56
$workbook.SaveAs($excelFilePath,$xlExcel8)
$Sheet = $excel.WorkSheets.Item(1)
$excel = $null
write-host "Results are saved in Excel file: " $excelFilePath
Get-process *Excel* |stop-Process -verbose