繁体   English   中英

C#将Datagridview导出到Word文档

[英]C# Export Datagridview to Word Document

我正在使用以下代码将选定的行从DatagGridview导出到Word文档。 代码正在工作,但是有一个问题。 它首先导出最后一行,我想不出一种方法使其按顺序导出。 例如,如果我选择Row [0,1,2,3],它将导出[3]而不是[2]和[1]。 任何想法是什么问题?

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);
        }       

我认为此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按每行索引的顺序对返回的行进行排序。

有关Linq的介绍,请参阅此文档

但简单来说,它是如何工作的是这样的:

首先,认识到DataGridView.SelectedRows以用户选择的顺序返回行。

为了以正确的顺序获取它们,我们需要按每行的.Index属性对其进行排序。

上面的Linq通过说:

“获取dataGridView1.SelectedRows中的所有行,并通过每行的Index属性对其进行排序,然后以该顺序返回所有行”。

但这太复杂了,无法在这里全面介绍。 您必须阅读我发布的Linq简介!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM