2013-05-09 74 views
1

我正在使用以下代碼將所選行從DatagGridview導出到Word文檔。代碼正在工作,但有一個問題。它最先輸出最後一行,我無法找到一種方法使其按順序導出。例如,如果我選擇行[0,1,2,3],它將導出行[3]而不是[2]和[1]。任何想法是什麼問題?C#將Datagridview導出爲Word文檔

public void WordDoc(string getfilename) 
     { 



      object FileName = getfilename; 


      //Create word Application Object 
      Word.Application word = new Word.Application(); 

      //Create word document Object 
      Word.Document doc = null; 

      //Create word Missing Object 
      object missing = System.Type.Missing; 

      object readOnly = false; 
      object isVisible = false; 
      // make visible Word application 
      word.Visible = true; 

      try 
      { 
       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); 
       doc.Activate();  
foreach (DataGridViewRow rows in dataGridView1.SelectedRows) 
         { 
         string item1 = rows.Cells[0].Value.ToString(); 
           string item2 = rows.Cells[2].Value.ToString(); 
           string item3 = rows.Cells[3].Value.ToString(); 
           string item4 = rows.Cells[4].Value.ToString(); 
           string item5 = rows.Cells[5].Value.ToString(); 
           string item6 = rows.Cells[6].Value.ToString(); 
           string item7 = rows.Cells[7].Value.ToString(); 
           string item8 = rows.Cells[8].Value.ToString(); 
           string item9 = rows.Cells[9].Value.ToString(); 
           string item10 = rows.Cells[10].Value.ToString(); 
           string item11 = rows.Cells[11].Value.ToString(); 
           string item12 = rows.Cells[12].Value.ToString(); 

           this.FindAndReplace(word, "!0!", item1); 
           this.FindAndReplace(word, "!1!", item2); 
           this.FindAndReplace(word, "!2!", item3); 
           this.FindAndReplace(word, "!3!", item4); 
           this.FindAndReplace(word, "!4!", item5); 
           this.FindAndReplace(word, "!5!", item6); 
           this.FindAndReplace(word, "!6!", item7); 
           this.FindAndReplace(word, "!7!", item8); 
           this.FindAndReplace(word, "!8!", item9); 
           this.FindAndReplace(word, "!9!", item10); 
           this.FindAndReplace(word, "!10!", item11); 
           this.FindAndReplace(word, "!11!", item12); 
         } 
} 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error : " + ex.Message); 
      } 
     } 



      private void FindAndReplace(Word.Application word, object findText, object replaceText) 
      { 
       word.Selection.Find.ClearFormatting(); 
       object matchCase = true; 
       object matchWholeWord = true; 
       object matchWildCards = false; 
       object matchSoundsLike = false; 
       object matchAllWordForms = false; 
       object forward = true; 
       object format = true; 
       object matchKashida = false; 
       object matchDiacritics = false; 
       object matchAlefHamza = false; 
       object matchControl = false; 
       object read_only = false; 
       object visible = true; 
       object replace = 1; 
       object wrap = 2; 

       word.Selection.Find.Execute(ref findText, ref matchCase, 
       ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, 
       ref matchAllWordForms, ref forward, ref wrap, ref format, 
       ref replaceText, ref replace, ref matchKashida, 
       ref matchDiacritics, 
       ref matchAlefHamza, ref matchControl); 
     }  
+0

您的部分代碼缺失問題請提供更多代碼以便找到解決方案.. – Obama 2013-05-09 08:42:28

+0

@Obama這是該函數的完整代碼。你爲什麼說缺少代碼? – user2345661 2013-05-09 08:44:31

+0

嘗試評論:word.Selection.Find.ClearFormatting(); – Obama 2013-05-09 08:46:23

回答

0

我想這應該Linq的工作(你會在「的.cs」文件的頂部需要using System.Linq;):

var orderedRows = from DataGridViewRow row in dataGridView1.SelectedRows 
        orderby row.Index 
        select row; 

foreach (DataGridViewRow row in orderedRows) 
    ... 

[編輯],如OP要求提供更多資訊:

這是使用Linq按照每行索引的順序排序返回的行。

See this documentation for an introduction to Linq

但是簡單地說,它的工作原理是這樣的:

首先,要認識到DataGridView.SelectedRows中,他們是由用戶選擇的順序返回行。

爲了讓它們按正確的順序排列,我們需要按照每行的.Index屬性對它們進行排序。

LINQ的上面說做這個的:

「拿中的所有行dataGridView1.SelectedRows,責令各行的索引屬性,然後在順序返回所有行」。

但是,這太複雜了,不能完全進入這裏;你必須閱讀我發佈的Linq介紹!

+0

@Mathew Watson你是我的捍衛者冠軍。 – user2345661 2013-05-09 08:54:46

+0

@Mathew Watson只要再幫我一下,請解釋代碼實際上是什麼? – user2345661 2013-05-09 08:55:30

+0

我不知道爲什麼有人低估了這個答案 - 這是正確的不是嗎? – 2013-05-09 08:55:33