将DataGrid1中的内容通过button2导入Excel。我的表一共有四列,编号、课程、成绩、学期。按照大虾们指点,修改成这样了,不过现在程序运行时没有错误,但是点击按钮确没有反应。请高手们看看 怎么修改?请说的具体点,指出要改的地方!本人非常感谢!!!!!赶快有人来帮帮我吧!!
  程序主要代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
    public class ScoreQuery : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.NumericUpDown numericUpDown1;
        private System.Windows.Forms.NumericUpDown numericUpDown2;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.DataGrid dataGrid1;
        private System.Data.OleDb.OleDbConnection oleDbConnection1;
        private System.Windows.Forms.DataGrid dataGrid2;
        private System.Windows.Forms.Button button2;
        private System.ComponentModel.Container components = null;        public ScoreQuery()
        {
                       InitializeComponent();        }        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }        private void InitializeComponent()
        {
             // button2
            // 
            this.button2.Location = new System.Drawing.Point(474, 11);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(88, 23);
            this.button2.TabIndex = 10;
            this.button2.Text = "打印成绩列表";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
             // dataGrid1
            // 
            this.dataGrid1.AllowSorting = false;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.CaptionText = "成绩列表";
            this.dataGrid1.DataMember = "";
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, 64);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.PreferredColumnWidth = 100;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.Size = new System.Drawing.Size(464, 152);
            this.dataGrid1.TabIndex = 6;
            // ScoreQuery
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(640, 354);
            this.Controls.Add(this.dataGrid2);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "ScoreQuery";
            this.Text = "学生成绩查询";
            this.Load += new System.EventHandler(this.ScoreQuery_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid2)).EndInit();
            this.ResumeLayout(false);        }
        #endregion
        public bool strisyear(string s)
        {
            if (s.Trim().Length != 4)
                return (false);
            else
            {
                int n = 1;
                for (int i = 0; i <= s.Length - 1; i++)
                {
                    if (!System.Char.IsNumber(s[i]))
                    {
                        n = -1;
                        break;
                    }
                }
                if (n == 1)
                    return (true);
                else
                    return (false);
            }
        }        private void button1_Click(object sender, System.EventArgs e)
        {
            if (!strisyear(textBox2.Text.Trim()) || !strisyear(textBox3.Text.Trim()))
                MessageBox.Show("年份输入非法");
            else if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show("学号不能为空");
            }
            else if (int.Parse(textBox2.Text.Trim()) <= int.Parse(textBox3.Text.Trim()) && numericUpDown1.Value <= numericUpDown2.Value)
            {
                string sql, xq1, xq2;
                xq1 = textBox2.Text.Trim() + numericUpDown1.Value.ToString();
                xq2 = textBox3.Text.Trim() + numericUpDown2.Value.ToString();
                sql = "select ScoreId as 编号,Course as 课程,Score as 成绩,cstr(cint(Semester/10))+'-'+cstr(cint(Semester/10)+1)+'年度第'+cstr(Semester mod 10)+'学期' as 学期 from ScoreInfo where StudentNumber='" + textBox1.Text.Trim() + "' and Semester between '" + xq1 + "'and '" + xq2 + "' order by Semester desc";
                OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
                DataSet ds = new DataSet();
                adp.Fill(ds, "score");
                if (ds.Tables["score"].Rows.Count != 0)
                {
                    dataGrid1.DataSource = ds.Tables["score"].DefaultView;
                    dataGrid1.CaptionText = "共检索出" + ds.Tables["score"].Rows.Count + "门课程";
                    for (int i = 0; i <= ds.Tables["score"].Rows.Count - 1; i++)
                        dataGrid1[i, 0] = i + 1;                    sql = "select cstr(cint(Semester/10))+'-'+cstr(cint(Semester/10)+1)+'年度第'+cstr(Semester mod 10)+'学期' as 学期,avg(Score) as 平均成绩,sum(Score) as 总成绩,count(ScoreId) as 课程数量  from ScoreInfo where StudentNumber='" + textBox1.Text.Trim() + "'and Semester between '" + xq1 + "'and '" + xq2 + "' group by Semester";
                    adp.SelectCommand.CommandText = sql;
                    DataSet ds2 = new DataSet();
                    adp.Fill(ds, "tj");
                    dataGrid2.DataSource = ds.Tables["tj"].DefaultView;
                }
                else
                {
                    dataGrid1.DataSource = null;
                    dataGrid1.CaptionText = "暂时无纪录";
                }
            }
            else
            {
                MessageBox.Show("请在左边输入较小的学期数值");
            }
        }        private void button2_Click(object sender, System.EventArgs e)
        {
            string sql, xq1, xq2;
            xq1 = textBox2.Text.Trim() + numericUpDown1.Value.ToString();
            xq2 = textBox3.Text.Trim() + numericUpDown2.Value.ToString();
            sql = "select ScoreId as 编号,Course as 课程,Score as 成绩,cstr(cint(Semester/10))+'-'+cstr(cint(Semester/10)+1)+'年度第'+cstr(Semester mod 10)+'学期' as 学期 from ScoreInfo where StudentNumber='" + textBox1.Text.Trim() + "' and Semester between '" + xq1 + "'and '" + xq2 + "' order by Semester desc";
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, oleDbConnection1);
            DataSet ds = new DataSet();
            adp.Fill(ds, "score");
            AddExcel(ds);        }        protected void AddExcel(DataSet ds)
        {
            DataTable dt = ds.Tables["score"];
            string fileName = Guid.NewGuid() + ".xls";
            Excel.Application excel = new Excel.ApplicationClass();
            int rowIndex = 1;
            int colIndex = 0;
            excel.Application.Workbooks.Add(true);            foreach (DataColumn col in dt.Columns)
            {
                colIndex++;
                excel.Cells[1, colIndex] = col.ColumnName;
            }
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 0;
                for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
                }
            }
            excel.Visible = false;
            excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
            excel.Quit();
            excel = null;
            GC.Collect();
        }
        private void ScoreQuery_Load(object sender, EventArgs e)
        {        }    }
}