前提:有数据库db 
其中有数据表student(包括:姓名,性别代码)
                 xb(包括:性别代码,性别)
已有的Excel数据表“excel1”内容包括:姓名 性别
实现功能:点击“导入”按钮,打开窗口选择excel1表,dataSet中显示数据谢谢大家,帮忙解决一下

解决方案 »

  1.   

    这里对你有帮助http://xhp5678.cnblogs.com/archive/2005/08/13/214126.html
      

  2.   

    private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
    {
    string reFileName=this.openFileDialog1.FileName;
    try
    {
    GetPath();
    OleDbConnection ExcelConn=new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source="+reFileName+";Extended Properties=Excel 8.0;");
    ExcelConn.Open();
    sda.SelectCommand=new OleDbCommand("select * from [Sheet1$]",ExcelConn);
    sda.Fill(ds,"CurrentTable");
    this.dataGrid1.DataSource=ds.Tables["CurrentTable"].DefaultView;
    ExcelConn.Close();
    ExcelConn.Dispose();
    }
    catch(Exception err)
    {
    MessageBox.Show(err.Message);
    }
    }
      

  3.   

    先添加Excel的引用。
    public DataSet ExcelToDS(string Path) 

    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + Path + ";" +
    "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); 
    conn.Open(); 
    string strExcelPSID = ""; 
    string strExcelBox = "";
    OleDbDataAdapter myCommand = null; 
    DataSet ds = null; 
    strExcelPSID="select * from [Sheet1$]"; //Excel表Sheet1
    myCommand = new OleDbDataAdapter(strExcelPSID, strConn); 
    ds = new DataSet(); 
    myCommand.Fill(ds,"Sheet1"); 
    strExcelBox="select * from [Sheet2$]"; //Excel表Sheet1
    myCommand = new OleDbDataAdapter(strExcelBox, strConn); 
    conn.Close();
    myCommand.Fill(ds,"Sheet2"); 
    return ds; 
    }
    返回DataSet,可以DataGrid绑定或直接导入到Sql数据库
      

  4.   

    我的那段代码拿到asp.net就能用,不过要告诉你的是,这个只能在服务器上导入,如果你想在客户端导入到服务器DataSet,考虑把Excel文件上传到服务器后再导入吧,如果我们在客户端能直接把文件内容导入到服务器,呵呵。
      

  5.   

    換個思維:把數據先導入到數據庫中,再顯示。
    建議導入方法:調用DTS方式導入,又快又方便。
      

  6.   

    在Web客户端 你可以直接显示到DataSet?
    DataSet是??
      

  7.   

    你可以按照kingdhy(飞鹰) 的方法先将excel导入到dataset中。然后再将dataset里的内容一条一条地加入到数据库中。我目前也就只找出了这个方法。呵呵