仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC [英] Only in Windows Service: System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC

查看:204
本文介绍了仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序可以生成报告并将其另存为Excel文件到文件夹中.

I am developing an application that generates a report and saves it as an Excel file in a Folder.

该解决方案由 Logic Library WinForm UI Windows Service 组成.

The solution consists of Logic Library, WinForm UI, and Windows Service.

逻辑库( .Net Framework 4.7.2 )中的核心工作.

The Core work in Logic Library (.Net Framework 4.7.2).

我的PC上安装的Office是 Office 2007 ,而操作系统是 Windows 10 Professional 64位.

The Office installed on my PC is Office 2007 and the OS is Windows 10 Professional 64 Bit.

UI应用程序完美生成并保存Excel文件,而 Windows服务生成错误:

The UI App generates and Save the Excel File perfectly while the Windows Service generates the error:

System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC在Microsoft.Office.Interop.Excel._Workbook.SaveAs(对象文件名,对象文件格式,对象密码,对象WriteResPassword,对象ReadOnlyRecommended,对象CreateBackup,XlSaveAsAccessMode AccessMode,对象冲突解决方案,对象AddToMru,对象TextCodepage,对象TextVisualLayout,对象本地)在C:\ REPOS \ Reporting.Tool.Services \ Utilities \ Common.cs:line 67

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local) at Utilities.Common.ExportToExcel(DataSet ds, String fileName) in C:\REPOS\Reporting.Tool.Services\Utilities\Common.cs:line 67

WinForm UI Windows Service 都是表示层,并且没有逻辑,因此所有内容都可以在库中使用.

Both WinForm UI and Windows Service are presentation layers and have no logic so all work in the Library.

Windows Service安装程序代码为:

Windows Service installer code is:

namespace SqlToEmailReportingService
{
    partial class ProjectInstaller
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
            this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
            // 
            // serviceProcessInstaller1
            // 
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);
            // 
            // serviceInstaller1
            // 
            this.serviceInstaller1.Description = "Reporting Service";
            this.serviceInstaller1.ServiceName = "ReportingService";
            // 
            // ProjectInstaller
            // 
            this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1});

        }

        #endregion

        private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
        private System.ServiceProcess.ServiceInstaller serviceInstaller1;
    }
}

该服务正在执行许多操作,并且根据日志它们都可以正常工作,但是当保存excel文件时,它将记录主题错误.

The Service is doing many things and according to logs they are all working but when reaching the point of saving the excel file it logs the subject error.

保存文件的库代码为:

public void ExportToExcel(DataSet ds, string fileName)
        {
            // Creating a Excel object. 
            Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

            try
            {

                worksheet = workbook.ActiveSheet;

                worksheet.Name = "ExportedFromDatGrid";

                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    worksheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].ColumnName;
                }

                int cellRowIndex = 2;
                int cellColumnIndex = 1;

                //Loop through each row and read value from each column. 
                for (int i = 0; i < ds.Tables[0].Rows.Count /*- 1*/; i++)
                {
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        worksheet.Cells[cellRowIndex, cellColumnIndex] = ds.Tables[0].Rows[i][j].ToString();
                        cellColumnIndex++;
                    }
                    cellColumnIndex = 1;
                    cellRowIndex++;
                }

                worksheet.Columns.AutoFit();

                _logger.Information("Excel File Generated");

                workbook.SaveAs($"C:\\TemData\\excel_files\\" + fileName + ".xlsx");

            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
            }
            finally
            {
                excel.Quit();
                workbook = null;
                excel = null;
            }

        }

文件夹权限(所有人的完全控制)

有什么建议吗?解决方案?

Any advice? solution?

推荐答案

经过数小时的研究和尝试,解决方案是:

After hours of research and try, the solution is:

看起来像是权限问题;因为WinForm使用的是安装Office的用户,而Windows服务使用的是LocalSystem用户登录.

As it looks like a permission problem; as the WinForm is using the User who installed the Office while the windows service is using the LocalSystem user to log on.

为了允许LocalSystem用户运行Office,我需要在 Microsoft Excel Application Properties 中选择 interactive user 选项.

In order to allow the LocalSystem user to run the Office, I needed to select the interactive user option in the Microsoft Excel Application Properties.

分步操作:

  1. 到达中的MMC组件服务(对于32位操作系统,使用 mmc comexp.msc ,而对于64位操作系统,使用 mmc comexp.msc/32 )运行命令窗口(Windows + R键):
  1. Reach MMC Component Services (For 32 bit OS, use mmc comexp.msc while for 64 bit OS , use mmc comexp.msc /32) in Run a command window (Windows + R keys):

  1. 探索树->选择DCOM Config(右键单击 Microsoft Excel Application 并运行属性)->在身份"标签->中启用交互式用户选项.确定.
  1. Explore the tree --> Select DCOM Config (Right-click on Microsoft Excel Application and run the properties) --> Enable The interactive user option in identity tab --> OK.

这篇关于仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆