C++ 使用openxml编辑docx返回无效的memorystream

C++ 使用openxml编辑docx返回无效的memorystream,c++,web-services,openxml,memorystream,C++,Web Services,Openxml,Memorystream,我创建了一个DLL,其中包含一个Word模板,我有使用openXML编辑文档的代码,然后通过内存流将结果发送到一个web服务,在那里文档被下载给用户。问题是,正在发送的内存流要么是没有更新的原始模板文档,要么是在文档明显损坏的情况下发送更新的Word文档XML格式。代码如下: string strTemplate = AppDomain.CurrentDomain.BaseDirectory + "Report Template.docx"; WordprocessingDocument wd

我创建了一个DLL,其中包含一个Word模板,我有使用openXML编辑文档的代码,然后通过内存流将结果发送到一个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;
以下是我在web服务中的代码,它运行良好:

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;

在查看了提供的代码之后,我提供了一个修改过的代码段,它应该适合您使用OpenXML的
WordprocessingDocument
类从文件模板返回修改过的
MemoryStream
的需要。您提供的web服务代码段应按原样工作

// file path of template
string strTemplate = AppDomain.CurrentDomain.BaseDirectory + "Report Template.docx";

// create FileStream to read from template
FileStream fsTemplate = new FileStream(strTemplate, FileMode.Open, FileAccess.Read);

// create MemoryStream to copy template into and modify as needed
MemoryStream msDocument = new MemoryStream();

// copy template FileStream into document MemoryStream
fsTemplate.CopyTo(msDocument);

// close the template FileStream as it is no longer necessary
fsTemplate.Close();

// reset cursor position of document MemoryStream back to top 
// before modifying
msDocument.Position = 0;

// create WordProcessingDocument using the document MemoryStream
using (WordprocessingDocument wdDocument = WordprocessingDocument.Open(msDocument, true)) {

    //Access the main Workbook part, which contains all references.
    MainDocumentPart mainPart = wdDocument.MainDocumentPart;

    /* ... CODE TO UPDATE TEMPLATE ... */

    // save modification to main document part
    wdDocument.MainDocumentPart.Document.Save();

   // close wdDocument as it is no longer needed
   wdDocument.Close();
}

// reset cursor position of document MemoryStream back to top
msDocument.Position = 0;

// return memory stream as promised
return msDocument;