Wednesday, June 28, 2017

Step By Step : Excel-DNA, C#, Excel User Defined Functions

Hi,

Welcome to my blog of Excel DNA using Visual Studio and C#. In this post we will see how to
create Excel User Defined Functions ( UDFs ) using Visual Studio and C# by using Excel-DNA.

Introduction: 
As per https://excel-dna.net/, Excel-DNA is an independent project to provide high-performance user-defined functions ( UDFs ), create custom ribbon bar and more to your Excel sheet. It allows to develop native .xll add-ins for Excel using C#, VB.NET, F#. It helps to integrate .NET into Excel.

Prerequisites:
Since we are integrating .NET into Excel we need Visual Studio. We can get Visual Studio 2015 Community Edition for free at https://beta.visualstudio.com/downloads/.

Once you install Visual Studio you are ready to go. So let's dig in and see step-by-step guide of creating a user-defined function ( UDF ) and execute that in Excel.

Create Project:
Launch Visual Studio. You need to create a Class Library Project. You can create by going to
File -> New -> Project and select Visual C# and then select Class Library project and give it a name like MyExcelDNALibrary. Please see below
























This creates a new solution for you like below
























Next step you need to do is add Excel-DNA add-in to your class library project. This can be done using NuGet Package Manager ( this is included in most of the recent visual studio versions).

In your Visual Studio, go to View menu at the top and the Other Windows and select Package Manager Console. This will open Package Manager Console pane. Please see below


















Note : In Package Manager Console make sure you select the correct project under default project dropdown. In above image the red circled one.

Give the following command next to PM> in Package Manager Console and press Enter 
Install-Package Excel-DNA. 
You will get a bunch of text successful and complete messages and Excel-DNA add-in will
be added to your project.










Now your Solution Explorer must look like below




















This step is optional. Open Solution Explorer and right click on Class1.cs and then select Rename to rename class to UDFHelper.




























Now double click above class to open it and give using ExcelDna.Integration; at the top. Your class must look like below


























To make things simple, let's add a function that gets current date and time like below

    [ExcelFunction(Name = "CURRENTDATETIME")]
    public static DateTime CurrentDateTime()
    {
        return DateTime.Now;
    }

Lets also add a function that adds 2 numbers and returns result
       [ExcelFunction(Name = "ADDTWONUMBERS")]
        public static int Add(int a , int b)
        {
            return a + b;
        }

Now your class must look like below





























ExcelFunction tells that the function must be exposed in Excel as work sheet function. 
[ExcelFunction(Name = "CURRENTDATETIME")] tells that .NET C# CurrentDateTime function must be exposed in Excel as CURRENTDATETIME.
[ExcelFunction(Name = "ADDTWONUMBERS")] tells that .NET C# Add function must be exposed in Excel as ADDTWONUMBERS.

In Solution Explorer right-click on your Excel-DNA Class Library project and select Properties. This will open properties tab. Go to Debug and make sure you have Excel.exe location
for Start external program. In my case it was C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE

If you have 32-bit Excel then you can skip below steps and go directly to testing. If you have 64-bit Excel please follow bekow steps.

64-bit Excel:
In Solution Explorer right-click on your Excel-DNA Class Library project and select Properties. This will open properties tab. Go to Debug.













In Command line arguments : add 64 before .xll  for example "MyExcelDNALibrary-AddIn64.xll" like below. This will tell that 64-bit version XLL will be loaded in Excel when you debug the application else you will get error



















Test:
We are now ready to test. In Visual Studio, in command bar make sure you select Debug and then click on the green triangle to run the project or else press F5 on keyboard to run application.

















If you donot have any compile errors, your solution will be compiled and Excel-DNA creates few xll files. Excel should be launched and depending on your security settings it will ask whether
to enable the add-in. Just select Enable button. This will open your Excel session.




















Select a cell in excel and give =CURRENTDATETIME() like below. ( If everything goes fine Excel intellisense will show the method like below).

















Select method like below to see result.


















In order to format cell, select cell and right-click and follow below


































Now you must see sensible date like below





















To use Add follow below




















Conclusion :
In this article we saw how we can create user defined functions (UDFs) and use them in Excel using Excel-DNA add-in. We created functions using .NET and C#. In future post I will show you how to create custom ribbon bar in Excel and how we can add buttons to it and perform some functionality like open a web-browser and launch a sample page. Till then I hope you find this post useful.


I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.


Saturday, February 25, 2017

Step by Step : Windows Phone 7 : Setting Up Development Environment

Hi,
Welcome to my blog of Windows Phone 7 Development. In this series of posts, I will try to explain various concepts in Windows Phone 7 Development. We will try to learn how to develop applications for Windows Phone 7.  So lets start.

In this post we will see how to set up Windows Phone 7 Development environment.

Introduction: 
Windows Phone 7 Development targets developing applications for Windows Phone 7 mobile operating system developed by Microsoft..NET framework is used to do Windows Phone 7 Development created by Microsoft. We can use any one of the several languages supported by the .NET framework, like C# to write applications which gets executed inside of a runtime environment called the Common Language Runtime. There are two distinct development approaches you can take when creating your application.

First Approach:
The first approach is to use Silverlight for Windows Phone. Silverlight is used by developers to create rich internet applications. A Silverlight application uses declarative markup (called XAML) to create user interface and code written in a .NET framework language to control an application’s behavior. For developing a data driven application for Windows Phone 7, we need to use Silverlight.

Second Approach:
The second approach is to use the XNA framework to develop your Windows Phone 7 app. It is Microsoft’s game development framework and has been used in recent years to create both Windows and Xbox 360 applications. If you’re creating a game for Windows Phone 7, you’ll likely use the XNA framework. The XNA framework is quite powerfu and need a great deal of learning curve and longer development cycles.

Installing SDK:
The Windows Phone 7 SDK is supported with one of the below operating systems except starter ( express ) versions
  • Windows Vista
  • Windows 7
Note : You cannot develop Windows Phone 8 applications using Windows Phone 7 SDK.
Hardware Requirements :
  • Minimum 4GB hard disk space
  • Minimum 3GB RamDirectX 10 or above capable graphics card with a WDDM 1.1 driver
Please go to below link and download and install below items
http://dev.windows.com/en-us/develop/download-phone-sdk
  • Windows Phone SDK 7.1
  • Windows Phone SDK 7.1.1 Update















This will install the following in your system
  • Microsoft Visual Studio 2010 Express For Windows Phone
  • Windows Phone Emulator
  • Windows Phone SDK 7.1 Assemblies
  • Silverlight 4 SDK and DRT
  • Windows Phone SDK 7.1 Extensions for XNA Game Studio 4.0
  • Micorosoft Expression Blend SDK for Windows Phone 7
  • Microsoft Expression Blend SDK for Windows Phone OS 7.1
  • WCF Data Services Client for Windows Phone
  • Microsoft Advertising SDK for Windows Phone
If your installation goes fine you will have all the necessary software requirements that are needed to develop Windows Phone 7 mobile applications.

Conclusion:
In this post we saw what are the hardward and software requirements to develop Windows Phone 7 applications and how we can install Windows Phone 7 SDK that will install the required components to start developing Windows Phone 7 applications.

In next post we will develop our first Windows Phone 7 application which is Hello World application.
I hope you enjoyed reading this post as much as I did while writing it. Please feel free to let me know your valuable and constructive comments and suggestions which can help me to make this article better and improve my technical and written skills. Last but not the least please excuse me for my grammar and typos.

Thanks and have a nice and wonderful day.