Excel 2013 COM API Hangs ExportAsFixedFormat under

2019-07-04 11:31发布

问题:

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;
                    }
                }