【发布时间】:2023-04-10 22:11:01
【问题描述】:
我创建了一个使用 Word 模板的 DLL,我有使用 openXML 编辑文档的代码,然后通过内存流将结果发送到 Web 服务,在该 Web 服务中将文档下载给用户。问题是内存流发送的是没有更新的原始模板文档,或者发送的是文档明显损坏的更新的 Word 文档 XML 格式。代码如下:
string strTemplate = AppDomain.CurrentDomain.BaseDirectory + "Report Template.docx";
WordprocessingDocument wdDocument;
//stream the template
byte[] fileBytes = File.ReadAllBytes(strTemplate);
MemoryStream memstreamDocument = new MemoryStream();
memstreamDocument.Write(fileBytes, 0, (int)fileBytes.Length);
wdDocument = WordprocessingDocument.Open(memstreamDocument, true);
//CODE TO UPDATE TEMPLATE
//Save entire document
wdDocument.MainDocumentPart.Document.Save();
保存文档后,如果使用以下代码,内存流返回原始模板而不对文档进行任何更新:
return memstreamDocument;
如果使用以下代码,内存流会返回带有更新的 openXML 数据,但文档已损坏:
MemoryStream memstreamUpdatedDocument = new MemoryStream();
Stream streamDocument = wdDocument.MainDocumentPart.GetStream();
streamDocument.CopyTo(memstreamUpdatedDocument);
return memstreamUpdatedDocument;
这是我在网络服务中运行良好的代码:
HttpResponse response = HttpContext.Current.Response;
MemoryStream stream = GR.GetReport("", intReportID, Culture, ConnectionString, false);
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.AddHeader("content-disposition", "attachment; filename=\"" + "Report_" + intReportID+ ".docx\"");
response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
stream.Position = 0;
stream.CopyTo(response.OutputStream);
response.End();
return response;
【问题讨论】:
标签: c++ web-services openxml memorystream