用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件

Posted dayang12525

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace MediaTools.Tool
{
    public class WordHelper
    {       
        public static void TxtToword(string docPath, string txtPath)
        {
            var txtContent = File.ReadAllText(txtPath, Encoding.UTF8);
            WriteToWord(docPath, txtContent);
        }
        public static void WriteToWord(string docPath, string content)
        {
            if (!File.Exists(docPath)) CreateWordprocessingDocument(docPath);
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(docPath);
            object missing = System.Reflection.Missing.Value;
            doc.Content.Text = content;
            app.Visible = false;
            doc.Save();
            doc.Close();
        }

        private static void CreateWordprocessingDocument(string filepath)
        {
            // Create a document by supplying the filepath. 
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
            {
                // Add a main document part. 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
                Body body = mainPart.Document.AppendChild(new Body());
                DocumentFormat.OpenXml.Drawing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Drawing.Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text(""));
            }
        }

        public static string GetContentFromWord(string docPath)
        {
            const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

            StringBuilder textBuilder = new StringBuilder();
            using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docPath, false))
            {  
                NameTable nt = new NameTable();
                XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
                nsManager.AddNamespace("w", wordmlNamespace);
                XmlDocument xdoc = new XmlDocument(nt);
                xdoc.Load(wdDoc.MainDocumentPart.GetStream());

                XmlNodeList paragraphNodes = xdoc.SelectNodes("//w:p", nsManager);

                foreach (XmlNode paragraphNode in paragraphNodes)
                {
                    //XmlNodeList textNodes = paragraphNode.SelectNodes(".//[‘w: t‘&‘w:tab‘]", nsManager);
                    XmlNodeList textNodes = paragraphNode.SelectNodes(".//w:t", nsManager);

                    foreach (System.Xml.XmlNode textNode in textNodes)
                    {
                        textBuilder.Append(textNode.InnerText);
                    }
                    textBuilder.Append(Environment.NewLine);
                }
            }
            var result = textBuilder.ToString();
            return result;
        }
    }
}

 

以上是关于用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件的主要内容,如果未能解决你的问题,请参考以下文章

利用openxml在Excel中插入图表

C#研究OpenXML之路

VS Code中添加程序集安装包即添加DLL引用

dotnet OpenXML 继承组合颜色的 GrpFill 属性

dotnet OpenXML 预设 PresetColorValues 颜色对应的值

dotnet OpenXML 形状的 Outline 的 LineWidth 线条轮廓粗细宽度的行为