Reputation: 566
I have a C# feeder that generates some data and sends them to some sort of frontend. Is there a direct way in which I can receive these data using excel microsoft office ? P.S. I do not want to write a code that recieves the data and then writes it into excel sheet. I want to be able to directly get the data from excel itself, as a frontend for my C# backend. Is that ever possible ?!
Upvotes: 1
Views: 1003
Reputation: 8359
I did a quick dr. Google search for WebService calls from Excel and found the following article
Option Explicit
' Excel VBA Function wrapper to call currency conversion Web Service on the web!
Public Function ccyConvert(rsCurrIn As String, rsCurrOut As String,
ByVal vfAmtIn As Single) As Single
Dim objSClient As MSSOAPLib30.SoapClient30
' Remove the 30 if using an earlier version of SOAP
Dim fResult As Single
' Point the SOAP API to the web service that we want to call...
Set objSClient = New SoapClient30
Call objSClient.mssoapinit(
par_WSDLFile:="http://webcontinuum.net/webservices/ccydemo.wsdl")
' Call the web service
fResult = objSClient.calcExcRate(rsCurrIn, rsCurrOut, vfAmtIn)
Set objSClient = Nothing
ccyConvert = fResult
End Function
As Excel has the possibility to access data (from different dataSources) it may be a proper solution to create a c# WebService which delivers the requested data.
Pls note that this is not my code - it's just a copy from the mentioned article - so credit goes to the author!
I just thought about another solution. You may consider making your own C# com component. Within any excel VBA code you are now able to call any method within your assembly. Once again mr. google found an article describing how to achieve this!
c# code for your own com component
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DotNetLibrary
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class DotNetClass
{
public DotNetClass()
{
}
public string DotNetMethod(string input)
{
return "Hello " + input;
}
}
}
VBA code within an excel macro
Private Sub TestDotNetCall()
Dim testClass As New DotNetClass
' or do whatever you want with return value
MsgBox testClass.DotNetMethod(“World”)
End Sub
Upvotes: 1