繁体   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