【问题标题】:How to read text from word file? C#如何从word文件中读取文本? C#
【发布时间】:2017-10-13 22:00:57
【问题描述】:

我正在尝试从 C# 中的文档文件中读取文本

  1. 我无法在 C# 中创建新文档。
    我尝试使用“使用 Microsoft.Office.Interop.Word” 或“使用 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

【问题讨论】:

标签: c#


【解决方案1】:

你好,请看下面的代码

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

【讨论】:

  • tnx.但行“文档文档 = app.Documents.Open(ref readFromPath);”不像我之前提到的那样工作。也许我的参考有问题,但我没有找到匹配参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
相关资源
最近更新 更多