Excel 2013 COM API Hangs ExportAsFixedFormat under Service Account

白昼怎懂夜的黑 提交于 2019-12-10 17:53:20

问题


I have a .NET Windows Service which calls the Excel 2013 COM API to export an excel document on PDF, I have tried this code with the Windows Service running under two different domain accounts, both local administrator on the machine running the code. Under my personal account, which I'm also logged into the machine as, the code executes as expected. Under the service account, which is an active directory account, that is not logged into the machine the excel document is opened but the export logic never returns and when I stop the service the Excel instance that the service opens remains open.

This is the code I'm using:

object paramMissing = Type.Missing;
                XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
                XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
                bool paramOpenAfterPublish = false;
                bool paramIncludeDocProps = true;
                bool paramIgnorePrintAreas = false;
                object paramFromPage = Type.Missing;
                object paramToPage = Type.Missing;

                Workbook excelWorkBook = null;
                try
                {
                    // Open the source workbook.
                    excelWorkBook = excelApplication.Workbooks.Open(file,
                        paramMissing, paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing);

                    // Save it in the target format.
                    if (excelWorkBook != null)
                        excelWorkBook.ExportAsFixedFormat(paramExportFormat,
                            Directory.PdfDirectory + directoryName + @"\"+ Path.GetFileName(file).Replace(".xlsx", ".pdf"), paramExportQuality,
                            paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                            paramToPage, paramOpenAfterPublish,
                            paramMissing);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {

                    // Close the workbook object.
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }
                }

来源:https://stackoverflow.com/questions/25252451/excel-2013-com-api-hangs-exportasfixedformat-under-service-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!