Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C对Microsoft.Office.Interop.Word进行排序#_C#_Sorting_Ms Word_Document - Fatal编程技术网

C# 用C对Microsoft.Office.Interop.Word进行排序#

C# 用C对Microsoft.Office.Interop.Word进行排序#,c#,sorting,ms-word,document,C#,Sorting,Ms Word,Document,我使用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 =

我使用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);
}
//打开一个文档文件。
Microsoft.Office.Interop.Word.Application Application=新的Microsoft.Office.Interop.Word.Application();
Document d Document=application.Documents.Open(txtploadedpathtoken.Text);
//循环浏览文档中的所有单词。
int count=document.Words.count;

对于(int i=1;i您的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});
}

如果
document.Words
确实是一个数组,它将从
0..(计数-1)索引
而不是您在此处使用的
1..count
。我试图从0开始,但它给了我一个错误。感谢建议的解决方案,但结果仍然是一样的。没有按照Word文件中的顺序进行操作。@user3248886,我想您的问题是如何将控件添加到面板中,请检查我的更新,因为您提供了
0,0
pos对于每个控件,它可能会给出错误的命令是的,先生。在我删除0,0后,它现在按照顺序显示。非常感谢。