2017-10-13 65 views
0

我试图读取文档文件在C#中的文本如何从word文件中读取文本? C#

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

代码:

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

+2

您没有添加引用? – KeithL

+0

选中此链接:https://stackoverflow.com/questions/3633615/how-can-i-read-docx-file – Bambuk

+0

我添加了参考“Microsoft Office List 16.0”。我需要别的东西吗? – Olive

回答

-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()); 
+0

tnx。但行“Document doc = app.Documents.Open(ref readFromPath);”不像我之前提到的那样工作。也许我有一个参考问题,但我没有找到匹配参考。 – Olive