本文介绍了使用DDE将数据发送到Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道DDE是老式的过时技术。但我正在创建一个C#Windows窗体应用程序,以便将数据发送到Excel文件以供研究之用。我使用的是Win32 DDE功能,以下是我到目前为止所尝试的功能。



我尝试了什么:



I know DDE is old fashion outdated technology. But I am creating a C# Windows Form application to send data to Excel file for research purpose. I use Win32 DDE functions and below is what I have tried so far.

What I have tried:

[DllImport("user32.dll", EntryPoint = "DdeInitialize")]
public static extern int DdeInitialize(out uint pidInst, IntPtr pfnCallback, IntPtr afCmd, IntPtr ulRes);

[DllImport("user32.dll", EntryPoint = "DdeConnect")]
private static extern IntPtr DdeConnect(uint idInst, IntPtr hszService, IntPtr hszTopic, IntPtr pCC);

[DllImport("user32.dll", EntryPoint = "DdeCreateStringHandle")]
private static extern IntPtr DdeCreateStringHandle(uint idInst, string psz, int iCodePage);

public Form1()
{
    InitializeComponent();
    Process application = new Process();
    application.StartInfo.FileName = @"C:\Users\xxx\Desktop\DDE_Client.xlsx";
    application.Start();
    uint instId;
    DdeInitialize(out instId, application.MainWindowHandle, IntPtr.Zero, IntPtr.Zero);

    IntPtr sPtr = DdeCreateStringHandle(instId, "Excel", 1004);
    IntPtr tPtr = DdeCreateStringHandle(instId, "Sheet1", 1004);

    IntPtr channel = DdeConnect(instId, sPtr, tPtr, (IntPtr)null);
}





DdeConnect返回非零值,表示连接成功。



我的问题:



如何将字符串值发送到Excel工作表?例如,我想将名称发送到excel单元格A1。我应该使用哪种DDE功能?



DdeConnect returns non-zero value which indicates the connection is successful.

My question:

How to send a string value to the excel sheet? For example, I want to send "Name" to excel cell A1. Which DDE function should I use for the task?

推荐答案


这篇关于使用DDE将数据发送到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 03:32