首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

用C#开发Excel插件的强大开源工具

推荐一个开源项目,方便我们使用C#为Excel开发插件。

01

项目简介

Excel-DNA是一个.Net开源项目,为开发者提供了一种便利的方法,可以将.Net代码与Excel集成,能够轻松的为Excel创建自定义函数、图表、表单等,一方面不仅可以利用.Net强大的库,另外一方面还可以与外部数据、程序等连接交互。

利用该项目我们可以用来,开发一些数据分析与处理、数据自动化等一些场景。

02

使用示例

1、创建一个类型:类库的项目,这边选择.Net 6。

2、修改项目文件ClassLibrary.csproj

修改TargetFramework修改为net6.0-windows,示例代码如下:

<PropertyGroup>

<TargetFramework>net6.0-windows<!--TargetFramework></code><code> <ImplicitUsings>enable<!--ImplicitUsings></code><code> <Nullable>enable<!--Nullable></code><code>  <!--PropertyGroup></code><code></P><P></code><code><!--Project></code></pre></p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>3、添加依赖库:</strong>Excel-DNA。</p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>4、创建类并添加如下代码:</strong></p><p class="code-snippet__fix code-snippet__js"><P class="code-snippet__line-index code-snippet__js"><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P><P></P></P><pre class="code-snippet__js" data-lang="cs"><code>using ExcelDna.Integration;</code><code></P><P></code><code>public static class MyFunctions</code><code>{</code><code> [ExcelFunction(Description = ".Net自定义方法")]</code><code> public static string SayHello(string name)</code><code> {</code><code> return "Hello " + name;</code><code> }</code><code></P><P></code><code> [ExcelFunction(Description = ".Net自定义方法")]</code><code> public static int AddExt(int num1, int num2)</code><code> {</code><code> return num1 + num2;</code><code> }</code><code>}</code></pre></p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>5、运行项目,</strong>并在对话框选择:仅为本对话启用此加载项。</P><P></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><img class="rich_pages wxw-img" data-galleryid="" data-imgfileid="302460414" data-ratio="0.8545034642032333" data-s="300,640" data-src="https://inews.gtimg.com/om_bt/OlH3Ac9sKww68DBRp0M4udRg8iO2DUJNcoEw7-SjLAhCEAA/641" data-type="png" data-w="433" src="https://inews.gtimg.com/om_bt/OlH3Ac9sKww68DBRp0M4udRg8iO2DUJNcoEw7-SjLAhCEAA/641" style=""/></p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>6、使用效果</strong></P><P></p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>SayHello自定义方法:</strong></p><p style="text-align: left;margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><strong>AddExt自定义方法:</strong></p><p style="text-align: center;margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"><img class="rich_pages wxw-img" data-galleryid="" data-imgfileid="302460418" data-ratio="0.3713768115942029" data-s="300,640" data-src="https://inews.gtimg.com/om_bt/Odx_No7fmNpYDoV-YQk64HFC_ggEpsVTO8u2L0Ab8it8gAA/641" data-type="png" data-w="552" src="https://inews.gtimg.com/om_bt/Odx_No7fmNpYDoV-YQk64HFC_ggEpsVTO8u2L0Ab8it8gAA/641" style=""/></p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;"> </p><p "hiragino="" "microsoft="" "pingfang="" arial,="" center;visibility:="" gb",="" helvetica="" neue",="" sans="" sans-serif;text-align:="" sc",="" style="margin-right: 8px;margin-bottom: 0em;margin-left: 8px;outline: 0px;letter-spacing: 0.544px;text-wrap: wrap;color: rgb(34, 34, 34);line-height: 1.6em;background-color: rgb(255, 255, 255);font-family: -apple-system-font, BlinkMacSystemFont, " ui",="" visible;"="" yahei="" yahei",="">03</p><p "hiragino="" "microsoft="" "pingfang="" arial,="" center;visibility:="" gb",="" helvetica="" neue",="" sans="" sans-serif;text-align:="" sc",="" style="margin-right: 8px;margin-left: 8px;outline: 0px;letter-spacing: 0.544px;text-wrap: wrap;color: rgb(34, 34, 34);line-height: 1.6em;background-color: rgb(255, 255, 255);font-family: -apple-system-font, BlinkMacSystemFont, " ui",="" visible;"="" yahei="" yahei",="">项目地址</p><p style="margin-left: 8px;margin-right: 8px;margin-bottom: 8px;">https://github.com/Excel-DNA/ExcelDna</p><p style="margin-right: 8px;margin-bottom: 8px;margin-left: 8px;outline: 0px;text-wrap: wrap;-webkit-font-smoothing: antialiased;font-family: PingFang-SC-Regular;list-style: none;font-size: 15px;line-height: 1.75;color: rgb(5, 7, 59);letter-spacing: normal;text-align: start;background-color: rgb(253, 253, 254);visibility: visible;"><img class="rich_pages wxw-img" data-imgfileid="302460419" data-ratio="0.5509259259259259" data-src="https://inews.gtimg.com/om_bt/OTZySK6J_UY3oJeb7hb8jAu1RAbofxrO5gBD02t4zSLpIAA/641" data-w="1080" src="https://inews.gtimg.com/om_bt/OTZySK6J_UY3oJeb7hb8jAu1RAbofxrO5gBD02t4zSLpIAA/641" style="outline: 0px;visibility: visible !important;width: 677px !important;"/></p>

  • 发表于:
  • 原文链接https://page.om.qq.com/page/O42lwKOW22CT5k2H8Ea3FSJg0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券