简体   繁体   English

Office.Interop.Excel-为什么需要在服务器上打开Excel?

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

I have been given a legacy project which uses Microsoft.Office.Interop.Excel[11.0.0.0] to import and export files. 我得到了一个使用Microsoft.Office.Interop.Excel [11.0.0.0]导入和导出文件的旧项目。 On the server MS Excel has to be running for the import/exports to work. 在服务器上,必须运行MS Excel才能使导入/导出正常工作。 By running I mean someone has to log on the server and open Excel via start menu (yes it's that bad!). 通过运行,我的意思是某人必须登录服务器并通过开始菜单打开Excel(是的,这很糟糕!)。 Does anyone know why it needs to be kept open? 有谁知道为什么需要保持开放状态? The import/export is extremely flakey and requires a close and re-open twice a day as it keeps crashing. 导入/导出非常容易,需要关闭并每天重新打开两次,以防止崩溃。

Here is the code, be warned...it's horrible! 这是代码,请警告...这太可怕了!

 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

Consider using Open XML SDK if you deal with open XML files only. 如果仅处理开放XML文件,请考虑使用Open XML SDK Or you may consider using any other commercial components designed for the server-side execution. 或者,您可以考虑使用为服务器端执行而设计的任何其他商业组件。

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. Microsoft当前不建议也不支持任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. 如果要构建在服务器端上下文中运行的解决方案,则应尝试使用对无人值守执行安全的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side. 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. 如果您从服务器端解决方案中使用Office应用程序,则该应用程序将缺少许多成功运行所需的功能。 Additionally, you will be taking risks with the stability of your overall solution. 此外,您将承担整体解决方案稳定性的风险。

You can read more about that in the Considerations for server-side Automation of Office article. 您可以在“ 服务器端Office自动化注意事项”文章中阅读有关此内容的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Office.Interop.Excel 将文件另存为 PDF/A - Save file as PDF/A using Office.Interop.Excel 为什么即使导入Microsoft.Office.Interop.Excel(VB.NET),也无法在所有Excel对象上获得“未定义”? - Why do I get “not defined” on all the Excel objects even after importing Microsoft.Office.Interop.Excel (VB.NET)? 使用Microsoft.Office.Interop.Excel.dll库; Excel是否需要在最终用户的计算机上? - Using the Microsoft.Office.Interop.Excel.dll library; does Excel need to be on the end user's machine? 如何使用Microsoft.Office.Interop.Excel在SSIS 2013中找到VB.Net的正确语法? - How do I find the right syntax for VB.Net in SSIS 2013 using Microsoft.Office.Interop.Excel? 使用Office Interop将单元格写入Excel的最快方法? - Fastest way to write cells to Excel with Office Interop? 升级到Office 2016参考错误:Microsoft.Office.Interop.Excel - Upgrading to Office 2016 Reference Error: Microsoft.Office.Interop.Excel Excel到DataTable? 不使用Microsoft.Office.Interop.Excel - Excel to DataTable? Without using Microsoft.Office.Interop.Excel 使用 Microsoft.Office.Interop.Excel 而实际上没有 Excel? - Using Microsoft.Office.Interop.Excel without actually having Excel? 在Assembly / COM浏览器窗口中的哪里可以找到“ microsoft.office.interop.excel”? - Where can I find “microsoft.office.interop.excel” in the Assembly / COM browser window? 我收到错误“应用程序”在命名空间“microsoft.office.interop.excel”中不明确 - I'm getting the error “Application” is ambiguous in the namespace 'microsoft.office.interop.excel'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM