1

I am trying to open an excel table, which is embeded in an word document. As it is stored in binary data i read it from stream and get it. In the excel table and in the word document are some values like $amount, i replace them, but then as i try to save the embeded object the changes are not saved, while the changes in word document are. Where is the mistake? It's driving me crazy.

Here is my code

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());
}

.

private static void ProcessTemplate(Stream template, Dictionary<string, string> replaceDic)
{
      using (var workbook = SpreadsheetDocument.Open(template, true, new OpenSettings() { AutoSave = true }))
      {
          // Replace shared strings
          SharedStringTablePart sharedStringsPart = workbook.WorkbookPart.SharedStringTablePart;
          IEnumerable<x.Text> sharedStringTextElements = sharedStringsPart.SharedStringTable.Descendants<x.Text>();
          DoReplace(sharedStringTextElements, replaceDic);
          // Replace inline strings
          IEnumerable<WorksheetPart> worksheetParts = workbook.WorkbookPart.GetPartsOfType<WorksheetPart>();
          foreach (var worksheet in worksheetParts)
          {
                var allTextElements = worksheet.Worksheet.Descendants<x.Text>();
                DoReplace(allTextElements, replaceDic);
          }

      } // AutoSave enabled
}

private static void DoReplace(IEnumerable<x.Text> textElements, Dictionary<string, string> replaceDic)
{
       foreach (var text in textElements)
       {
            foreach (KeyValuePair<string, string> pair in replaceDic)
            {
                if (text.Text.Contains(pair.Key))
                    text.Text = text.Text.Replace(pair.Key, pair.Value);
            }
       }
}
1
  • can you share the answer please Commented Sep 5, 2016 at 14:31

1 Answer 1

1

Comment out this line:

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

The ProcessTemplate() function already has the Open XML Excel stream contents saved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.