C# - Excel Application is only working on Visual Studio

0

On my APP, when I press one button, my APP generate one excel file by:

Excel.Application xl = new Excel.Application();
Excel.Workbook wb = xl.Workbooks.Open(...);
Excel.Worksheet sheet = wb.ActiveSheet;

This code works when I compile it on Visual Studio. The problem is that, when I publish my code and I paste it on inetpub folder of my server, it doesn't work. When I press the button, the webpage doesn't do anthing (it doesn't show any error). But I know the code is failing on the first line I put above because I was studying it debugging.

I also tested it on my PC (installing IIS, etc) but it doesn't work neither...

Any idea? Thank you.

EDIT:

I saw the following error: Access denied:

The COM class generator for the component with CLSID {00024500-0000-0000-C000-000000000046} could not be retrieved due to the following error: 80070005 Access denied. (Exception of HRESULT: 0x80070005 (E_ACCESSDENIED)).

c#
excel
asked on Stack Overflow Nov 11, 2019 by Daniel R • edited Nov 11, 2019 by Daniel R

2 Answers

0

DocumentFormat.OpenXml provides more elegant API than Office excel COM library which requires configuring permissions and installing office on the server.

Add a reference to DocumentFormat.OpenXml Nuget package then add the bellow function in your project, pass your excel file path into it.

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();
    }
answered on Stack Overflow Nov 11, 2019 by Voice Of The Rain • edited Nov 11, 2019 by Voice Of The Rain
-1

you need to install excel application in your server.

answered on Stack Overflow Nov 11, 2019 by Hamid

User contributions licensed under CC BY-SA 3.0