如何从 Excel 工作簿中删除宏?

发布于 2024-11-27 16:02:56 字数 253 浏览 1 评论 0原文

我目前被迫使用 Office Live / Skydrive(无论正确的名称是什么)来编辑 Excel 工作簿。 Skydrive 不允许我上传包含宏的工作簿,而且我不需要宏,因此我想用 C# 编写一个小清理应用程序,只需删除所有宏。如何使用 Excel 2007 及更高版本 (.xlsm) 文档执行此操作?

编辑:基本上,我想打开一个xlsm文档并将其另存为xlsx文档,而不需要安装Excel,即通过OpenXML SDK或其他东西。

I am currently forced to use Office Live / Skydrive (whatever the proper name is) to edit Excel workbooks. Skydrive won't let me upload a workbook that contains macros, and I don't need the macros, so I would like to write a little cleansing app in C# that simply removes all macros. How can I go about this using Excel 2007 and above (.xlsm) documents?

EDIT: Basically, I would like to open an xlsm document and save it as an xlsx document, without required Excel to be installed, i.e. via the OpenXML SDK or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-12-04 16:02:57

使用 openXML SDK 2.0,这对我有用:

SpreadsheetDocument document = SpreadsheetDocument.Open("excelFile.xlsm", true);
document.WorkbookPart.DeletePart(document.WorkbookPart.VbaProjectPart);
document.Dispose();

With openXML SDK 2.0, this worked for me:

SpreadsheetDocument document = SpreadsheetDocument.Open("excelFile.xlsm", true);
document.WorkbookPart.DeletePart(document.WorkbookPart.VbaProjectPart);
document.Dispose();
灼疼热情 2024-12-04 16:02:57

我没有时间检查 OpenXML SDK 文档中的数千页,但您可以尝试如下操作:

.xlsm 文件是一个 ZIP 存档,主要包含 XML 文件。您需要从 xl\_rels 文件夹中的 workbook.xml.rels 文件中删除对宏的引用。这是一个示例:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Target="worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId3"/>
  <Relationship Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId2"/>
  <Relationship Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId1"/>
  <Relationship Target="vbaProject.bin" Type="http://schemas.microsoft.com/office/2006/relationships/vbaProject" Id="rId6"/>
  <Relationship Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Id="rId5"/>
  <Relationship Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Id="rId4"/>
</Relationships>

它是您需要删除的 http://schemas.microsoft.com/office/2006/relationships/vbaProject 类型的关系。关系中引用的文件也应从 ZIP 存档中删除。在本例中,该文件名为 vbaProject.bin 并放置在 xl 文件夹中。

使用 OpenXML SDK,您应该能够导航 Excel 文件的逻辑结构并执行此类清理,而无需了解 ZIP 存档的确切结构。但是,像检查 ZIP 存档一样检查 Excel 文件仍然有助于理解文件的结构。

I didn't have time to check the thousands of pages in the OpenXML SDK documentation but you could try something like this:

The .xlsm file is a ZIP archive containing mostly XML files. You need to remove the references to macros from the workbook.xml.rels file in the xl\_rels folder. Here is a sample:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Target="worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId3"/>
  <Relationship Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId2"/>
  <Relationship Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId1"/>
  <Relationship Target="vbaProject.bin" Type="http://schemas.microsoft.com/office/2006/relationships/vbaProject" Id="rId6"/>
  <Relationship Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Id="rId5"/>
  <Relationship Target="theme/theme1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Id="rId4"/>
</Relationships>

It is the relationship of type http://schemas.microsoft.com/office/2006/relationships/vbaProject you need to remove. The file referenced in the relationship should also be removed from the ZIP archive. In this case the file is named vbaProject.bin and is placed in the xl folder.

Using the OpenXML SDK you should be able to navigate the logical structure of the Excel file and perform this kind of cleanup without understanding the exact structure of the ZIP archive. However, inspecting the Excel file as if it was a ZIP archive can still be helpful to understand the structure of the file.

末が日狂欢 2024-12-04 16:02:57

您可以尝试的一件事是更改您收到的文件的文档类型,如前面的 答案。可以找到传递到 ChangeDocumentType 方法的可用选项 此处

我不完全确定这会删除所有宏内容,但如果您无法从用户那里获取 .xlsx 文件,那么它值得一试。

One thing you could try is to change the document type of the file you are receiving as explained in a previous answer. The available options to pass into the ChangeDocumentType method are found here.

I am not totally sure this will strip all the macro content, but its worth a shot if you aren't able to get a .xlsx file from your user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文