如何从word文件中读取文本? C#

时间:2017-10-13 13:56:38

标签: c#

我正在尝试从C#

中的文档文件中读取文本
  1. 我无法在C#中创建新文档 我尝试使用“使用Microsoft.Office.Interop.Word” 或者“使用System.Windows.Documents”,但它无法识别代码“Document doc = new Document”。
  2. 另外如何从.docx文件中读取文本?
  3. 代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Office.Interop.Word; //didnt recognize "Office"
    
    
    
    namespace DocumentTest1
    {
        /// <summary>
       /// Interaction logic for MainWindow.xaml
      /// </summary>
        public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
         Application word = new Application();
         Document doc = new Document();
      }
    

    }

    可能是什么问题? TNX

1 个答案:

答案 0 :(得分:-2)

您好请参阅以下代码

List<string> data = new List<string>();
Application app = new Application();
Document doc = app.Documents.Open(ref readFromPath);

foreach (Paragraph objParagraph in doc.Paragraphs)
    data.Add(objParagraph.Range.Text.Trim());