Import data from excel using C#

In this example, it shows on how to import data from excel file and load to datagridview control.
Code snippet

String name = "Sheet1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
            @"|DataDirectory|\2014-2015 1ST SEM.xlsx" +
            ";Extended Properties='Excel 8.0;HDR=YES;';";

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

OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
dataGridView1.DataSource = data;
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView1.RowsDefaultCellStyle.BackColor = Color.Beige;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LemonChiffon;

Programming Language: C#
Database: MS Access

1 (mga) komento: