EXCEL在Windows 7中打开,但在Windows Server 2012中出错

时间:2014-08-14 13:36:06

标签: sql-server excel powershell

我有一个生成EXCEL电子表格的脚本。我可以在Windows 7工作站中轻松打开它,但当我尝试在Windows Server 2012中打开它时,我收到错误

enter image description here

当我点击“确定”时,我得到了

enter image description here

这是代码的主旨

$SQL1 = "..."

$SQL2 = "..."

$SQL3 = "..." 

$SQL4 = "..." 



# Create Excel file to save the data

if (!(Test-Path -path "$DirectoryToSave")) #create it if not existing 
  { 
  New-Item "$DirectoryToSave" -type directory | out-null 
  } 

$excel = New-Object -Com Excel.Application
$excel.Visible = $True
$wb = $Excel.Workbooks.Add()
$ws = $wb.Worksheets.Add()
$currentWorksheet=1

$ws = $wb.Worksheets.Item(1)
$ws.name = "GUP Download Activity"


$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL1)

if ($qt.Refresh()){
    $ws.Activate()
    $ws.Select()
    ...
}

$ws = $wb.Worksheets.Item(2)
$ws.name = "Totals"


$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL2)

if ($qt.Refresh()){
    $ws.Activate()
    $ws.Select()
    ...
 }


$ws = $wb.Worksheets.Item(3)
$ws.name = "GUP Downloads per Computer"


$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL3)

if ($qt.Refresh()){
    $ws.Activate()
    $ws.Select()
    ...
 }


$ws = $wb.Worksheets.Item(4)
$ws.name = "GUP Monthly Trends"


$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN2;UID=$username;PWD=$password", $ws.Range("A1"), $SQL4)

if ($qt.Refresh()){
    $ws.Activate()
    $ws.Select()
    ...
 }


$filename = "D:\Script\Daily_GUP_Report1.xlsx"
if (test-path $filename ) { rm $filename } 
$wb.SaveAs($filename,  $xlOpenXMLWorkbook) #save as an XML Workbook (xslx) 
$wb.Saved = $True #flag it as being saved 
$wb.Close() #close the document 
$Excel.Quit() #and the instance of Excel 
$wb = $Null #set all variables that point to Excel objects to null 
$ws = $Null #makes sure Excel deflates 
$Excel=$Null #let the air out 

1 个答案:

答案 0 :(得分:1)

对我有用的是其中一项建议here

1:开始 - >运行并输入'DCOMCNFG' 2:打开“DCOM配置”并找到     'Microsoft Excel Application'
3:将其标识设置为“启动”     用户