ExcelDna 记录
1. NuGet安装ExcelDna.AddIn, 输入以下代码,这样就可以使用用户自定义函数了。要求不高的话,到此设置就可以用了。
using ExcelDna.Integration; public static class MyFunctions { [ExcelFunction(Description = "My first .NET function", IsVolatile = false, IsThreadSafe = true)] public static string SayHello(string name) { return "Hello " + name; } }
2. 添加代码提示。安装ExcelDna.IntelliSense,新建一个AddIn.cs,放以下代码。
using ExcelDna.Integration; using ExcelDna.IntelliSense; public class AddIn : IExcelAddIn { public void AutoOpen() { IntelliSenseServer.Install(); } public void AutoClose() { IntelliSenseServer.Uninstall(); } }
然后在函数和函数的参数上添加Description属性
[ExcelFunction(Description = "我的第一个自定义函数", Name = "Hello")] public static string Hello([ExcelArgument(Description = "请输入名字")] string name) { return $"Hello, {name}"; }
3. 异步函数。默认ExcelDna不支持返回值为async Task<string>的函数,所以直接这样写以下的代码是不能运行的。
using System.Threading.Tasks; namespace ExcelHelper { public static class MyFunctions { [ExcelFunction] public static async Task<string> MyFuncAsync(object o) { return await SomeOtherFuncAsync(o); } } }
有两种办法可以解决这个问题:
1.1 第一种方法:使用ExcelAsyncUtil.RunAsTask方法
using ExcelDna.Integration; namespace ExcelHelper { public static class MyFunctions {
[ExcelFunction] public static string MyFunction(object o) { return ExcelAsyncUtil.RunAsTask(nameof(MyFunction), new object[] { o }, () => { return SomeOtherFuncAsync().Result; }); } } }
1.2 推荐使用第二种方法,这种方法同时还可以设置异步函数返回结果之前临时显示一个字符串如”处理中..."。
NuGet安装 ExcelDna.Registration,在配置文件中加入“显式声明函数”。
如果是.net Framework 4.X,在ExcelHelper-AddIn.dna里加上ExplicitRegistration="true",如下:
<?xml version="1.0" encoding="utf-8"?> <DnaLibrary Name="ExcelHelper Add-In" RuntimeVersion="v4.0" xmlns="http://schemas.excel-dna.net/addin/2020/07/dnalibrary"> <ExternalLibrary ExplicitRegistration="true" Path="ExcelHelper.dll" ExplicitExports="false" LoadFromBytes="true" Pack="true" IncludePdb="false" /> </DnaLibrary>
如果是.net,在.csproj文件中加入<ExcelAddInExplicitRegistration>true</ExcelAddInExplicitRegistration>,如下:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net8.0-windows</TargetFrameworks> <ExcelAddInExplicitRegistration>true</ExcelAddInExplicitRegistration> </PropertyGroup> <ItemGroup> <PackageReference Include="ExcelDna.Addin" Version="*-*" /> <PackageReference Include="ExcelDna.Registration" Version="*-*" /> </ItemGroup> <ItemGroup> <Folder Include="Properties\" /> </ItemGroup> </Project>
然后把AddIn.cs改为如下
using ExcelDna.Integration; using ExcelDna.Registration; using ExcelDna.IntelliSense; using System; using System.Linq.Expressions; public class AddIn : IExcelAddIn {
// 这个函数用于在异步函数返回结果前临时显示"processing..." static ParameterConversionConfiguration GetPostAsyncReturnConversionConfig() { string rval = "process..."; return new ParameterConversionConfiguration().AddReturnConversion((type, customAttributes) => type != typeof(object) ? null : ((Expression<Func<object, object>>)((object returnValue) => returnValue.Equals(ExcelError.ExcelErrorNA) ? rval : returnValue))); } public void AutoOpen() { var postAsyncReturnConfig = GetPostAsyncReturnConversionConfig(); ExcelRegistration.GetExcelFunctions() .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false) .ProcessParameterConversions(postAsyncReturnConfig) .RegisterFunctions(); IntelliSenseServer.Install(); } public void AutoClose() { IntelliSenseServer.Uninstall(); } }
浙公网安备 33010602011771号