如何启用“共享"功能Excel工作簿上的选项? [英] How can I enable the "Sharing" options on Excel workbooks?

查看:412
本文介绍了如何启用“共享"功能Excel工作簿上的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 OpenXML ClosedXML ?或任何其他可以帮助您解决问题的库...我相信通常在保存文档时会执行此操作(至少在VBA中是这样),但是我找不到如何在C#中指定保存参数的方法.

Is it possible to enable "Sharing" on excel documents through OpenXML or ClosedXML? Or any other library if it can help... I believe this is usually performed when you save the document (at least that's how it works in VBA), but I can't find how to specify saving arguments in C#.

我想避免使用InterOp,因为我可能会通过网络在多个文件上批量处理此过程.

I'd like to avoid using InterOp since I might batch this process on multiple files through a network.

根据 2009年以来的一些旧页面,OpenXML无法操作受保护的文件存在局限性.但是,这也适用于共享吗?

According to some old pages from 2009, there are limitations where OpenXML cannot operate protected files. However, would that apply to sharing too?

推荐答案

使用OpenXML SDK共享Excel文档的文档不多. 我做了一些测试,发现可以在Excel文档上启用共享 使用OpenXML SDK.要启用共享,必须执行以下步骤:

Sharing Excel documents using OpenXML SDK is not well documented. I did some tests and found that it is possible to enable sharing on Excel documents using OpenXML SDK. The following steps are necessary to enable sharing:

  1. WorkbookUserDataPart添加到Excel文档.添加一个空的Users集合 部分.在此集合中,Excel存储当前具有以下所有权限的所有用户: 此共享工作簿打开.

  1. Add a WorkbookUserDataPart to your Excel document. Add an empty Users collection to the part. In this collection Excel stores all users who currently have this shared workbook open.

WorkbookRevisionHeaderPart添加到Excel文档.添加Headers集合 部分.在此集合中,Excel将存储对历史记录,版本和修订的引用 信息.将第一个元素(Header)添加到包含以下内容的集合中 SheetIdMap(用于跟踪修订记录).在下面的代码示例中 我添加了文档中包含的所有工作表. 此外,将WorkbookRevisionLogPart添加到工作簿的修订标头部分. 在日志部分中,存储了对文档进行的修订的列表.

Add a WorkbookRevisionHeaderPart to your Excel document. Add a Headers collection to the part. In this collection Excel will store references to history, version and revision information. Add a first element (Header) to the collection which contains the SheetIdMap (used for tracking revision records). In the code sample below I've added all worksheets included in the document. Furthermore add a WorkbookRevisionLogPart to the workbook's revision header part. In the log part a list of revision made to the document is stored.

下面的代码示例显示了如何在Excel文档上启用共享. 该代码还检查文档上是否已启用共享.

The code sample below shows how to enable sharing on an Excel document. The code also checks whether sharing is already enabled on a document.

启用共享之前,应创建原始文档的备份.

using (SpreadsheetDocument sd = SpreadsheetDocument.Open("c:\\temp\\enable_sharing.xlsx", true))
{
  WorkbookPart workbookPart = sd.WorkbookPart;

  if (workbookPart.GetPartsCountOfType<WorkbookRevisionHeaderPart>() != 0)
  {
    Console.Out.WriteLine("Excel document already shared!");
    return;
  }

  // Create user data part if it does not exist.
  if (workbookPart.GetPartsCountOfType<WorkbookUserDataPart>() == 0)
  {
    Console.Out.WriteLine("Adding user data part");
    WorkbookUserDataPart workbookUserDataPart = workbookPart.AddNewPart<WorkbookUserDataPart>();

    Users users = new Users() { Count = (UInt32Value)0U };
    users.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

    workbookUserDataPart.Users = users;
  }

  // Create revision header part and revision log part.
  WorkbookRevisionHeaderPart workbookRevisonHeaderPart = workbookPart.AddNewPart<WorkbookRevisionHeaderPart>();

  WorkbookRevisionLogPart workbookRevisionLogPart = workbookRevisonHeaderPart.AddNewPart<WorkbookRevisionLogPart>();

  // Create empty collection of revisions.
  Revisions revisions = new Revisions();
  revisions.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

  workbookRevisionLogPart.Revisions = revisions;

  string lastSetOfRevisionsGuid = Guid.NewGuid().ToString("B");

  // Create headers collection (references to history, revisions)
  Headers headers = new Headers() { Guid = lastSetOfRevisionsGuid };
  headers.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

  int worksheetPartsCount = workbookPart.GetPartsCountOfType<WorksheetPart>();                        

  // Create first element in headers collection
  // which contains the SheetIdMap.
  Header header = new Header() { Guid = lastSetOfRevisionsGuid, DateTime = DateTime.Now, 
                                 MaxSheetId = (UInt32Value)(uint)worksheetPartsCount+1, UserName = "hans", Id = "rId1" };

  // Create the list of sheet IDs that are used for tracking
  // revision records. For every worksheet in the document
  // create one SheetId.
  SheetIdMap sheetIdMap = new SheetIdMap() { Count = (UInt32Value)(uint)worksheetPartsCount };

  for (uint i = 1; i <= worksheetPartsCount; i++)
  {
    SheetId sheetId = new SheetId() { Val = (UInt32Value)i };

    sheetIdMap.Append(sheetId);
  }        

  header.Append(sheetIdMap);
  headers.Append(header);

  workbookRevisonHeaderPart.Headers = headers;
}

这篇关于如何启用“共享"功能Excel工作簿上的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆