PowerShell以不可读的格式保存Excel工作表

时间:2016-12-28 13:22:15

标签: excel powershell

我有下面的代码,用于检查数据库的“文件到磁带”作业,并在Excel工作表中提供输出。

$date = Get-Date
$day = $date.Day
$hour = $date.Hour 
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $true
$Excel.DisplayAlerts = $false
$Workbook = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
#Counter variable for rows and columns
$intRow = 1
$intCol = 1

$Sheet.Cells.Item($intRow,1) = "Tasks/Servers"     
$Sheet.Cells.Item($intRow,2) = "DateLastRun"
$Sheet.Cells.Item($intRow,3) = "PRX1CSDB01"
$Sheet.Cells.Item($intRow,4) = "PRX1CSDB02"
$Sheet.Cells.Item($intRow,5) = "PRX1CSDB03"
$Sheet.Cells.Item($intRow,6) = "PRX1CSDB11"
$Sheet.Cells.Item($intRow,7) = "PRX1CSDB12"
$Sheet.Cells.Item($intRow,8) = "PRX1CSDB13"
$Sheet.Cells.Item($intRow+1,1) = "File To Tape weekly Full Backup"
$Sheet.UsedRange.Rows.Item(1).Borders.LineStyle = 1

#FTT.txt contains the path for a list of servers
$path =  Get-Content D:\Raghav\DB_Integrated\FTT.txt

foreach ($server in $path)
{
If (Test-Path $server)
{
$BckpWeek = gci -path $server | select-object | where {$_.Name -like "*logw*"} | sort LastWriteTime | select -last 1
$Sheet.Cells.Item($intRow+1,$intCol+1) = $BckpWeek.LastWriteTime.ToString('MMddyyyy')
$Sheet.UsedRange.Rows.Item($intRow).Borders.LineStyle = 1
$x = (get-date) - ([datetime]$BckpWeek.LastWriteTime)
if( $x.days -gt 7){$status_week = "Failed"}
else{$status_week = "Successful"}
$Sheet.Cells.Item($intRow+1,$intCol+2) = $status_week
$intCol++
}
else
{
$Sheet.Cells.Item($intRow+1,$intCol+2) = "Path Not Found"
$intCol++
}
}
$Sheet.UsedRange.EntireColumn.AutoFit()
$workBook.SaveAs("C:\Users\Output.xlsx",51)
$excel.Quit()

但是,当我尝试将Output.xlsx的内容导入变量说$cc时,我会以不可读的格式获取数据。

$cc = Import-Csv "C:\Users\Output.xlsx"

附件是我将output.xlsx导出到$ cc的图片。我试图将输出也放在csv格式中。但这似乎也没有帮助。output file任何人对此有任何想法或者之前遇到过任何类似情况?

1 个答案:

答案 0 :(得分:0)

@ZevSpitz - 在寻找OleDbConnection课程时,我登陆https://blogs.technet.microsoft.com/pstips/2014/06/02/get-excel-data-without-excel/。这就是我想要的。谢谢你指出我正确的方向。

@MikeGaruccio - 不幸的是,我在Get-Help菜单中找不到Import-Excel命令。我正在使用Powershell 4.0。无论如何,谢谢你的建议。