这里,我想使用xceed docx作为docx文件导出一个报告,但是它返回空白文档(空)。
MemoryStream stream = new MemoryStream();
Xceed.Words.NET.DocX document = Xceed.Words.NET.DocX.Create(stream);
Xceed.Words.NET.Paragraph p = document.InsertParagraph();
p.Append("Hello World");
document.Save();
return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "DOCHK.docx");请帮帮忙
发布于 2018-04-26 01:30:24
问题是:
虽然您的数据已经写入MemoryStream,但内部的“流指针”或游标(在传统术语中,将其视为磁带头)位于您编写的数据的末尾:
在document.Save()之前
stream = [_________________________...]
ptr = ^调用document.Save()后
stream = [<xml><p>my word document</p><p>the end</p></xml>_________________________...]
ptr = ^当您调用Controller.File( Stream, String )时,它将继续从当前的ptr位置读取数据,因此只读取空白数据:
stream = [<xml><p>my word document</p><p>the end</p></xml>from_this_point__________...]
ptr = ^ (实际上,它根本不会读取任何内容,因为MemoryStream特别不允许超出其内部长度限制(默认情况下是迄今为止写入它的数据量)。
如果将ptr重置为流的开始,那么当读取流时,返回的数据将从写入数据的开头开始:
stream = [<xml><p>my word document</p><p>the end</p></xml>_________________________...]
ptr = ^解决方案:
在从流读取数据之前,需要将MemoryStream重置为0:
using Xceed.Words.NET;
// ...
MemoryStream stream = new MemoryStream();
DocX document = DocX.Create( stream );
Paragraph p = document.InsertParagraph();
p.Append("Hello World");
document.Save();
stream.Seek( 0, SeekOrigin.Begin ); // or `Position = 0`.
return File( stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "DOCHK.docx" );https://stackoverflow.com/questions/50033523
复制相似问题