繁体   English   中英

C# 从 MemoryStream 打开 Office 文档和 Xps 文件

[英]C# Open Office Documents and Xps Files from MemoryStream

我有一个通过 Xps 查看器查看 Word 和 Excel 文件的应用程序。 我将 office 文件转换为 xps 文件并在 WPF XPS Document Viewer 中显示。

但这里有一个小问题; 我不希望用户看到文件,我在关闭后删除文件。

我想知道是否有任何解决方案可以将 xps 转换为 memory stream 并在 Xps Viewer 中查看

编辑:

我不想在磁盘上创建任何 xps 文件。 转换过程必须在 MemoryStream 内部完成。

在 poc 项目中,以下代码行对我来说效果很好,可以为您提供一个起点。 对于文档转换部分(word/excel -> xps),您可以使用 XPS Document Writer 通过自动化打印它们。

System.IO.Stream docStream = ...any xps as stream;
Package package = Package.Open(docStream);

//Create URI for Xps Package
//Any Uri will actually be fine here. It acts as a place holder for the
//Uri of the package inside of the PackageStore
string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
Uri packageUri = new Uri(inMemoryPackageName);

//Add package to PackageStore
PackageStore.AddPackage(packageUri, package);

XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName);
FixedDocumentSequence fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();

// Do operations on xpsDoc here
DocViewer.Document = fixedDocumentSequence;

//Note: Please note that you must keep the Package object in PackageStore until you
//are completely done with it since certain operations on XpsDocument can trigger
//delayed resource loading from the package.

//PackageStore.RemovePackage(packageUri);
//xpsDoc.Close();

请显示一些代码...

If your XPS document is written into a stream you could pass that stream to Package.Open then the Package to XpsDocument then the Xpsdocument to DocumentViewer ... this way the xps stays in memory all the time.

例如 Aspose.Words/.Cells 可以从 word 和 Excel 生成 XPS 到 stream - 所以不涉及文件...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM