Solved

Excel Automation Using C#

Posted on 2006-11-03
2
1,618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 98
array not updating 8 39
Load XML element 3 41
Concurrency Error Persistent using Data Grid View Update C# 2 23
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Suggested Courses

740 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