1

我正在尝试打开一个嵌入在 word 文档中的 excel 表。因为它存储在二进制数据中,所以我从流中读取它并获取它。在 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());
}

.

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

1 回答 1

1

注释掉这一行:

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

ProcessTemplate() 函数已经保存了 Open XML Excel 流内容。

于 2013-05-13T06:50:54.623 回答