用 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;
        }
    }
}

 

转载于:https://www.cnblogs.com/dayang12525/p/10268244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值