我正在开发一个Excel 2007 XLSX工作簿。它有多个工作表,其中大多数使用Excel的分组功能。我希望能够保护工作表,保护一些公式等免受用户干预,但似乎没有办法做到这一点,仍然允许用户随时扩展或折叠Grouped列。
对此,标准答案似乎是使用以下命令插入宏:
ActiveSheet.EnableGrouping
ActiveSheet.Proect UserInterfaceonly = true
但是这个工作簿现在没有宏,也没有任何宏。有没有办法在没有宏的情况下在Excel中执行此操作?
谢谢!
答案 0 :(得分:0)
如果没有任何东西阻止您在外部运行代码,那么只需将此代码放入另一个启用宏的工作簿中,或者从单独的C#winform / console应用程序exe中运行它。 注意我已快速搜索Excel 2007对象模型,但没有找到EnableGrouping方法,但也许我看起来不够努力。
外部VBA
Sub UpdateWorkbook()
Constant workbookpath As String = "C:\somepath\someworkbookname.xlsx"
Dim wkbk As Workbook
Set wkbk = Application.Workbooks.Open(workbookpath)
Dim wksht As Worksheet
Set wksht = wkbk.Worksheets.Item("sheetname")
wksht.EnableGrouping
wksht.Protect UserInterfaceonly = true
Set wksht = Nothing
Set wkbk = Nothing
End Sub
外部C#
public void UpdateWorkbook()
{
const string workbookpath = @"C:\somepath\someworkbookname.xlsx";
Excel.Application xlApp = New Excel.Application();
// To use the next line you need the Excel Extension library otherwise use Type.Missing.
Excel.Workbook wkbk = Application.Workbooks.Open(workbookpath);
Excel.Worksheet wksht wksht = (Excel.Worksheet)wkbk.Worksheets.get_Item("sheetname");
//Check the way this method works..
wksht.EnableGrouping();
// UserInterfaceOnly is the 6th parameter, so 5 before and 11 after.
wksht.Protect(missing,missing,missing,missing,missing,True,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing);
Set wksht = null;
Set wkbk = null;
Set xlApp = null;
}