在现有的工作簿中隐藏excel功能区。

问题描述 投票:0回答:1

以使其简短。我有一个excel工作簿,我用VBA隐藏功能区。

Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"

enter image description here现在我用C#写了一个应用程序,打开那个工作簿。然后打开的工作簿没有色带,但 "上面 "的空间也被隐藏了。右上角没有x,-等,它只是直接从excel的网格开始。enter image description here我现在想知道,我是否可以在工作簿中删除VBA代码,并用C#代码隐藏功能区?或者当我从C#程序中打开时,我必须在VBA中扩展代码以达到同样的效果。我没有想到会有这样的效果,但我是个初学者。事情是我只找到了使用ribbon xml添加ribbon的例子。我想用一个现有的工作簿来做,这个工作簿只有openend,不需要修改,只需要隐藏它。没有修改,只是隐藏它。C#代码看起来像这样。

namespace opn_excel_1

public partial class Form1 : Form
{
    string fileExcel;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        fileExcel = "C:\\Users\\1.0.xlsm";
        excel.Application xlApp;
        excel.Workbook xlWorkBook;
        xlApp = new excel.Application();
        // open Workbook
        xlWorkBook = xlApp.Workbooks.Open(fileExcel, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlApp.Visible = true;

    }
}

什么是解决这个问题的最佳方案?

问候

c# excel ribbon
1个回答
1
投票

我不明白你的目标是什么。你提供的VBA代码正确地隐藏了Ribbon.如果你想给用户重新打开它的可能性,你可以使用

CommandBars.ExecuteMso "MinimizeRibbon"

以下是您可以在程序上做的事情

Sub ShowRibbon()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
End Sub

Sub HideRibbon()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
End Sub

Sub MinimizeRibbon()
    CommandBars.ExecuteMso "MinimizeRibbon"
End Sub

MinimizeRibbon将最小化色带。如果你在它最小化的时候调用它,它就会恢复正常。

编辑:我在启动时用隐藏色带代码做了一些测试,结果和预期的一样。enter image description here

我用来打开Excel和Workbook的代码是。

var progId = "Excel.Application";
dynamic xlApp = Activator.CreateInstance(Type.GetTypeFromProgID(progId));
xlApp.Visible = true;
xlApp.ShowWindowsInTaskbar = true;
string fileExcel = @"C:\Users\accogli.davide\Documents\Test.xlsm";
object xlWorkBook = xlApp.Workbooks.Open(fileExcel);

我使用Activator,因为我在Visual Studio中没有集成Office的功能。

在工作簿中,我在打开VBA代码时,使用了以下代码

Private Sub Workbook_Open()
    Modulo1.HideRibbon
End Sub

在Modulo1中,我有HideRibbon方法。

Sub HideRibbon()
    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.