将文本添加到现有Word文档(.docx)会导致文件损坏 [英] Adding Text To an Existing Word Document (.docx) results in file corruption

查看:175
本文介绍了将文本添加到现有Word文档(.docx)会导致文件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文本添加到从一个目录复制到另一个目录的文档中。继承我的代码:




            string dir = Directory.GetCurrentDirectory();

            string filename = dir +" \\templates\\TWord.docx" ;;

            string fn = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+" Doc1.docx"; $
            File.Copy(filename,fn,true);

            String txt =" Testing"; $
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fn,true);

            DocumentFormat.OpenXml.Wordprocessing.Document document = wordprocessingDocument.MainDocumentPart.Document;

            DocumentFormat.OpenXml.Wordprocessing.Body body = document.MainDocumentPart.Document.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Body());

            DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());



            Run run = para.AppendChild(new Run());

            run.AppendChild(new Text(txt));
$


            wordprocessingDocument.Close();





该文件复制正常,并开始该过程,但是当我尝试在run.AppendChild()执行后打开文件时,word会给出错误:我们很抱歉我们不能打开Doc1因为我们发现它的内容有问题"


详细信息: 


未指定错误


位置:部分:/word/document.xml,Line: 1,列:0

解决方案

您的问题是这行代码:

 DocumentFormat.OpenXml.Wordprocessing.Body body = 
document.MainDocumentPart.Document.AppendChild(
new DocumentFormat.OpenXml.Wordprocessing.Body());

如果您从现有文档开始,该文档将包含< w:body>元件。 Word文档只能有
一个 < w:body>。您需要使用以下内容来获取正文:



< pre class ="prettyprint"> DocumentFormat.OpenXml.Wordprocessing.Document document =
wordprocessingDocument.MainDocumentPart.Document;
Body body = document.Body;






Hi, Im trying to add text to a document copied from one directory to another. Heres my code:

            string dir = Directory.GetCurrentDirectory();
            string filename = dir + "\\templates\\TWord.docx";
            string fn = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+ "Doc1.docx";
            File.Copy(filename, fn, true);
            String txt = "Testing";
            WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(fn, true);
            DocumentFormat.OpenXml.Wordprocessing.Document document = wordprocessingDocument.MainDocumentPart.Document;
            DocumentFormat.OpenXml.Wordprocessing.Body body = document.MainDocumentPart.Document.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Body());
            DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());

            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text(txt));

            wordprocessingDocument.Close();

The file copies fine, and begins the process however when I try to open the file after the run.AppendChild() is executed word gives the error: We're Sorry We Cant Open Doc1 Because we found a problem with its contents"

Details: 

Unspecified error

Location: Part:/word/document.xml,Line:1,column:0

解决方案

Your problem is this line of code:

DocumentFormat.OpenXml.Wordprocessing.Body body = 
   document.MainDocumentPart.Document.AppendChild(
   new DocumentFormat.OpenXml.Wordprocessing.Body());

If you start with an existing document, that document will already contain a <w:body> element. A Word document can have only one <w:body>. You need to use something like the following to get the Body:

DocumentFormat.OpenXml.Wordprocessing.Document document = 
  wordprocessingDocument.MainDocumentPart.Document;
Body body = document.Body;



这篇关于将文本添加到现有Word文档(.docx)会导致文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆