문제

I use Microsoft.Office.Interop.Word to get words from Word file and then I populate it into a table layout panel. Unfortunately, the words displayed at the table layout panel are not following exact sequence as in the Word file.

How to fix this?

// 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);
}
도움이 되었습니까?

해결책

Your word document reading code seems OK. but you may need to change how you add items to panel. Since you add new items to same position (0,0) it may give incorrect order.

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top