我正在尝试使用 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();