ExcelDNA - 创建 table:xlcAddListItem 或其他方式

ExcelDNA - Creating a table: xlcAddListItem or another way

我正在尝试弄清楚如何使用 ExcelDNAExcelReference 范围创建 table。

使用 Excel 你通常会做这样的事情:

range2.Worksheet.ListObjects.Add(Microsoft.Office.Interop.Excel.XlListObjectSourceType.xlSrcRange, range2, System.Type.Missing, Microsoft.Office.Interop.Excel.XlYesNoGuess.xlYes, System.Type.Missing).Name = "uniquesomething";

我一直在尝试使用 xlcAddListItemExcelDNA 做一些类似的事情,但是如果没有任何适当的文档,它是相当粗糙的。有没有人有任何成功?基本上以 ExcelDNA 数组大小调整器为例,然后将网格输出转换为 table.

到目前为止我试过了

ExcelDna.Integration.SourceItem mySourceItemHopefully = (ExcelDna.Integration.SourceItem)XlCall.Excel(XlCall.xlcAddListItem, 1, myExcelReference);
mySourceItemHopefully.Name = "uniquesomething";

但是我收到访问冲突错误。

为此,您必须使用 COM 对象模型,您可以在 Excel-DNA 插件中安全地使用它,只要您:

  1. 使用ExcelDnaUtil.Application获取根Application对象,并且

  2. 在宏或 COM 回调上下文中从主线程调用所有 COM 对象模型。

您可能需要从 ExcelReference.

中获取 COM Range 对象

这可以很简单:

static Range ReferenceToRange(ExcelReference xlref)
{
    string refText = (string)XlCall.Excel(XlCall.xlfReftext, xlref, true);
    dynamic app = ExcelDnaUtil.Application;
    return app.Range[refText];
} 

(这里的 'dynamic' 应用程序也可以输入为 Microsoft.Office.Interop.Excel.Application,如果您有对 PIA 程序集的引用。)

对于更复杂的 Excel 具有多个矩形的引用,您可以从此处的 VB.NET 代码开始:https://github.com/Excel-DNA/Registration/blob/882517eed2137d2b2f9b4b794803258d20e5a174/Source/ExcelDna.Registration.VisualBasic/RangeParameterConversion.vb


C API 不支持添加表。 Microsoft 尚未扩展 xlcXXX 宏以涵盖 Excel 最新版本中的新功能(自 Excel 2000 年起)。 (但是,xlfXXX 函数列表一直保持最新,直到 Excel 2013 年。)

您列出的两个宏函数与您制作的表格类型无关 - 您可以在此处找到的 MacroHelp 帮助文件中看到这一点:http://www.xceed.be/Blog.nsf/dx/excel-macro-function-help-file-for-windows-7

ADD.LIST.ITEM (xlcAddListItem): "Adds an item in a list box or drop-down control on a worksheet or dialog sheet control."

TABLE (xlcTable): "Equivalent to choosing the Table command from the Data menu. Creates a table based on the input values and formulas you define on a worksheet. Use data tables to perform a "what-if”分析工作簿以查看其他单元格中的值如何受到影响。 所以这是关于数据表的,它与您尝试创建的 'ListObject' 表不同。