[Implement] Import Excel To DataGridView

[Implement] Import Excel To DataGridView

必須import下列

using System;

using System.Data;

using System.Windows.Forms;

{
    System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection
        ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=
        Excel 8.0;");

    connection.Open();

    string query = "select * from [" + SheetName + "$]";

    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(query,  connection);
    System.Data.DataSet ds = new System.Data.DataSet();

    adapter.Fill(ds);

    return ds.Tables[0];
}

private void Form1_Load_1(object sender, EventArgs e)
{

    //Sheet1 = 要抓取的Sheet名稱
    dataGridView1.DataSource = GetDataTable(@"FilePath\TestExcel.xls", "Sheet1");
}