Solved

Excel Automation Using C#

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 28
In C# Using WebClient method how to post certificate from file with password 3 31
Coding C# in Linux 8 60
VB.NET 2008 - SQL Timeout 9 23
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

19 Experts available now in Live!

Get 1:1 Help Now