using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb; namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void btnImportExcel_Click(object sender, EventArgs e)
        {
            ImportExcel();
        }        public void ImportExcel()
        {
            openFileDialog1.Filter = "*.XLS | *.XLS";
            openFileDialog1.Title = "请选择要导入的电子电子表格";
            if (DialogResult.OK == openFileDialog1.ShowDialog(this))
            {
                //创建一个连接字符串 ...... 
                string connectionString = string.Format(
                    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;",
                    openFileDialog1.FileName);
                //创建一个连接
                OleDbConnection newConnection = new OleDbConnection(connectionString);
                newConnection.Open();
                string strExcel = "select * from 偏差报告";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, connectionString);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "偏差报告");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].TableName;
                newConnection.Close();                string cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;",
                        Application.StartupPath + ".//1.mdb");
                OleDbConnection conn = new OleDbConnection(cs);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("select * from 偏差报告", cs);
                da.Fill(ds);
                da.Update(ds.Tables["偏差报告"]);
                conn.Close();
            }
        }    }
}
以上是我的代码,我的想法是将Excel中的数据保存到数据集ds中(这步已经成功了),再将ds中的数据保存到Access中,不过不知道为什么存不上。

解决方案 »

  1.   

    这样如果行,那你告诉我。
    你ACCESS中有没有同EXCEL中的SCHEMA?
    否则你让DA如何update。
      

  2.   

    我是个新手,尤其在数据库方面。
    我也不知道这样行不行。
    Access中表的结构和Excel的相同。如果这种方法不行,请告知有没有可行的方法?
      

  3.   

    Excel To Access,,,也只能做到这样
      

  4.   

    应该有一个他的增删改语句。。类使于SQL中SqlCommandBuilder这个玩意 ,,,OLEDB中不知道怎么写。我去查等会。
      

  5.   

    应该要有这样一个语句。。OleDbCommandBuilder (OleDbDataAdapter) 不过你ACCESS表查出来的记录应该要有主键。。
      

  6.   

    能具体点吗,我的水平很菜。
    我是想将Excel表读入到Access的表“偏差报告”中。
      

  7.   

       public void ImportExcel() 
            { 
                openFileDialog1.Filter = "*.XLS ¦ *.XLS"; 
                openFileDialog1.Title = "请选择要导入的电子电子表格"; 
                if (DialogResult.OK == openFileDialog1.ShowDialog(this)) 
                { 
                    //创建一个连接字符串 ...... 
                    string connectionString = string.Format( 
                        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", 
                        openFileDialog1.FileName); 
                    //创建一个连接 
                    OleDbConnection newConnection = new OleDbConnection(connectionString); 
                    newConnection.Open(); 
                    string strExcel = "select * from 偏差报告"; 
                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, connectionString); 
                    DataSet ds = new DataSet(); 
                    myCommand.Fill(ds, "偏差报告"); 
                    dataGridView1.DataSource = ds; 
                    dataGridView1.DataMember = ds.Tables[0].TableName; 
                    newConnection.Close(); 
                    string cs = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};User ID=;Password=;", 
                            Application.StartupPath + ".//1.mdb"); 
                    OleDbConnection conn = new OleDbConnection(cs); 
                    conn.Open(); 
                    OleDbDataAdapter da = new OleDbDataAdapter("select * from 偏差报告", cs);                //加上这句你试试吧
                    OleDbCommandBuilder a = OleDbCommandBuilder (da) ;
                    da.Fill(ds); 
                    da.Update(ds.Tables["偏差报告"]); 
                    conn.Close(); 
                } 
            } 
      

  8.   


    应该是OleDbCommandBuilder a =new OleDbCommandBuilder (da)吧,也不行!
      

  9.   

    你把Excel中的数据每行按字段读出来,然后用insert语句插入数据库,这是一定可以的,只是有些麻烦
      

  10.   

    还有一个问题,首先是确认你的数据类型是对的,然后确认你Excel的数据在插入时符合数据完整性约束
    比如你插入的字段是一个外键,而插入的数据不符合约束,是不可以的我在C++里用Ado插入Sqlserver的时候就出了这问题,而且不会报错,很恶心