Solved

Excel Automation Using C#

Posted on 2006-11-03
2
1,612 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now