[英]Sort Microsoft.Office.Interop.Word in C#
我使用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);
}
您的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});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.