将Excel导入Datagridview -

时间:2014-03-01 14:28:57

标签: c# excel datagridview

我想将Excel导入Datagridview,搜索后发现:Import Excel to Datagridview

stirng file = "c:\myFile.xlsx";
DataGridView dgvIM;

private void Import()
        {
            String name = "Items";
            String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                            file +
                            ";Extended Properties='Excel 8.0;HDR=YES;';";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
            con.Open();

            OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
            System.Data.DataTable data = new System.Data.DataTable();
            sda.Fill(data);
            dgvIM.DataSource = data;
        }

并收到此错误:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: 'Items$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.

这是什么意思?

0 个答案:

没有答案