Word打开文件时发现不可读的内容错误

时间:2020-01-03 07:29:11

标签: c# openxml asp.net-core-2.1 openxml-sdk memorystream

我在打开Word(.docx)文件时遇到错误“ Word在abc.docx中发现了不可读的内容。您要恢复此文档的内容吗?”。

我尝试了Internet上提供的所有解决方案,但没有成功。下面是我将内容写入流中的代码。

private void test()
        {
            using (MemoryStream one = await db.DownloadFile("templates", "one.docx"))
            {
                using (MemoryStream two = await db.DownloadFile("templates", "two.docx"))
                {
                    using (MemoryStream newStream = new MemoryStream())
                    {
                        one.CopyTo(newStream);
                        editingMemoryStream.Position = 0;

                        using (WordprocessingDocument mainDoc = WordprocessingDocument.Open(one, true))
                        {
                            using (WordprocessingDocument newDoc =
                                WordprocessingDocument.Open(newStream, true))
                            {
                                Generate(modal, new, main, report);
                            }
                        }
                    }
                }
            }
        }
private void Generate(List<modal> mo, WordprocessingDocument new, MemoryStream report)
 {
     var main = new.MainDocumentPart;
     modal = mo[0];

     AddTableToBody(report, modal.table, mo);        
  }

public void AddTableToBody(MemoryStream temp, dailyReport,
                    MainDocumentPart main)
   {
            using (WordprocessingDocument newDoc = WordprocessingDocument.Open(editingMemoryStream, true))
            {
                WP.Body body= dailyReport.MainDocumentPart.Document.Body;
                var main = dailyReport.MainDocumentPart;
                //* some code is here*//

                var clone = dailyReport.CloneNode(true);
                main.Document.Body.AppendChild(new WP.Paragraph(new WP.Run(clone)));
                main.Document.Save();
            }
        }

该文件包含2个带有一些标签的表。

0 个答案:

没有答案