Read from word document line by line

前端 未结 3 1652
孤独总比滥情好
孤独总比滥情好 2020-11-27 18:25


I\'m trying to read a word document using C#. I am able to get all text but I want to be able to read line by line and store in a list and bind

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 19:00

    Ok. I found the solution here.


    The final code is as follows:

    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 data = new List();
    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();
    

提交回复
热议问题