excel到datagridview [英] excel to datagridview

查看:73
本文介绍了excel到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从MS访问datagridview导入数据和图像。

现在我可以使用访问文件的excel文件以及如何使用?

i am using this code for import data and image from MS access to datagridview.
now can i use excel file insteted of access file and how?

private void MainForm_Load(object sender, EventArgs e)
    {
        // Declare dataBase variables
        string cnStr, cmdText;
        cnStr =  "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=Persons.mdb";
        OleDbConnection cn = new OleDbConnection(cnStr);
        OleDbCommand cmd;
        OleDbDataReader dr;
        DataTable dt = new DataTable("Persons");
        try
        {
            cn.Open();

            // Load Data into DataGridView
            cmdText = "SELECT * FROM Persons";
            cmd = new OleDbCommand(cmdText, cn);
            dr = cmd.ExecuteReader();
            if (dr.HasRows) dt.Load(dr);
            dr.Close();

            dgv.DataSource = dt;

            // Initialize DataGridView Columns
            dgv.RowHeadersVisible = false;
            foreach (DataGridViewColumn col in dgv.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
                col.ReadOnly = true;
                if (col.GetType().Name == "DataGridViewImageColumn")
                {
                    foreach (DataGridViewRow row in dgv.Rows)
                    {
                        if (row.IsNewRow) continue;
                        row.Height = row.Cells["image"].ContentBounds.Height + 6;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cn.Close();
            cn = null;
        }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        // Calling DataGridView Printing
        PrintDGV.Print_DataGridView(dgv);
    }
}

推荐答案

您只需要更改用于检索数据和休息的代码代码的代码无需更改。

You just need to change the code for retrieving data and rest of the code need not be changed.
cnStr =  "Provider=Microsoft.Jet.OLEDB.4.0;" +
                    "Data Source=Persons.xls;Extended Properties=Excel 8.0;";
            OleDbConnection cn = new OleDbConnection(cnStr);
            OleDbCommand cmd;
            DataTable dt = new DataTable("Persons");
            try
            {
                cn.Open();
 
                // Load Data into DataGridView
                cmd = System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1



,cn);
System.Data.DataSet ds = new System.Data.DataSet();
cmd.Fill(ds);
dt = ds.Tables [ 0 ];

dgv.DataSource = dt
", , cn); System.Data.DataSet ds = new System.Data.DataSet(); cmd.Fill(ds); dt=ds.Tables[0]; dgv.DataSource = dt



我还没有执行此代码,但这样的事情应该有效。



希望,它有帮助:)


I haven't executed this code but something like this should work.

Hope, it helps :)


这篇关于excel到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆