string path;
            OpenFileDialog ofd = new OpenFileDialog();            if (ofd.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("未选择文件");
                return;
            }
            
            path= ofd.FileName;//得到路径和文件名
            
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+path+";  Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string tableName="";
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tableName += dt.Rows[i][2].ToString().Trim();
            }            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            strExcel = "select * from ["+tableName+"] ";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            DataSet dataSet1 = new DataSet();
            myCommand.Fill(dataSet1, "["+tableName+"]"); 
            //以上代码经测试,通过
            //以下代码未通过,我想实现最简单的将dataset的修改内容保存入excel文件
            OleDbCommand dbcommand = new OleDbCommand();
            dbcommand.Connection=conn;
            dbcommand.CommandText = "update [" + tableName + "] set F10='aaa' where f1=1";
            dbcommand.ExecuteNonQuery();
            conn.Close();
            //以上代码编译出错,说是“少参数”

解决方案 »

  1.   

    最实在的解决办法:DataSet.WriteXml(...);
      

  2.   

    兄弟,输出后都是乱码,我要excel格式的,谢谢
      

  3.   

    问题重提一下
    string path;
    OpenFileDialog ofd = new OpenFileDialog();
    if (ofd.ShowDialog() != DialogResult.OK)
    {
    MessageBox.Show("未选择文件");
    return;
    }            
    path= ofd.FileName;//得到路径和文件名
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="+path+";Extended Properties=Excel 8.0;";
    OleDbConnection conn = new OleDbConnection(strConn);
    conn.Open();
    string tableName="";
    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    tableName += dt.Rows[i][2].ToString().Trim();
    }
    string strExcel = "";
    OleDbDataAdapter myCommand = null;
    strExcel = "select * from ["+tableName+"] ";
    myCommand = new OleDbDataAdapter(strExcel, strConn);
    DataSet dataSet1 = new DataSet();
    myCommand.Fill(dataSet1, "["+tableName+"]"); 至此已将EXCEL文件数据读取到DataSet中
    请高手给出,修改某条记录并且保存回原文件的代码,谢谢了,我要用ADO.net的方式做