C#如何操作多个EXCEL表,我使用的连接方式是与数据库的连接一样!例如我有多个数据表,nic$,学生表$,oyrf$,另外在学生表里,第一行是考试科目,和考查科目,下一行就是对应的考试科(数学,操作系统...),和考查科(思想道德,职业规划...),我想读取数据时能够区分字段,就是考试科读考试科,考查科读考查科,现在就是不能确定他们的位置!

解决方案 »

  1.   

    和操作数据库表一样
    给你个例子
    sqlstr = "select * from [nic$] "
    注意表名后要加$
      

  2.   

    excel 表的第一行做为字段名
      

  3.   

    例如张三他的成绩分第一学期和第二学期,第一学期是一张表,第二学期是一张表,但是我显示数据时(DataGuidView),单击组合框就的第一学期就可以显示第一学期的成绩,选第二学期就可以显示第二张表!
      

  4.   

    C#不是有DataSet吗,把全部表导入不就可以了用
    DataSet thisDataSet;
    thisDataSet.Tables["表名"]就可以访问了啊
    连数据你用DataSet吗??DataRelation还能够设置他们表之间的对应关系
      

  5.   

    这个是你的思路的问题.
    不知道你想做成怎样的界面.
    给你个方法.
    先绑定学期,datagridview 加一个控件(由你定),点击后,传参数,再查出学期?表数据,再绑定另一个datagridview或另一个界面.
      

  6.   

    登陆框,学号是密码,表的姓名是用户名,当登陆后显示的是FORM2,里面有DataGuidView,显示的是第一学期和第二学期对应学号的人的成绩信息!
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    namespace excelData
    {   public partial class Form1 : Form
        {
            public OleDbDataAdapter da;
            public OleDbConnection conn;
            public DataSet ds;
           public string num;
            
            public void showExcel()
            {
                conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=admin.xls;Extended Properties=Excel 8.0");
                string sql = "select * from [第一学期$]";
                da = new OleDbDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds, "[第一学期$]");
                ds.Tables["[第一学期$]"].PrimaryKey = new DataColumn[] { ds.Tables["[第一学期$]"].Columns["学号"] };
            }        public Form1()
            {
                InitializeComponent();
            }       private void button1_Click(object sender, EventArgs e)
           {           try
               {
                   //if (textBox1.Text.Trim() != "" & textBox2.Text.Trim() != "")
                   //{
                       num = textBox2.Text;
                       showExcel();
                       
                       DataRow dr = ds.Tables["[学生表$]"].Rows.Find(num);                   if (textBox1.Text == "黄春娇" || textBox1.Text == "王国贸" || textBox1.Text == "赵秀琼" & textBox2.Text == "123" & comboBox1.SelectedItem == "班干部")
                       {
                           Form3 f3 = new Form3();
                           f3.Show();
                       }
                       else if (textBox1.Text == dr["姓名"].ToString() & textBox2.Text == dr["学号"].ToString() & comboBox1.SelectedItem == "学生")
                       {
                           Form2 f2 = new Form2();
                           f2.Show();
                       }
                       else if (textBox1.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
                       {
                           MessageBox.Show("请输入用户名", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                       }
                       else if (textBox2.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
                       {
                           MessageBox.Show("请输入密码", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                       }
                       else if (textBox1.Text == "" & textBox2.Text == "" & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部")
                       {
                           MessageBox.Show("请输入用户名和密码", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                       }
                       else if (textBox1.Text != dr["姓名"].ToString() || textBox2.Text != dr["学号"].ToString() & comboBox1.SelectedItem == "学生" || comboBox1.SelectedItem == "班干部" & textBox1.Text != "黄春娇" || textBox1.Text != "王国贸" || textBox1.Text != "赵秀琼" || textBox2.Text == "123")
                       {
                           MessageBox.Show("用户名或密码错误", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                       }
                   }           
               //else
               //{
                 // MessageBox.Show("用户名或密码不能为空", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               //}
               //}
               catch (Exception ex)
               {
                   MessageBox.Show("错误原因:" + ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               }
                    
           }
                  private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.AcceptButton = button1;
            }       private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
           {
               if (comboBox1.SelectedItem == "学生")
               {
                   button1.Text = "查询";
               }
               else if (comboBox1.SelectedItem == "班干部")
               {
                   button1.Text = "登陆";
               }
           }
        }
    }
    这个 是我做的FORM1的代码!
      

  8.   

    private void Form2_Load(object sender, EventArgs e)
            {
                
                Form1 f1 = new Form1();
                num = f1.num;
                f1.showExcel();
                DataRow dr = ds.Tables["[第一学期$]"].Rows.Find(num);
                f2_dg.DataSource = ds.Tables["[第一学期$]"];
            }FORM1的值传给FORM2,但是运行时出现:未将对象引用设置到对象实例.