18


我正在尝试使用 C# 阅读 word 文档。我能够获取所有文本,但我希望能够逐行读取并存储在列表中并绑定到 gridview。目前我的代码只返回一个包含所有文本的项目列表(而不是根据需要逐行)。我正在使用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();
4

3 回答 3

25

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


最终代码如下:

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();
于 2013-09-01T04:24:12.393 回答
8

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

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();
于 2016-03-19T13:10:59.733 回答
4

这个怎么样哟。从文档中获取所有单词并在返回或任何对您更好的情况下拆分它们。然后变成列表

   List<string> lines = doc.Content.Text.Split('\n').ToList();
于 2016-10-19T02:51:53.007 回答