簡體   English   中英

Office.Interop.Excel-為什么需要在服務器上打開Excel?

[英]Office.Interop.Excel - Why do I need to have Excel open on the server?

我得到了一個使用Microsoft.Office.Interop.Excel [11.0.0.0]導入和導出文件的舊項目。 在服務器上,必須運行MS Excel才能使導入/導出正常工作。 通過運行,我的意思是某人必須登錄服務器並通過開始菜單打開Excel(是的,這很糟糕!)。 有誰知道為什么需要保持開放狀態? 導入/導出非常容易,需要關閉並每天重新打開兩次,以防止崩潰。

這是代碼,請警告...這太可怕了!

 objExcelApp = New Excel.Application()
            objExcelApp.DisplayAlerts = False
            objExcelBook = objExcelApp.Workbooks.Open(strExcelFileName, Password:="")
            If objExcelBook.Sheets.Count > 0 Then
                objExcelWorkSheet = CType(objExcelBook.Sheets(1), Excel.Worksheet)
                objExcelWorkSheet.Name = gstrDEFAULTSHEETNAME 'Rename the sheet
                objExcelRangeHeader = CType(objExcelWorkSheet.Rows(1), Excel.Range)
                objExcelWorkSheet.Cells.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone

                objExcelRangeFormat = objExcelWorkSheet.UsedRange
                intColumn = objExcelRangeFormat.Columns.Count

                For intColumnCount = 1 To intColumn
                    objColumnRange = CType(objExcelRangeHeader.Columns(intColumnCount), Excel.Range)
                    objColumnRange.EntireColumn.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White)
                    objColumnRange.EntireColumn.ClearFormats()
                    objColumnRange.EntireRow.ClearFormats()
                Next
                With objExcelWorkSheet
                    .Name = gstrSHEET_EXCEL
                    .Rows.Range("1:1").Font.Bold = True
                    .Rows.Range("1:1").Font.Color = RGB(0, 0, 255)
                    .StandardWidth = 50
                    .Cells.WrapText = True                    
                    .EnableOutlining = True
                End With

            End If


            objCountLink = CType(objExcelBook.LinkSources(), Object)
            If Not IsNothing(objCountLink) Then
                If (objCountLink.ToString.Length > 0) Then
                    lblErrorMessage.Visible = True
                    lblErrorMessage.InnerHtml = "<li> The spreadsheet you trying to import contains external links. Please ensure all links have been removed before attempting to import </li>"
                    objExcelBook.Close()
                    objExcelApp.Workbooks.Close()
                    objExcelApp.Quit()
                    objExcelApp = Nothing
                    Exit Sub
                End If
            End If
            objExcelBook.Save() 'Save the excel
            objExcelBook.Close()
            objExcelApp.Workbooks.Close()
            objExcelApp.Quit()
            objExcelApp = Nothing

如果僅處理開放XML文件,請考慮使用Open XML SDK 或者,您可以考慮使用為服務器端執行而設計的任何其他商業組件。

Microsoft當前不建議也不支持任何無人參與的非交互客戶端應用程序或組件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office應用程序自動化,因為Office可能表現出不穩定的行為和/在此環境中運行Office時出現死鎖或死鎖。

如果要構建在服務器端上下文中運行的解決方案,則應嘗試使用對無人值守執行安全的組件。 或者,您應該嘗試找到允許至少部分代碼在客戶端運行的替代方法。 如果您從服務器端解決方案中使用Office應用程序,則該應用程序將缺少許多成功運行所需的功能。 此外,您將承擔整體解決方案穩定性的風險。

您可以在“ 服務器端Office自動化注意事項”文章中閱讀有關此內容的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM