如何在没有VBA代码的情况下单击工作表上的按钮来在后面运行MFC或C ++代码? [英] How to run MFC or C++ code behind with a click of a button on a Worksheet, without VBA code?

查看:111
本文介绍了如何在没有VBA代码的情况下单击工作表上的按钮来在后面运行MFC或C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在工作表上单击一个按钮来运行MFC或C ++代码,而没有VBA代码.

如果可以的话,可以给我小费吗? :rolleyes::rolleyes:

I want to run MFC or C++ code behind with a click of a button on a Worksheet, without VBA code.

Can you give me a tip if it is possible ? :rolleyes: :rolleyes:

推荐答案

这是一个C#代码,单击工作表上的一个按钮即可,没有VBA代码.

我想像这样运行MFC或C ++代码.

请给我一个提示:) :)

This is a C# code behind with a click of a button on a Worksheet, without VBA code.

I want to run MFC or C++ code like this.

Plz, give me a tip to me :) :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using MSForms = Microsoft.Vbe.Interop.Forms;
namespace ExcelTest
{
    public partial class Form1 : Form
    {
        public Excel.Application theApp = null;
        public Excel._Workbook theWorkbook;
        public Excel._Worksheet theWorkSheet;
        public Excel.Range theRange;
        Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
        private MSForms.CommandButton btnExcel;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            theApp = new Excel.Application();
            theApp.Visible = true;
            EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
            theApp.WorkbookBeforeClose += EventDel_BeforeBookClose;
            theWorkbook = theApp.Workbooks.Open(@"C:\test.xlsx", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
            theWorkSheet = (Excel.Worksheet)theWorkbook.Worksheets.get_Item(1);
            btnExcel = (MSForms.CommandButton)FindControl("btnExcel");
            btnExcel.Click += new MSForms.CommandButtonEvents_ClickEventHandler(btnExcel_Click);
        }
        private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
        {
            MessageBox.Show("Excel Close");
        }
        object FindControl(string name)
        {
            return FindControl(name, (Excel.Worksheet)theWorkbook.ActiveSheet);
        }
        object FindControl(string name, Excel.Worksheet sheet)
        {
            Excel.OLEObject theObject;
            try
            {
                theObject = (Excel.OLEObject)sheet.OLEObjects(name);
                return theObject.Object;
            }
            catch
            {
                // Returns null if the control is not found.
            }
            return null;
        }
        private void btnExcel_Click()
        {
            MessageBox.Show("Button Clicked");
        }

    }
}


这篇关于如何在没有VBA代码的情况下单击工作表上的按钮来在后面运行MFC或C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆