C# 如何将其转换为自定义excel函数?
我有下面的C代码,它从数据库中提取数据,并在加载Excel时填充单元格A1/和数据。如何将其转换为自定义函数,从而在用户键入公式(即“=getMyData(“mycustominput”)”)时填充字段,而不是在加载excel时填充字段C# 如何将其转换为自定义excel函数?,c#,excel,C#,Excel,我有下面的C代码,它从数据库中提取数据,并在加载Excel时填充单元格A1/和数据。如何将其转换为自定义函数,从而在用户键入公式(即“=getMyData(“mycustominput”)”)时填充字段,而不是在加载excel时填充字段 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using Excel = Microsof
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using MySql.Data.MySqlClient;
namespace custom_excel
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range firstRow = activeWorksheet.get_Range("A1", missing);
firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, System.Type.Missing);
Excel.Range newFirstRow = activeWorksheet.get_Range("A1", missing);
string connString = "Server=localhost;Port=3306;Database=test;Uid=name;password=password";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT field_value FROM customers LIMIT 1";
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
newFirstRow.Value2 = reader["field_value"].ToString();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
}
}在这里冒险,但是你看过吗?听起来它可能对你正在尝试的工作有用 埃里克·卡特(Eric Carter)的一篇老文章也可能有用,它使用了一个自动化插件。看起来您可以在没有任何第三方库的情况下完成这项工作