我使用Microsoft.Office.Interop.Word从Word文件中获取单词,然后将其填充到表格布局面板中。不幸的是,在表格布局面板中显示的单词没有按照Word文件中的确切顺序排列。
怎么解决这个问题?
// Open a doc file.
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document d ocument = application.Documents.Open(txtUploadedPathToken.Text);
// Loop through all words in the document.
int count = document.Words.Count;
for (int i = 1; i <= count; i++)
{
// Write the word.
string text = document.Words[i].Text;
//Console.WriteLine("Word {0} = {1}", i, text);
tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true}, 0, 0);
}发布于 2014-02-05 03:33:18
您的word文档读取代码似乎没有问题。但您可能需要更改将项添加到面板中的方式。由于您将新项添加到相同的位置(0,0),它可能会给出不正确的顺序。
foreach (Microsoft.Office.Interop.Word.Range range in document.Words)
{
string text = range.Text;
tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true});
}https://stackoverflow.com/questions/21568053
复制相似问题