在不引用 SDK 的情况下将流转换为 WordDoc
因此,我创建了一个用于操作和编辑 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎是处理 opc 包时要走的路。
以下代码行对我来说看起来很好。 Package.Open 有许多构造函数,您可以将它们与路径字符串、流等一起使用...
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...