Solved

Excel Automation Using C#

Posted on 2006-11-03
2
1,617 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:techhound
2 Comments
 
LVL 2

Accepted Solution

by:
aquila98 earned 500 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
 

Author Comment

by:techhound
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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