Excel DDE Commands

! https://zhuanlan.zhihu.com/p/635569763

Excel DDE Commands

连接参数

Application: Excel
Topic:
System: 整个应用。
[sheetname] 指定 worksheet。

我使用的是 .NET 组件 Specshell.NDde

		private DdeClient _ddeClient;
        public void Init()
        {
            try
            {
                _ddeClient = new DdeClient("EXCEL", "System");
                _ddeClient.Context.Encoding = System.Text.Encoding.Default;
                _ddeClient.Connect();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

	    public void Execute(string cmd, bool needEsc = false)
        {
            if (needEsc)
            {
                _ddeClient.TryExecute("[ESC]", 10);
            }
            _ddeClient.TryExecute(cmd, 5);
        }

	    public void Dispose()
        {
            try
            {
                _ddeClient?.Disconnect();
            }
            catch { }
        }

DDE commands

  • 命令大小写不敏感(case insensitivity)
  • 命令可以带括号,也可以不带括号
    [paste] = [paste()]
Command 说明 Description
Application commands
[app.activate] 激活应用 activate the application
[quit] 关闭应用 quit the application
Workbook commands
[activate("WorkbookName")] 激活指定的文档 activate a workbook
[close(false)] 关闭文档 close a workbook
[file.close(false)] 关闭文档 close a workbook
[new(1)] 新建一个 workbook new a workbook
[open("filepath.xlsx")] 打开一个 workbook open a workbook
[save.as("test1.xlsx")] 保存文档 save as a workbook
Worksheet commands
[workbook.delete("Sheet1")] 删除一个 worksheet delete a worksheet
[workbook.insert(1)] 新建一个 worksheet new a worksheet
[workbook.move("", "", 1)] 移动一个 worksheet move a worksheet
[workbook.name("Sheet1", "SheetA")] 命名一个 worksheet rename a worksheet
[workbook.new] 打开插入对话框 open the insert dialog
[workbook.next] 移动到下一个 worksheet move to the next worksheet
[workbook.prev] 移动到上一个 worksheet move to the previous worksheet
[workbook.select("worksheetName")] 激活一个 worksheet activate a worksheet
data commands
[clear] 清除当前选择的值 clear selection values
[column.width(,"c1:c2",false,1)] 隐藏/显示列 hide/un-hide columns
[column.width(20,"c1:c2")] 设置列宽 set width of columns
[copy] 复制。 copy selection values
[edit.delete][1] 删除当前选择的单元 delete selection values
[esc] 发送一个 Escape 键? send the escape key?
[format.font("Verdana",12)] 设置字体 set font
[formula("=formula")] 设置值 set a value
[insert(shiftDirectionAndMode)] 插入 insert
[paste("R1C1")] 粘贴到指定的单元 paste to the cells
[paste][2] 粘贴 paste
[patterns(1,0,3)] 设置单元格式 set format of cells
[row.height(,"r1:r2",false,1)] 隐藏/显示行 hide/un-hide rows
[row.height(20,"r1:r2")] 设置列宽 set height of rows
[select("R[1]:R[1]")] 相对位置,选择下一行 select a relative row
[select("R[1]")] 相对位置,选择下一行 select a relative row
[select("R1:R2")] 选择行 select rows
[select("R1C1")] 选择单元 select cells
[undo] 撤销操作 undo
  1. [edit.delete]: 下方的单元会上移。 ↩︎

  2. [paste]: 支持 Range 的粘贴。 ↩︎