好久没发帖了,已经在VB版发了,在这不再重述,能解决的一并给分,一万多可用分没用武之地,呵呵.http://community.csdn.net/Expert/topic/5049/5049065.xml?temp=.6038782

解决方案 »

  1.   

    string  strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Excel所在的路径;Extended Properties=Excel 8.0;";
    System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
    System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM ["+指定的Sheet名字+"$]", strConn);
    DataSet myDataSet= new DataSet();
    myCommand.Fill(myDataSet,"table1");
      

  2.   

    //参考
    http://dev.csdn.net/develop/article/15/15544.shtm
      

  3.   

    给你一个完整的类:
    using System;
    using System.Data;
    using System.Windows.Forms;
    using System.Data.OleDb;namespace Test
    {
    /// <summary>
    /// ExcelOperator 的摘要说明。
    /// </summary>
    public class ExcelOperator
    { #region 私有成员 /// <summary>
    /// 要操作的Excel文件名称
    /// </summary>
    private string m_ExcelFileName;         #region 数据库相关
            
    /// <summary>
    /// 链接对象
    /// </summary>
    private System.Data.OleDb.OleDbConnection m_Conn = null;
    /// <summary>
    /// 提取数据命令对象
    /// </summary>
    private System.Data.OleDb.OleDbCommand  m_CommSelect = null;
    /// <summary>
    /// 更新数据命令对象
    /// </summary>
    private System.Data.OleDb.OleDbCommand  m_CommUpdate = null;
    /// <summary>
    /// 数据适配器对象
    /// </summary>
    private System.Data.OleDb.OleDbDataAdapter m_da = null;  #endregion #endregion #region 属性
    public string ExcelFileName
    {
    get{return this.m_ExcelFileName;}

    set
    {
    this.m_ExcelFileName = value;
    this.m_Conn.ConnectionString = GetConnectionString(value);

    }
    }
    #endregion #region 构造函数 /// <summary>
    /// 带参数的构造
    /// </summary>
    /// <param name="FileName">Excel文件名</param>
    public ExcelOperator(string FileName)
    {
    this.m_ExcelFileName = FileName; ///构造数据库对象
    ///
    this.m_Conn = new OleDbConnection(GetConnectionString(FileName)); this.m_CommSelect = new OleDbCommand();
    this.m_CommUpdate = new OleDbCommand(); this.m_CommSelect.Connection = this.m_Conn;
    this.m_CommUpdate.Connection = this.m_Conn; this.m_da = new OleDbDataAdapter(); this.m_da.SelectCommand = this.m_CommSelect;
    this.m_da.UpdateCommand = this.m_CommUpdate;
    }
            
    /// <summary>
    /// 不带参数的构造
    /// </summary>
    public ExcelOperator()
    {
    ///构造数据库对象
    ///
    this.m_Conn = new OleDbConnection(); this.m_CommSelect = new OleDbCommand();
    this.m_CommUpdate = new OleDbCommand(); this.m_CommSelect.Connection = this.m_Conn;
    this.m_CommUpdate.Connection = this.m_Conn; this.m_da = new OleDbDataAdapter(); this.m_da.SelectCommand = this.m_CommSelect;
    this.m_da.UpdateCommand = this.m_CommUpdate;

    } #endregion
    #region 方法
             
    #region 私有方法
             
    /// <summary>
    /// 根据EXCEL文件名,返回连接字符串
    /// </summary>
    /// <param name="FileName">Excel文件名</param>
    /// <returns>连接字符串</returns>
    private static string GetConnectionString(string FileName)
    {
    return 
    @"Provider=Microsoft.Jet.OLEDB.4.0;" + 
    @"Data Source=" + FileName  + ";" + 
    @"Extended Properties=Excel 8.0"/* + Convert.ToChar(34).ToString() + 
    @"Excel 8.0;"+ "Imex=2;HDR=No;" + Convert.ToChar(34).ToString()*/; 
    }
    #endregion #region 公有方法 /// <summary>
    /// 返回Excel表中的Sheets
    /// </summary>
    /// <returns>Sheets的名称</returns>
    public string[] GetExcelSheets()
    {
       
    System.Data.DataTable dt = null; string[] res = null;
    try
    {
    //打开数据库链接
    this.m_Conn.Open(); //将Sheets获取到表中
    dt = this.m_Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
                     if(null != dt)
    {
    res = new string[dt.Rows.Count]; for(int i = 0;i<dt.Rows.Count;i++)
    {
    res[i] = dt.Rows[i]["TABLE_NAME"].ToString();
    res[i] = res[i].Substring(0,res[i].Length-1);
    } this.m_Conn.Close();
    }
    else
    {
    res = null;
    }
    }
    catch(System.Data.OleDb.OleDbException e)
    {
        

    this.m_Conn.Close(); MessageBox.Show("获取Excel文件工作表失败",e.Message);
    return null;

    } return res;
    }
    #endregion public DataSet ReadODBC(string SheetName)
    {
    this.m_CommSelect.CommandText = @"SELECT * FROM ["+SheetName+"$]";
    DataSet myDataSet = new DataSet();
    try
    {
    this.m_da.Fill(myDataSet);
    return myDataSet;
    }
    catch(System.Data.ConstraintException exp)
    {
    MessageBox.Show(exp.Message);
    return null;
    }
    catch(System.Data.OleDb.OleDbException exp)
    {
    MessageBox.Show(exp.Message);
    return null;
    }
    catch(System.Runtime.InteropServices.COMException exp)
    {
    MessageBox.Show(exp.Message);
    return null;
    }
    catch(Exception exp)
    {
    MessageBox.Show(exp.Message);
    return null;
    }
    } #endregion
    }
    }
      

  4.   

    //获取Execl中Sheet名字
    private static ArrayList GetExecl(ref ArrayList ary,string Url)
    {
    Object refmissing = System.Reflection.Missing.Value;
    Excel._Application exc = new Excel.ApplicationClass();
    exc.Visible = false;
    Excel.Workbooks workbooks = exc.Workbooks;
    workbooks._Open(Url,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing);
    for(int i=0;i<exc.Worksheets.Count;i++)
    {
    Excel.Worksheet sheet = (Excel.Worksheet)exc.Worksheets.get_Item(i+1);
    ary.Add(sheet.Name.ToString());
    }
    exc.Application.Quit();
    return ary;
    }
      

  5.   

    用GetExcelSheets()函数可以得到excel表中有数据的sheet,然后把sheet列到dropdownlist控件中供选择。用ReadODBC(string SheetName)方法来读取特定sheet内的数据
      

  6.   

    大家还是没有看明白我的意思.给定一个Sheet的名称我当然会怎么把他的数据读取出来,这个我已经在帖子上给出代码了.我现在的问题是不知道Sheet的名称下,我要获取Excel中第二个Sheet的数据内容,那怎么知道第二个Sheet的名称是什么呢???To Small__Wolf
    你的使用Excel.ApplicationClass对象的方法是可以实现我的要求.但我在帖子已经说明我不想使用Excel的COM对象了.To kbxj406(羽儿)
    你的方法只能在事先知道Excel文件中Sheet的名字才能读取数据.为什么呢?因为GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null)返回的表的顺序和Excel文件中实际的Sheet的顺序不是一一对应的,不信你可以试一下新建一个Excel文件,默认下返回的是"Sheet1","Sheet2","Sheet3".
    但如果你把Excel文件里面的Sheet的顺序调乱一下,它返回的还是"Sheet1","Sheet2","Sheet3".我发现GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null)返回的表名是经过排序的.但我希望返回的顺序和Excel文件里面的顺序要一样.
      

  7.   

    to 我现在的问题是不知道Sheet的名称下,我要获取Excel中第二个Sheet的数据内容,那怎么知道第二个Sheet的名称是什么呢???像你所说的,本来就没有什么太好的方法,毕竟计算机没那么智能。方法一:修改Sheet的名字,减少这种判断;方法二:逐个Sheet试着去读,或者判断某个单独的cell,来决定Sheet是否为你要读的。
      

  8.   

    唉,看来都是要用Excel对象了,因为我必须要按照实际顺序打开Excel各个表格啊!!!
      

  9.   

    DataTable tn = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    tn的"TABLE_NAME"列可以得到表名(已包含"$")用tn.Rows[1]["TABLE_NAME"]做为表名,你看行不行(这句没试验,直接打的,可能有错)