简体   繁体   English

将EXCEL工作表绑定到dataGridView吗?

[英]Binding an EXCEL worksheet to a dataGridView?

I am trying to bind an EXCEL datasheet to a dataGridView ? 我正在尝试将EXCEL数据表绑定到dataGridView吗? I think that this is close but I do not know why the data is not showing up in the grid. 我认为这已经很接近了,但是我不知道为什么数据没有显示在网格中。 I have seen several post on Stackoverflow but I could not get any of them to work. 我已经在Stackoverflow上看到了几篇文章,但是我无法使它们起作用。 So I decided to put my own example up and see if I can get someone to try this code. 因此,我决定提出自己的示例,看看是否可以找人尝试这段代码。 All the form needs is a button with the click event and the dataGridView. 表单所需的全部是带有click事件和dataGridView的按钮。

Class Code: 班级代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Mail;
    using System.Data.OleDb;

    namespace TestExcel
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                String name = "Items";
                String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                                @"C:\Users\itpr13266\Desktop\test.xls" + 
                                ";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);
                DataTable data = new DataTable();
                sda.Fill(data);
                dataGridView1.DataSource = data;
            }
        }
    }

dataGridView1.DataSource = data;之后,您缺少dataGridView1.DataBind() dataGridView1.DataSource = data;

Change your connection string Provider to the below given as , check in your excel file name is "test.xls" and sheet name is "Items" or not ? 将您的connection string Provider更改为以下提供的,请检查您的excel文件名是否为“ test.xls”,工作表名称是否为“ Items”?

 String filenamewithpath = @"C:\Users\itpr13266\Desktop\test.xls";

 String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filenamewithpath +
                          ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

here is the complete code.Works 100% 这是完整的代码。Works100%

    private void button1_Click(object sender, EventArgs e)
    {
        String name = "Items";
        String filenamewithpath = @"C:\Users\itpr13266\Desktop\test.xls";

        String constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filenamewithpath +
                                 ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

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

        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
        DataTable data = new DataTable();
        sda.Fill(data);
        dataGridView1.DataSource = data;
    }

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

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