Exception Details: System.Data.OleDb.OleDbException: 'YSDTemplate$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。Source Error: 
Line 79:  oledbconn.Open();
Line 80:  OleDbCommand oledbcom=new OleDbCommand("select * from [YSDTemplate$]",oledbconn);
Line 81:  OleDbDataReader oledbdr=oledbcom.ExecuteReader(CommandBehavior.CloseConnection);
Line 82: 
Line 83:  while(oledbdr.Read())
 
坐等帮助,谢谢各位

解决方案 »

  1.   

    select * from [YSDTemplate$]这样能取到数据么?楼主在瞎搞???你的是EXCEL,不是数据库,还是用readstream吧
      

  2.   


    OleDbCommand oledbcom=new OleDbCommand("select * from [Sheet1$]",oledbconn);
      

  3.   

    同样得错误有一点我不明白,excel中插入菜单里面有个名称里面有个定义,那里有一项引用位置什么意思啊???
      

  4.   

    我给你一段 倒入DataTable的类
    using System;
    using System.Data;
    using System.Data.OleDb;namespace Common
    {
    /// <summary>
    /// Summary description for Excel.
    /// </summary>
    public class Excel
    {
    private const string STRING_CONECTTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"";
    public Excel()
    {
    //
    // TODO: Add constructor logic here
    //
    }
                       //excelFilePath Excel文件地址
    public static DataTable GetDataTable(string excelFilePath)
    {
    try
    {
    DataSet ds = new DataSet();
    string connstring = string.Format(STRING_CONECTTIONSTRING,excelFilePath);
                                        string sql="select * from [Sheet1$]";
    using(OleDbConnection cnn = new OleDbConnection(connstring))
    {
    cnn.Open();
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql,cnn);
    dataAdapter.Fill(ds);
    cnn.Close();
    return ds.Tables[0];
    }
    }
    finally
    {
    }
    }
                      
       }
    }
      

  5.   

    为什么一定要用sql了
    NickLee.Common.Excel就可以直接导入导出www.cnblogs.com/mail-ricklee上有下载地址。