从行word文档在线阅读 [英] Read from word document line by line

查看:186
本文介绍了从行word文档在线阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图读取使用C#中的Word文档。我能够得到的所有文字,但我希望能够通过在线阅读商品列表中,并绑定到一个GridView 。目前,我的code仅返回与所有文本(如期望的不是逐行)一个项目的列表。我使用的的Microsoft.Office.Interop.Word 库读取文件。下面是我的code至今:

 应用程序字=新的应用程序();
    文档DOC =新的文件();

    目标文件名=路径;
    //定义一个物体传递到API缺少的参数
    反对失踪= System.Type.Missing;
    DOC = word.Documents.Open(REF文件名,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪);

    字符串读=的String.Empty;
    名单<字符串>数据=新的名单,其中,串>();
    的foreach(范围tmpRange在doc.StoryRanges)
    {
        //读取+ = tmpRange.Text +< BR>中;
        data.Add(tmpRange.Text);
    }
    ((_document)文档).Close();
    ((_Application)字).Quit();

    GridView1.DataSource =数据;
    GridView1.DataBind();
 

解决方案

确定。我找到了解决办法<一href="http://mantas$c$c.com/c-how-to-parse-the-text-content-from-microsoft-word-document/">here.


最后code是如下:

 应用程序字=新的应用程序();
    文档DOC =新的文件();

    目标文件名=路径;
    //定义一个物体传递到API缺少的参数
    反对失踪= System.Type.Missing;
    DOC = word.Documents.Open(REF文件名,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪,失踪参考,
            裁判失踪,失踪参考,参考失踪);

    字符串读=的String.Empty;
    名单&LT;字符串&GT;数据=新的名单,其中,串&GT;();
    的for(int i = 0; I&LT; doc.Paragraphs.Count;我++)
    {
        字符串TEMP = doc.Paragraphs [I + 1] .Range.Text.Trim();
        如果(临时!=的String.Empty)
            data.Add(临时);
    }
    ((_document)文档).Close();
    ((_Application)字).Quit();

    GridView1.DataSource =数据;
    GridView1.DataBind();
 


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 to a gridview. Currently my code returns a list of one item only with all text (not line by line as desired). I'm using the Microsoft.Office.Interop.Word library to read the file. Below is my code till now:

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

解决方案

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

这篇关于从行word文档在线阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆