如何在将数据导入sql server时解决此异常?

时间:2017-05-22 04:54:23

标签: c# asp.net excel

Microsoft Office Access数据库引擎找不到对象'Sheet1 $$ A7:B'。确保对象存在,并且您正确拼写其名称和路径名称。

using (OleDbConnection excel_con = new OleDbConnection(conString))
        {
            excel_con.Open();
            string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
            DataTable dtExcelData = new DataTable();

            //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
            dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("PersonId", typeof(int)),
            new DataColumn("Name", typeof(int)),
            new DataColumn("Salary",typeof(decimal)) });

            using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "$" + "A7:B]", excel_con))
            {
                oda.Fill(dtExcelData);
            }
            excel_con.Close();

0 个答案:

没有答案