{"id":817,"date":"2013-11-25T15:13:37","date_gmt":"2013-11-25T15:13:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=817"},"modified":"2013-11-26T11:57:08","modified_gmt":"2013-11-26T11:57:09","slug":"creating-an-xll-add-in-using-excel-dna","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-an-xll-add-in-using-excel-dna\/","title":{"rendered":"Creating an XLL add-in using Excel-DNA"},"content":{"rendered":"<p>On a previous post I walked through creating an XLL add-in using C# and ManagedXLL for Excel. This is a commercial library which offers many options for interacting and integrating with Excel. But there&#8217;s a nice, open source, alternative called Excel-DNA, if you prefer to go this way. <\/p>\n<p>So let&#8217;s take a look at creating a Excel add-in using Excel-Dna.<\/p>\n<ol>\n<li>Let&#8217;s start off by creating a class library in C# (I&#8217;m going to call mine ExcelDNATest, I know it&#8217;s not very original)<\/li>\n<li>Using NuGet, add the Excel-DNA package to your solution<\/li>\n<li>Copy and rename packages\\Excel-DNA.0.30.3\\tools\\ExcelDna.xll to projectname-AddIn.xll, so in my case that&#8217;s ExcelDNATest-AddIn.xll<\/li>\n<li>We need to copy this file to the output folder so either create a post build step to copy this or I simply added it to the project and marked it&#8217;s &#8220;Copy to Output Directory&#8221; to &#8220;Copy if newer&#8221;<\/li>\n<li>You&#8217;ll notice that the NuGet package added a file named projectname-AddIn.dna to the project. This is the config file for the XLL add-in we&#8217;re creating. If you open it you should see an ExternalLibrary element which has the name of the DLL we&#8217;re going to build.<\/li>\n<\/ol>\n<p>At this point we&#8217;ve created the barebones for our DLL which the information which allows the XLL file to call into our DLL which we shall use to contain the Excel functions.<\/p>\n<p>You should have found the NuGet package added a file ExcelDna.Integration.dll to our project. This contains the attributes and other code which allows us to expose Excel functions from our code and for us to talk to Excel.<\/p>\n<p>So at this point, to recap, we&#8217;ve created a DLL, we&#8217;ve added Excel-DNA to the project. We&#8217;ve copied\/renamed the XLL file to match out project and ensure the .dna configuration file points to our DLL name. Now it&#8217;s time to have a little more fun&#8230;<\/p>\n<p>Let&#8217;s add a function to our class and usable from Excel<\/p>\n<ol>\n<li>Rename your Class1.cs file to MyFunctions.cs (or whatever you prefer)<\/li>\n<li>Open the class file make the class static<\/li>\n<li>Add a static method as follows\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ExcelFunction(Name=&quot;MyFunctions.Hello&quot;, Description=&quot;Hello X&quot;, \r\nCategory=&quot;My Excel Functions&quot;]\r\npublic static string Hello(&#x5B;ExcelArgument(&quot;Enter your name&quot;)] string name)\r\n{\r\n   return &quot;Hello &quot; + name;\r\n} \r\n<\/pre>\n<p>We&#8217;ve now got a function which will appear in the Excel function wizard in the category &#8220;My Excel Functions&#8221;. The function will be named MyFunction.Hello and has the description &#8220;Hello X&#8221;.\n<\/li>\n<li>Simply type\n<p>=MyFunctions.Hello(&#8220;Mark&#8221;)<\/p>\n<p>in your spreadsheet cell to see the function in action.\n<\/ol>\n<p><strong>Alternatively create the code in the DNA file<\/strong><\/p>\n<p>I&#8217;m not sure how useful this is, but you can also create UDF (user-defined functions) inside the .DNA file.<\/p>\n<p>For example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;DnaLibrary Name=&quot;ExcelDNATest Add-In&quot; RuntimeVersion=&quot;v4.0&quot; Language=&quot;CS&quot;&gt;\r\n  &lt;!&#x5B;CDATA&#x5B;\r\n  using ExcelDna.Integration;\r\n  \r\n  public class SomeMoreFunctions\r\n  {\r\n     &#x5B;ExcelFunction(Description=&quot;Test Function&quot;)]\r\n     public static string Test()\r\n     {\r\n        return &quot;This is a test function&quot;;\r\n     }\r\n  }\r\n  ]]&gt;\r\n&lt;\/DnaLibrary&gt;\r\n<\/pre>\n<p>And the rest is as per the compiled source code, i.e. it&#8217;s available in the function wizard etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a previous post I walked through creating an XLL add-in using C# and ManagedXLL for Excel. This is a commercial library which offers many options for interacting and integrating with Excel. But there&#8217;s a nice, open source, alternative called Excel-DNA, if you prefer to go this way. So let&#8217;s take a look at creating [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[54,60],"tags":[],"class_list":["post-817","post","type-post","status-publish","format-standard","hentry","category-excel","category-excel-dna"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=817"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/817\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/817\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}