隐藏新工作表标签

时间:2013-05-27 03:45:56

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

如何删除允许用户在Excel Viewer控件中添加新工作表的最后一个工作簿选项卡?

SO16766034 question example

我找到了以下代码来隐藏上图中的按钮:

ExcelViewer1.Application.CommandBars["Workbook tabs"]._  
              Controls["Sheet List"].Visible = false;

但它会引发COM异常:

  

“错误HRESULT E_FAIL已从调用com组件返回”。

相同的命令适用于其他命令栏,如Ply,Cell。

2 个答案:

答案 0 :(得分:0)

试试这个

Sub remove()
  ActiveWindow.DisplayWorkbookTabs = False
End Sub

Sub add()
  ActiveWindow.DisplayWorkbookTabs = True
End Sub

答案 1 :(得分:0)

通过C#...

隐藏excel标签栏(底部)的示例代码
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application appExcel = new 
Microsoft.Office.Interop.Excel.Application();
appExcel.Visible = false;
Microsoft.Office.Interop.Excel.Workbook workbook = appExcel.Workbooks.Add(misValue);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
workbook.SaveAs(Environment.CurrentDirectory + @"\a.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  

appExcel.ActiveWindow.DisplayWorkbookTabs = false;

appExcel.Workbooks.Close();
appExcel.Quit();
Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(workbook);