[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel Automation Using C#

Posted on 2006-11-03
2
Medium Priority
?
1,623 Views
Last Modified: 2012-06-27
This is a bit complex so I'll try to explain as best I can:

I want to use Excel Automation inside ASP.NET.  I added the Microsoft.Excel COM object to my solution and everything compiled fine.  When I go to run a page that has an object that includes the automation I get the following error:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

All I really want to be able to do is open a workbook (already populated) and use Excel's WORKDAY function.  The question is, can Excel be used inside of an ASP.NET page (via an object, etc.)   I am not locked to this solution so if someone has a better way to do this or an C# implementation of WORKDAY that is just as good (or even better).

Thanks
0
Comment
Question by:James Cochrane
2 Comments
 
LVL 2

Accepted Solution

by:
aquila98 earned 2000 total points
ID: 17869550
Hello

Try this:
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Collections.Generic
Imports Microsoft.Office.Interop.Excel

Public Class TableLoader
    Public Function Load(ByVal path As String, ByRef pctfree As Integer, ByRef pctused As Integer, ByRef definition As String) As ArrayList
        Dim _Ret As ArrayList = New ArrayList
        Dim _XLBook As Workbook
        Dim _XLApp As Application
        Dim _XLSheet As Worksheet
        Dim _XLCell As Range
        Dim i As Integer
        Dim j As Integer
        Dim aTable As Table
        Dim tmp As String


        _XLApp = New Application
        _XLBook = _XLApp.Workbooks.Open(path)
        _XLSheet = CType(_XLBook.Worksheets("Sheet1"), Worksheet)
        _XLCell = CType(_XLSheet.Cells(2, 2), Range)
        pctfree = CType(_XLCell.Value2, Integer)
        _XLCell = CType(_XLSheet.Cells(3, 2), Range)
        pctused = CType(_XLCell.Value2, Integer)
        _XLCell = CType(_XLSheet.Cells(7, 1), Range)
        tmp = CType(_XLCell.Value2, String)
        i = 1
        definition = ""
        While tmp <> "" And i < 50 ' 50 lines maximum
            definition = definition + tmp + vbCrLf
            _XLCell = CType(_XLSheet.Cells(7 + i, 1), Range)
            tmp = CType(_XLCell.Value2, String)
            i = i + 1
        End While

        _XLBook.Close()
        _XLApp.Quit()
        Return _Ret
    End Function

End Class

Is it VB.Net code but it is not that hard to code equivalence in C#. You will need the following references:
Microsoft excel 11.0 object library (com)
and microsoft office object library (com)
microsoft visual basic for applications extensinility (com)
microsoft.office.tools.excel (.net)
microsoft.visualstudio.tools.application.runtime
0
 
LVL 1

Author Comment

by:James Cochrane
ID: 17869835
I found the solution on the Web.  Excel was not configured for DCOM for user ASPNET.  See http://blog.crowe.co.nz/archive/2006/03/02/589.aspx. I'll give you credit because this is a valid alternative solution.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question