如果没有受到什么阻碍你从外部运行的代码,然后只是把这个代码到另一个宏启用工作簿或从一个单独的C#的WinForm /控制台应用程序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;
}
该死 - 这看起来真的很有前途,但我不能有任何外部代码。工作簿通过网络提供给用户,因此必须完全独立。不管怎么说,还是要谢谢你! (它看起来像我所需要的是不可能的,所以我现在要摆脱分组) – Peter 2010-10-21 22:02:54
?如果你不能拥有外部代码或内部宏,那么你可能应该在超级用户上重新提出问题,而不是在编程问题的SO上。虽然罕见,但Excel中有些内容只有在使用代码时才可能(或更细化)。我能想到的唯一的其他选择是VSTO,您是否允许创建一个编译的VSTO DLL来与您的工作簿一起分发?如果没有,那么可以通过Excel UI检查工作帕尔帖的网站,excel嘲笑等来分组提示。有*可能*是一种方式来做到这一点,但不知道。 – 2010-10-21 22:10:02