C# for visual studio 2010 excel 开发

标签 c# visual-studio-2010 excel-2007

我创建了一个包含隐藏工作表的 excel 2007 工作簿。有一个未隐藏的工作表具有命令按钮,每个命令按钮上都有每个工作表名称。单击命令按钮时,隐藏的工作表应可见,焦点应放在该工作表上。下面的代码返回此错误:

An object reference is required for the non-static field, method, or property 'Microsoft.Office.Tools.Excel.WorksheetBase.Visible get'

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace TestProj
{
    public partial class Sheet17
    {
        private void Sheet17_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet17_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.Startup += new System.EventHandler(this.Sheet17_Startup);
            this.Shutdown += new System.EventHandler(this.Sheet17_Shutdown);

        }

        #endregion

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Sheet5 How = new Sheet5();
            How.Visible = Excel.XlSheetVisibility.xlSheetVisible;
        }

    }
}

有人可以帮我解决这个错误吗?

最佳答案

这样使用;

Sheet5 mySheet = new Sheet5();
mySheet.Visible = Excel.XlSheetVisibility.xlSheetVisible; 

或使您的Sheet5静态。如果不创建此类的任何实例,您将无法访问您的 non-static 类。

MSDN读取.

关于C# for visual studio 2010 excel 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14219270/

相关文章:

vba - 使用 VBA 来自受密码保护的网站的数据

excel - 如何格式化空excel表中的列?

c# - VSTO问题-无法创建Visual Studio Excel工作簿项目

c# - 在没有模型绑定(bind)的情况下发布到 .NET Core Web API Controller

c# - 当结构不匹配时,在 C# 中组合两个对象列表

c# - 单元测试 Blazor RenderFragment 元素

visual-studio-2010 - XNA 4中的管道装配是什么?

c# - VS 2012 发布后,使用异步与 VS 2010 的正确方法?

c# - ServiceStack 将身份验证放入 Razor View

c# - 如何从 native C++ 调用 C# 库(使用 C++\CLI 和 IJW)