Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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#-Excel应用程序仅在Visual Studio上运行_C#_Excel - Fatal编程技术网

C#-Excel应用程序仅在Visual Studio上运行

C#-Excel应用程序仅在Visual Studio上运行,c#,excel,C#,Excel,在我的应用程序上,当我按下一个按钮时,我的应用程序将通过以下方式生成一个excel文件: Excel.Application xl = new Excel.Application(); Excel.Workbook wb = xl.Workbooks.Open(...); Excel.Worksheet sheet = wb.ActiveSheet; 当我在VisualStudio上编译它时,这段代码可以工作。问题是,当我发布代码并将其粘贴到服务器的inetpub文件夹时,它就不起作用了。当我

在我的应用程序上,当我按下一个按钮时,我的应用程序将通过以下方式生成一个excel文件:

Excel.Application xl = new Excel.Application();
Excel.Workbook wb = xl.Workbooks.Open(...);
Excel.Worksheet sheet = wb.ActiveSheet;
当我在VisualStudio上编译它时,这段代码可以工作。问题是,当我发布代码并将其粘贴到服务器的inetpub文件夹时,它就不起作用了。当我按下按钮时,网页不会做任何事情(它不会显示任何错误)。但是我知道我上面提到的第一行代码失败了,因为我正在研究调试

我还测试了我的电脑(安装IIS等),但它也不工作

有什么想法吗?多谢各位

编辑:

我看到以下错误:访问被拒绝:


由于以下错误,无法检索CLSID为{00024500-0000-0000-C000-0000000000 46}的组件的COM类生成器:80070005访问被拒绝。(HRESULT的例外:0x80070005(E_ACCESSDENIED))。

您需要在服务器上安装excel应用程序。

DocumentFormat.OpenXml提供了比Office excel COM库更优雅的API,后者需要在服务器上配置权限和安装Office

添加对DocumentFormat.OpenXml Nuget包的引用,然后在项目中添加下面的函数,将excel文件路径传递给它

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

public static void CreateSpreadsheetWorkbook(string filepath)
    {
        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.
        SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

        // Add a WorkbookPart to the document.
        WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
        workbookpart.Workbook = new Workbook();

        // Add a WorksheetPart to the WorkbookPart.
        WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet(new SheetData());

        // Add Sheets to the Workbook.
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
            AppendChild<Sheets>(new Sheets());

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.
            GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        workbookpart.Workbook.Save();

        // Close the document.
        spreadsheetDocument.Close();
    }
使用DocumentFormat.OpenXml;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
公共静态void CreateSpreadsheetWorkbook(字符串文件路径)
{
//通过提供文件路径创建电子表格文档。
//默认情况下,AutoSave=true、Editable=true和Type=xlsx。
SpreadsheetDocument SpreadsheetDocument=SpreadsheetDocument.Create(文件路径,SpreadsheetDocumentType.工作簿);
//将工作簿部件添加到文档中。
WorkbookPart WorkbookPart=电子表格文档.AddWorkbookPart();
workbookpart.工作簿=新工作簿();
//将工作表部件添加到工作簿部件。
WorksheetPart WorksheetPart=workbookpart.AddNewPart();
worksheetPart.Worksheet=新工作表(new SheetData());
//将工作表添加到工作簿中。
工作表=电子表格Document.WorkbookPart.Workbook。
追加子项(新页());
//附加新工作表并将其与工作簿关联。
工作表=新工作表(){Id=spreadsheetDocument.WorkbookPart。
GetIdOfPart(工作表部分),SheetId=1,Name=“mySheet”};
附页(页);
workbookpart.Workbook.Save();
//关闭文档。
电子表格文档。关闭();
}

您还需要在问题中添加错误详细信息。切勿在服务器上使用Excel automation,它不应该这样使用,更不用说您将面临的授权噩梦了。正如我所说,它不会显示任何错误。我只是在控制台上看到一个错误来编辑它。正如我所说,不要这样做!你需要给你的应用太多的权限。这远比这个复杂。我已经安装了Excel。这不是海报要求的。谢谢你的回复。我正在尝试测试它,但只在第一行“SpreadsheetDocument.Create”上出错。类型“Package”是在未引用的程序集中定义的。“是否安装了DocumentFormat.OpenXml Nuget软件包?是的,我安装了。”。我还添加了usingdocumentformat.OpenXml行;使用DocumentFormat.OpenXml.Packaging;使用DocumentFormat.OpenXml.Spreadsheet;作品比你好多了!!对不起,我没有给你投赞成票,我不能:)我仍然没有必要的声誉