在不引用 SDK 的情况下将流转换为 WordDoc

发布于 2024-11-18 00:21:38 字数 1347 浏览 3 评论 0原文

因此,我创建了一个用于操作和编辑 Word 文档的 C# 库。这当然引用了 OpenXML SDK。但是,在我使用该库的地方,我不想也必须引用 .dll。

我可能从另一个项目使用的一种方法有一个 WordprocessingDocument 参数并具有此配置文件:

public bool FillTemplate(ref WordprocessingDocument document, XElement data)
{
    //EDIT the document and return True if succesful.
}

这里的问题当然是我必须在另一个项目中创建一个 WordprocessingDocument只是经过一条小溪。

好吧,我想最简单的解决方案是对方法进行不同的配置文件:

public bool FillTemplate(Stream document, XElement data)
{
    WordprocessingDocument doc = WordprocessingDocument.Open(document, true);
    return FillTemplate(doc, data);
}

但是我得到了我认为是一个绝妙主意的想法,只需为 Stream 创建一个扩展方法:

public static WordprocessingDocument ConvertToWordDocument(this Stream stream, bool isEditable)
{
    return WordprocessingDocument.Open(stream, isEditable);
}

并像这样使用它:

FileStream fStream = new FileStream(@"C:\Users\Me\Desktop\SomeDoc.docx", FileMode.Open);
var doc = fStream.ConvertToWordDocument(true);
filler.FillTemplate(ref doc, getXmlDataFor(42));

fStream.Flush();
fStream.Close();

但是,由于某种原因,这不起作用(文档发生了变化,但它似乎没有返回到流中),我对如何使用 Streams 和 WordprocessingDocument 包/包装器的整个想法有点怀疑。

什么是最佳解决方案,这样我就不会遇到很多麻烦? WordprocessingDocument 类实际上如何将其作为参数等传递?为什么流没有改变原来打开的文档?

So I've created a C# library that manipulates and edits Word Documents. This of course references the OpenXML SDK. Where I use the library however I do not wan't to have to reference the .dll as well.

One method that I might use from another project has a WordprocessingDocument parameter and has this profile:

public bool FillTemplate(ref WordprocessingDocument document, XElement data)
{
    //EDIT the document and return True if succesful.
}

problem here is of course that I would have to create a WordprocessingDocument inside the other project instead of just passing a stream.

Ok I guess the simplest solution would be a different profile on the method:

public bool FillTemplate(Stream document, XElement data)
{
    WordprocessingDocument doc = WordprocessingDocument.Open(document, true);
    return FillTemplate(doc, data);
}

But I got what I thought would be a brilliant idea just to create a extension method for Stream:

public static WordprocessingDocument ConvertToWordDocument(this Stream stream, bool isEditable)
{
    return WordprocessingDocument.Open(stream, isEditable);
}

and use it like this:

FileStream fStream = new FileStream(@"C:\Users\Me\Desktop\SomeDoc.docx", FileMode.Open);
var doc = fStream.ConvertToWordDocument(true);
filler.FillTemplate(ref doc, getXmlDataFor(42));

fStream.Flush();
fStream.Close();

However this doesn't work for some reason (Doc changes but it doesn't seem to get returned to the stream) and I got a little skeptical about the whole idea of how I'm using Streams and the WordprocessingDocument package/wrapper thingy.

What would be a optimal solution so I'm not going into a whole lot of trouble? How actually does the WordprocessingDocument class work in relation to passing it around as a parameter and such? Why didn't the stream change the originally opened document?

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

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

发布评论

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

评论(1

心凉 2024-11-25 00:21:38
System.IO.Packaging.Package

似乎是处理 opc 包时要走的路。
以下代码行对我来说看起来很好。 Package.Open 有许多构造函数,您可以将它们与路径字符串、流等一起使用...

System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(@"C:\Users\Me\Desktop\SomeDoc.docx");
DocumentFormat.OpenXml.Packaging.WordprocessingDocument document = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(package, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

// edit document                
package.Flush();
package.Close();
System.IO.Packaging.Package

seems to be the way to go when handling opc packages.
Following lines of code looks fine to me. Package.Open have a number of constructors that you can use with path strings, streams etc...

System.IO.Packaging.Package package = System.IO.Packaging.Package.Open(@"C:\Users\Me\Desktop\SomeDoc.docx");
DocumentFormat.OpenXml.Packaging.WordprocessingDocument document = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(package, DocumentFormat.OpenXml.WordprocessingDocumentType.Document);

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