日期:2014-05-18  浏览次数:21081 次

导入excel,报:定义了过多字段
C# code

  public DataSet ExcelToDs(string path)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + path + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();

            DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            DataSet ds = new DataSet();
            //一个EXCEL文件可能有多个工作表,遍历之
            foreach (DataRow dr in dtSchema.Rows)
            {
                string table = dr["TABLE_NAME"].ToString();
                if (table == "'map copy$'")
                {
                    string tname = "map copy$";
                    string strExcel = "SELECT * FROM [" + tname + "A1:IV100]";
                    ds.Tables.Add(table);

                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, conn);
                   [color=#FF0000] myCommand.Fill(ds, table);//定义了过多字段[/color]                }
            }

            conn.Close();
            return ds;
        }




怎么解决

------解决方案--------------------
map copy$名字如果带空格,两边需要加单引号
------解决方案--------------------
我也就只看出tname连接A1:IV100里有些问题要处理下
------解决方案--------------------
确实无法在一个表中创建过多的Field。