【发布时间】:2014-06-02 02:12:54
【问题描述】:
这是我对 sql server 执行并将结果存储在 Excel 中的查询。
我无法实现的是
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
【问题讨论】:
标签: excel powershell powershell-3.0