Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC_C#_.net_Filesystems_Windows Services_Excel Interop - Fatal编程技术网

C# 仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC

C# 仅在Windows服务中:System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC,c#,.net,filesystems,windows-services,excel-interop,C#,.net,Filesystems,Windows Services,Excel Interop,我正在开发一个应用程序,生成一个报告并将其保存为文件夹中的Excel文件 该解决方案由逻辑库、WinForm UI和Windows服务组成 逻辑库(.netframework4.7.2)中的核心工作 我的电脑上安装的Office是Office 2007,操作系统是Windows 10 Professional 64位 UI应用程序生成并保存Excel文件,而Windows服务生成错误: System.Runtime.InteropServices.COMException(0x800A03EC)

我正在开发一个应用程序,生成一个报告并将其保存为文件夹中的Excel文件

该解决方案由
逻辑库
WinForm UI
Windows服务
组成

逻辑库(
.netframework4.7.2
)中的核心工作

我的电脑上安装的Office是
Office 2007
,操作系统是
Windows 10 Professional 64位

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

System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC 在Microsoft.Office.Interop.Excel.\u工作簿.SaveAs(对象文件名、对象文件格式、对象密码、对象写入密码、对象只读建议、对象创建备份、XLSAVEASACCESS模式访问模式、对象冲突解决、对象添加模式、对象文本代码页、对象文本可视化页面、对象本地) 在C:\REPOS\Reporting.Tool.Services\Utilities\Common.cs中的Utilities.Common.ExportToExcel(数据集ds,字符串文件名)处:第67行

winformui
Windows服务
都是表示层,没有逻辑,因此都在库中工作

Windows服务安装程序代码为:

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;
    }
}
名称空间SQLTOEMAILLReportingService
{
部分类投影装入器
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
///  
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.serviceProcessInstaller1=新系统.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1=新系统.ServiceProcess.ServiceInstaller();
// 
//serviceProcessInstaller1
// 
this.serviceProcessInstaller1.Account=System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password=null;
this.serviceProcessInstaller1.Username=null;
this.serviceprocesinstaller1.AfterInstall+=新系统.Configuration.Install.InstallEventHandler(this.serviceprocesinstaller1\u AfterInstall);
// 
//服务安装程序1
// 
this.serviceInstaller1.Description=“报告服务”;
this.serviceInstaller1.ServiceName=“ReportingService”;
// 
//投射器
// 
this.Installers.AddRange(新的System.Configuration.Install.Installer[]{
此文件为.serviceProcessInstaller1,
此文件为.serviceInstaller1});
}
#端区
专用系统.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
专用System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
}
该服务正在做很多事情,根据日志,它们都在工作,但当到达保存excel文件的点时,它会记录主题错误

保存文件的库代码为:

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

        }
public void ExportToExcel(数据集ds,字符串文件名)
{
//创建Excel对象。
Microsoft.Office.Interop.Excel.\u Application Excel=新的Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.\u工作簿=Excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel.\u工作表=null;
尝试
{
工作表=工作簿.ActiveSheet;
工作表.Name=“ExportedFromDatGrid”;
对于(int j=0;j
文件夹权限(所有人完全控制)


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

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

因为这看起来像是一个许可问题;因为WinForm正在使用安装Office的用户,而windows服务正在使用LocalSystem用户登录

为了允许LocalSystem用户运行Office,我需要在
Microsoft中选择
交互式用户
选项