打开XML SDK来编辑Active文档

发布于 2024-08-25 19:35:06 字数 169 浏览 6 评论 0原文

是否可以使用 Open XML sdk 来操作当前在 Office 应用程序中打开的文档部分(word/ppt)。我知道最简单的方法是使用 VSTO,但它很慢并且需要使用剪贴板来插入元素,OXML sdk 更直接、更简单。

如果有人可以发布一些代码示例,那就太好了。

提前致谢
拉凯什

Is it possible to use the Open XML sdk to manipulate parts of document which is currently open in the Office app (word/ppt). I know the easiest thing is to use VSTO, but its slow and would involve clipboard use to insert elements, the OXML sdk is direct and simpler.

If somebody could post some code sample that would be great.

Thanks in advance
Rakesh

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

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

发布评论

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

评论(4

回忆躺在深渊里 2024-09-01 19:35:07

是的,可以使用 OpenXML sdk 2 修改打开的 VSTO 文档,然后使用更改后的 xml 更新打开的文档。

http://msdn.microsoft.com/en-us/library/ff191178.aspx

http://code.msdn.microsoft.com/Improve-Automation-415bff13基本上

,您从一个范围中获取 xml,将其视为流,将其打包,使用包上的 sdk,然后通过相反的过程将修改后的 xml 插入回来。

众所周知,这种 sdk 的常识用法是不可能的。然而,这是错误的。

Yes it is possible to modify an open VSTO document, using the OpenXML sdk 2, then update your open document using the changed xml.

http://msdn.microsoft.com/en-us/library/ff191178.aspx

http://code.msdn.microsoft.com/Improve-Automation-415bff13

Basically you get the xml from a range, treat it as a stream, package it up, use the sdk on the package, then insert the modified xml back by reversing the process.

The wisdom out there is that this common sense usage of the sdk is not possible. However, this is just wrong.

烟凡古楼 2024-09-01 19:35:07

如下所示:-

//include the namespace

using DocumentFormat.OpenXml.WordProcessing

//Open and manipulate temp.docx 

using (WordprocessingDocument myDoc = WordprocessingDocument.Open("temp.docx", true)) 
{
    //Access main part of document 
    MainDocumentPart mainPart = myDoc.MainDocumentPart; 

    //Add new comments part to document 
    mainPart.AddNewPart<WordprocessingCommentsPart>(); 

    //Delete Styles part within document 
    mainPart.DeletePart(mainPart.StyleDefinitionsPart); 

    //Iterate through all custom xml parts within document 
    foreach (CustomXmlPart customXmlPart in mainPart.CustomXmlParts) {
        //DO SOMETHING 
    }
}

另外,您可以使用 LINQ 来避免 foreach 循环。

Something like below:-

//include the namespace

using DocumentFormat.OpenXml.WordProcessing

//Open and manipulate temp.docx 

using (WordprocessingDocument myDoc = WordprocessingDocument.Open("temp.docx", true)) 
{
    //Access main part of document 
    MainDocumentPart mainPart = myDoc.MainDocumentPart; 

    //Add new comments part to document 
    mainPart.AddNewPart<WordprocessingCommentsPart>(); 

    //Delete Styles part within document 
    mainPart.DeletePart(mainPart.StyleDefinitionsPart); 

    //Iterate through all custom xml parts within document 
    foreach (CustomXmlPart customXmlPart in mainPart.CustomXmlParts) {
        //DO SOMETHING 
    }
}

Also, you could use LINQ to avoid foreach loops.

花开雨落又逢春i 2024-09-01 19:35:07

我能够使用 ClosedXML 使用 Excel 文档来执行此操作(将文件作为路径 excelFileName 保存到磁盘后):

byte[] byteArray = null;
using (var fs = new FileStream(excelFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
    int numBytesToRead = Convert.ToInt32(fs.Length);
    byteArray = new byte[numBytesToRead];
    fs.Read(byteArray, 0, numBytesToRead);
}
using (MemoryStream mem = new MemoryStream()) {
    mem.Write(byteArray, 0, byteArray.Length);
    XLWorkbook wb = new XLWorkbook(mem);
    ...
}

在我的情况下,我只读取文档,不会保存它,但您可以写入修改后的流如有必要,到另一个文件。

I was able to do this using an Excel Document using ClosedXML as such (after saving the file to disk as the path excelFileName):

byte[] byteArray = null;
using (var fs = new FileStream(excelFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
    int numBytesToRead = Convert.ToInt32(fs.Length);
    byteArray = new byte[numBytesToRead];
    fs.Read(byteArray, 0, numBytesToRead);
}
using (MemoryStream mem = new MemoryStream()) {
    mem.Write(byteArray, 0, byteArray.Length);
    XLWorkbook wb = new XLWorkbook(mem);
    ...
}

In my case I am only reading the document and will not be saving it, but you could write the modified stream to another file if necessary.

无所谓啦 2024-09-01 19:35:07

显然,如果没有 Sharepoint,你就无法做到这一点。

根据 Zeyad Jarabi/..

...您需要一个了解如何获取共享锁的平台(例如 SharePoint 或 SkyDrive)。如果没有这个概念,那么应用程序和 SDK 只能采用读锁或写锁,这会阻止这两种技术访问同一个文件。

Apparently you cannot do this without Sharepoint.

As per Zeyad Jarabi/..

...you need a platform that understands how to take a shared lock (like SharePoint or SkyDrive). Without this concept then the application and the SDK can only take read or write locks, which prevents these two pieces of technology from accessing the same file.

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