C# OpenXml在Word c中保存Excel嵌入对象#

C# OpenXml在Word c中保存Excel嵌入对象#,c#,excel,ms-word,openxml,C#,Excel,Ms Word,Openxml,我试图打开一个excel表格,它嵌入在word文档中。 因为它存储在二进制数据中,所以我从流中读取并获取它。在excel表格和word文档中有一些值,如$amount,我会替换它们,但当我尝试保存嵌入对象时,更改不会保存,而word文档中的更改会保存。错在哪里?这快把我逼疯了 这是我的密码 PaymentData data = PaymentData.FromString(args[1]); Dictionary<string, string> replaceDic = new Di

我试图打开一个excel表格,它嵌入在word文档中。 因为它存储在二进制数据中,所以我从流中读取并获取它。在excel表格和word文档中有一些值,如$amount,我会替换它们,但当我尝试保存嵌入对象时,更改不会保存,而word文档中的更改会保存。错在哪里?这快把我逼疯了

这是我的密码

PaymentData data = PaymentData.FromString(args[1]);
Dictionary<string, string> replaceDic = new Dictionary<string, string>()
{
    { "$value", data.Somedata }
};
string template = Path.GetFullPath("resources/rdoc.docx");
string documentText;

byte[] byteArray = File.ReadAllBytes(template);
using (MemoryStream stream = new MemoryStream())
{
    stream.Write(byteArray, 0, (int)byteArray.Length);
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
    {
        Stream xlStream = wordDoc.MainDocumentPart.EmbeddedPackageParts.First().GetStream();
        ProcessTemplate(xlStream, replaceDic);

        // Reset stream to beginning
        xlStream.Seek(0L, SeekOrigin.Begin);

        wordDoc.MainDocumentPart.EmbeddedPackageParts.First().FeedData(xlStream);

        using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            documentText = reader.ReadToEnd();
        }


        foreach (KeyValuePair<string, string> pair in replaceDic)
        {
            if (documentText.Contains(pair.Key))
                documentText = documentText.Replace(pair.Key, pair.Value);
        }

        using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        {
             writer.Write(documentText);
        }
  }
  // Save the file with the new name
  File.WriteAllBytes("resources/rdoc1.docx", stream.ToArray());
}
PaymentData data=PaymentData.FromString(args[1]);
Dictionary replaceDic=新字典()
{
{“$value”,data.Somedata}
};
字符串模板=Path.GetFullPath(“resources/rdoc.docx”);
字符串文档文本;
byte[]byteArray=File.ReadAllBytes(模板);
使用(MemoryStream stream=new MemoryStream())
{
stream.Write(byteArray,0,(int)byteArray.Length);
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Open(stream,true))
{
Stream xlStream=wordDoc.MainDocumentPart.EmbeddedPackageParts.First().GetStream();
ProcessTemplate(xlStream、replaceDic);
//将流重置为开始
xlStream.Seek(0L,SeekOrigin.Begin);
wordDoc.MainDocumentPart.EmbeddedPackageParts.First().FeedData(xlStream);
使用(StreamReader=newstreamreader(wordDoc.MainDocumentPart.GetStream())
{
documentText=reader.ReadToEnd();
}
foreach(replaceDic中的KeyValuePair对)
{
if(documentText.Contains(pair.Key))
documentText=documentText.Replace(pair.Key,pair.Value);
}
使用(StreamWriter writer=newstreamwriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
编写(文档文本);
}
}
//用新名称保存文件
writealBytes(“resources/rdoc1.docx”,stream.ToArray());
}

private static void ProcessTemplate(流模板、字典替换)
{
使用(var workbook=SpreadsheetDocument.Open(template,true,new OpenSettings(){AutoSave=true}))
{
//替换共享字符串
SharedStringTablePart sharedStringsPart=工作簿.WorkbookPart.SharedStringTablePart;
IEnumerable sharedStringTextElements=sharedStringsPart.SharedStringTable.Subjections();
DoReplace(sharedStringTextElements,replaceDic);
//替换内联字符串
IEnumerable worksheetParts=工作簿.WorkbookPart.GetPartSoftType();
foreach(工作表部件中的var工作表)
{
var allTextElements=worksheet.worksheet.subjects();
DoReplace(所有文本元素,替换DIC);
}
}//已启用自动保存
}
私有静态void DoReplace(IEnumerable textElements,Dictionary replaceDic)
{
foreach(textElements中的var文本)
{
foreach(replaceDic中的KeyValuePair对)
{
if(text.text.Contains(pair.Key))
text.text=text.text.Replace(pair.Key,pair.Value);
}
}
}
注释掉这一行:

//wordDoc.MainDocumentPart.EmbeddedPackageParts.First().FeedData(xlStream);
ProcessTemplate()函数已经保存了Open XML Excel流内容。

注释掉这一行:

//wordDoc.MainDocumentPart.EmbeddedPackageParts.First().FeedData(xlStream);

ProcessTemplate()函数已保存打开的XML Excel流内容。

能否共享答案请共享答案