OpenXml讀取word內容(一)


OpenXml讀取word內容注意事項

1、使用OpenXml讀取word內容,word后綴必須是".docx";如果word后綴是".doc"需要轉成".docx"后,才可以讀取;

2、需要引入相關dll;"WindowsBase.dll"、“DocumentFormat.OpenXml.dll”;

3、word大小>0字節(word大小為0字節會報錯);

word內容

相關代碼

 

    static void Main(string[] args)
        {
            string wordPathStr = @"C:\Users\user\Desktop\新建文件夾 (2)\5.docx";
            using (WordprocessingDocument doc = WordprocessingDocument.Open(wordPathStr, true))
            {
                Body body = doc.MainDocumentPart.Document.Body;
                foreach (var paragraph in body.Elements<Paragraph>())
                {
                    Console.WriteLine(paragraph.InnerText);
                }
            }
            Console.ReadLine();
        }
View Code

控制台顯示


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM