Calls .NET methods from add-in functions
Please note that if you are using Visual Studio 2005, you must have Service Pack 1 installed in order to build a valid XLL.
If the sample fails to load into Excel at run-time, please see the technical note .NET requirements.
This project contains two add-in functions that use the Common Language Runtime and the .NET class libraries.
For more information on calling .NET code from add-in functions, see Using .NET and the Common Language Runtime in the User Guide.
The worksheet function ClrDate
converts an Excel date to
an instance of System::DateTime, and then uses the method
System::DateTime::ToString(format) to return a CLR formatted date string.
CXlOper* ClrDate_Impl(CXlOper& xloResult, double date, const CXlStringArg& format) { // End of generated code //}}XLP_SRC // Call a CLR method, using toClr() to translate the arguments // & toXl() to translate the result. xloResult = toXl(toClrDate(date).ToString(toClr(format))); return xloResult.Ret(); }
Inputs are converted to CLR types using the global functions toClr() and toClrDate(). The output of the CLR method (a System::String) is converted back to an Excel string using toXl().
The macro command ShowDialog
calls the
WinForms library method System::Windows::Forms::MessageBox::Show.
An instance of ExcelForm is created,
to pass the Excel application window's handle to the WinForms
library, so that the message box has the correct owner window.
CXlOper* ShowDialog_Impl(CXlOper& xloResult) { // End of generated code //}}XLP_SRC // Call a WinForms method, using a new instance of // the ExcelForm to create an IWin32Window interface // for the main Excel application window. MessageBox::Show( gcnew XllPlus::Forms::ExcelForm(), "Hello from .NET", ".NET message box", MessageBoxButtons::OK, MessageBoxIcon::Hand); return xloResult.Ret(); }
In order to make this project build and run, the following step was required:
The following steps were required in order to use WinForms:
system.windows.forms.dll
,
so that the WinForms library is available.
(You can add an assembly reference by opening the
"Common Properties/References" page of the Project Properties dialog
and clicking "Add New Reference...".)
#include <xlpclrforms.h>
to ClrDemo.cpp,
to gain access to the ExcelForm class.
using namespace System::Windows::Forms;
was added.
ExcelForm | ple::clr::toClr | ple::clr::toClrDate | ple::clr::toXl
Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file ClrDemo.sln or the project file ClrDemo.vcproj.
You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.
When delivered, the help files are excluded from the build.
You can enable the help build by selecting the files
ClrDemo.help.xml
and
ClrDemo.chm
in the Solution Explorer,
and using the right-click menu to view Properties.
Select the page "Configuration Properties/General" and
set the "Excluded from build" property to "No".
See Generating help
in the User Guide for more information.