Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Adding a C# method as an Excel function

Hi,

This is a newbie question - be warned :)

I need to make a method that I've written in C# available to Excel
users as a function. I have: "public string encryptSomething(string
something)" and I'd like to make it visible to Excel so users can put
"=encryptSomething(A1)" in a cell and, well, encrypt something.

I know I can do this by writing a hybrid between C# and C++ to create
an xll file. I know that's how ExcelDna (http://exceldna.typepad.com/)
does its magic. Can someone point me to a simpler example of this
technique?

And shouldn't there be a more elgant way to do this? Something that
wouldn't involve C++?

Will VSTO give me this functionality? If so, will VSTO require my end
users to install something apart from whatever DLL/XLL I will be
shipping to them?


Thanks!
urig.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Adding a C# method as an Excel function

"Something simpler" for me would be to use VBA and call the the Windows APIs
that can deal with encryption, those called Crypt*.
Here's a couple of examples:
http://simplesamples.info/VB/EncryptDecrypt.php
http://www.ex-designz.net/apidetail.asp?api_id=394
You can wrap the code in a class and make to an add-in also, but all code
would be VBA.

If you need to use one of the .Net languages, then AFAIK you need something
compiled; xll, dll (ActiveX or Standard).

NickHK

"urig" wrote in message
ups.com...
Hi,

This is a newbie question - be warned :)

I need to make a method that I've written in C# available to Excel
users as a function. I have: "public string encryptSomething(string
something)" and I'd like to make it visible to Excel so users can put
"=encryptSomething(A1)" in a cell and, well, encrypt something.

I know I can do this by writing a hybrid between C# and C++ to create
an xll file. I know that's how ExcelDna (http://exceldna.typepad.com/)
does its magic. Can someone point me to a simpler example of this
technique?

And shouldn't there be a more elgant way to do this? Something that
wouldn't involve C++?

Will VSTO give me this functionality? If so, will VSTO require my end
users to install something apart from whatever DLL/XLL I will be
shipping to them?


Thanks!
urig.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding a C# method as an Excel function


Hi Urig,

Using 'ExcelDna' (http://exceldna.typepad.com) does not mean you hav
to create or compile any C++ code. Your user-defined functions, writte
in C# or VB.NET, can be either directly in a text-based script file, o
can be in a compiled library. You have to write nothing more than th
implementation of your functions. The ExcelDna library automaticall
exposes these functions and macros to Excel.

VSTO does not help with creating user-defined functions for Excel.

An alternative approach which does not require any third-party library
is to make a COM Automation Add-In. You can find some details o
'CodeProject' (http://www.codeproject.com/dotnet/excelnetauto.asp).

Regards,
Gover

--
gover
-----------------------------------------------------------------------
govert's Profile: http://www.officehelp.in/member.php?userid=622
View this thread: http://www.officehelp.in/showthread.php?t=132749

Posted from - http://www.officehelp.i

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Adding a C# method as an Excel function

Thanks for your replies NickHK and govert.

Special thanks go to you govert, for having written the wonderful
ExcelDna. I've had a look at your source code and the work you've done
is robust and impressive by any standard!

Currently, ExcelDna is my fallback. Its only drawbacks are that it
exposes my code to tampering by end clients and that it requires me to
distribute a few additional dlls. I would still rather have a my
deliverable consist of a single installable XLL file.

I was surprised to see how much work you had to put into ExcelDna to
register a new C# user-defined function into Excel. How come VSTO
doesn't supply this functionality? Maybe it does so only for Excel
2007? How come it's so hard to hook up .net code into MS Excel?


Regards,
urig




On Jan 24, 2:40 pm, govert wrote:
Hi Urig,

Using 'ExcelDna' (http://exceldna.typepad.com) does not mean you have
to create or compile any C++ code. Your user-defined functions, written
in C# or VB.NET, can be either directly in a text-based script file, or
can be in a compiled library. You have to write nothing more than the
implementation of your functions. The ExcelDna library automatically
exposes these functions and macros to Excel.

VSTO does not help with creating user-defined functions for Excel.

An alternative approach which does not require any third-party library,
is to make a COM Automation Add-In. You can find some details on
'CodeProject' (http://www.codeproject.com/dotnet/excelnetauto.asp).

Regards,
Govert

--
govert
------------------------------------------------------------------------
govert's Profile:http://www.officehelp.in/member.php?userid=6221
View this thread:http://www.officehelp.in/showthread.php?t=1327494

Posted from -http://www.officehelp.in


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding a C# method as an Excel function

Hi Uhrig,

Currently, ExcelDna is my fallback. Its only drawbacks are that it
exposes my code to tampering by end clients and that it requires me to
distribute a few additional dlls. I would still rather have a my
deliverable consist of a single installable XLL file.


Not sure what you mean with the tampering issue. Easy decompilation is
a property of .NET assemblies, and you should look at obfuscation
tools to address this. But this issue is independent of how you
integrate into Excel.
Indeed for ExcelDna you currently have to ship an extra .dll file and
a text file, and address the .Net version loading issue -- I would
like to simplify this in future. If you have particular requirements,
please let me know.

How come VSTO doesn't supply this functionality?
Maybe it does so only for Excel 2007?

I know of no plans to support the creation of UDFs in VSTO add-ins,
also not for Excel 2007. But the .XLL interface (that ExcelDna uses)
got a significant upgrade in Excel 2007, so it should be a great way
to integrate into Excel for the foreseeable future.

How come it's so hard to hook up .net code into MS Excel?

It's not hard at all, and there are many ways to do it. You can:
1. Reference a .Net library in VBA code, and create functions VBA
functions that call your library.
2. Create an Automation Add-In and integrate into Excel using COM
interop.
3. Create a .XLL add-in using C/C++ and reference your .NET code
(using the It-Just-Works C++/CLR binding).
4. Use ExcelDna which is easy, open-source and free (including for
commercial use).
5. Use the commercial ManagedXll library, which is expensive but has a
very broad range of features.
For the last two, you need to write no additional code to expose your
functions to Excel, you just add some attributes in your code. Seems
pretty easy to me.

Regards,
Govert

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a toolbar function to an excel cell Colin Hayes Excel Worksheet Functions 5 January 13th 10 12:28 AM
How about adding a quarterly function to Excel like its EOMonth? SDolley Excel Worksheet Functions 1 March 30th 05 06:58 PM
what was name of that function/method?? Bing Excel Programming 5 December 24th 04 06:06 PM
Need a method or function Eric Marple Excel Programming 2 August 9th 04 01:31 PM
Adding a custom function to the default excel function list DonutDel Excel Programming 3 November 21st 03 03:41 PM


All times are GMT +1. The time now is 06:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"