如何从 Excel 工作簿中删除宏?
我目前被迫使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 openXML SDK 2.0,这对我有用:
With openXML SDK 2.0, this worked for me:
我没有时间检查 OpenXML SDK 文档中的数千页,但您可以尝试如下操作:
.xlsm
文件是一个 ZIP 存档,主要包含 XML 文件。您需要从xl\_rels
文件夹中的workbook.xml.rels
文件中删除对宏的引用。这是一个示例:它是您需要删除的
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 theworkbook.xml.rels
file in thexl\_rels
folder. Here is a sample: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 namedvbaProject.bin
and is placed in thexl
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.
您可以尝试的一件事是更改您收到的文件的文档类型,如前面的 答案。可以找到传递到
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.