简体   繁体   English

将Excel数据导入到dataGridView

[英]Importing excel data to dataGridView

I've written the code below that allows the data in Excel to transfer to the dataGridView box. 我编写了下面的代码,该代码允许Excel中的数据传输到dataGridView框中。 I have one text box that allows the user to input which sheet number they want to extract the data from. 我有一个文本框,允许用户输入要从中提取数据的工作表编号。 Because my Excel is in Turkish, the sheet numbers are labeled Sayfa1, Sayfa2 and so on. 因为我的Excel是土耳其语,所以工作表编号分别标记为Sayfa1,Sayfa2等。

However I keep on getting this error message: "external table is not in the expected format". 但是,我继续收到此错误消息:“外部表未采用预期的格式”。

Can anyone tell me what the problem might be? 谁能告诉我可能是什么问题? I am using windows application form in C#. 我在C#中使用Windows应用程序形式。

private void button_kaydet_Click(object sender, EventArgs e)
    {
      string PathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +                  "C:\\Users\\casperpc\\Desktop\\hey.xlsx" + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
        OleDbConnection conn = new OleDbConnection(PathConn);
      OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * [" + txtbx_sheetName.Text + "$]", conn);
        DataTable dt = new DataTable();
      myDataAdapter.Fill(dt);

      dataGridView1.DataSource = dt;
    }

I had to add "from" after "select *" in order to make the query work. 为了使查询工作,我必须在“选择*”之后添加“来自”。 Was that causing your problem? 那是你的问题吗? It threw an "Invalid Query" exception, not a data in unexpected type exception, when I ran it before inserting "from", but with the "from" operator, it functions exactly as I expected. 当我在插入“ from”之前运行它时,它引发了“ Invalid Query”异常,而不是意外类型异常中的数据,但是使用“ from”运算符,它的功能完全符合我的预期。

Is there anything unusual about the format of your .xlsx file? .xlsx文件的格式是否有异常之处?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM