逐行阅读word文档

本文关键字:文档 word 逐行 | 更新日期: 2023-09-27 18:29:43


我正在尝试用C#阅读一个word文档。我可以获取所有文本,但我希望能够逐行阅读存储在列表中,并绑定到网格视图。目前,我的代码只返回一个项目的列表,其中包含所有文本(而不是根据需要逐行返回)。我正在使用Microsoft.Office.Interop.Word库读取该文件。以下是我到目前为止的代码:

    Application word = new Application();
    Document doc = new Document();
    object fileName = path;
    // Define an object to pass to the API for missing parameters
    object missing = System.Type.Missing;
    doc = word.Documents.Open(ref fileName,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);
    String read = string.Empty;
    List<string> data = new List<string>();
    foreach (Range tmpRange in doc.StoryRanges)
    {
        //read += tmpRange.Text + "<br>";
        data.Add(tmpRange.Text);
    }
    ((_Document)doc).Close();
    ((_Application)word).Quit();
    GridView1.DataSource = data;
    GridView1.DataBind();

逐行阅读word文档

好的。我在这里找到了解决方案。


最终代码如下:

Application word = new Application();
Document doc = new Document();
object fileName = path;
// Define an object to pass to the API for missing parameters
object missing = System.Type.Missing;
doc = word.Documents.Open(ref fileName,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing);
String read = string.Empty;
List<string> data = new List<string>();
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
    string temp = doc.Paragraphs[i + 1].Range.Text.Trim();
    if (temp != string.Empty)
        data.Add(temp);
}
((_Document)doc).Close();
((_Application)word).Quit();
GridView1.DataSource = data;
GridView1.DataBind();

上面的代码是正确的,但太慢了。我已经改进了代码,它比上面的代码快得多。

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());
((_Document)doc).Close();
((_Application)app).Quit();

这个yo怎么样。从医生那里得到所有的单词,并在返回时将它们分开,或者其他对你更好的东西。然后转入列表

   List<string> lines = doc.Content.Text.Split(''n').ToList();