private void btnimport_Click(object sender, EventArgs e)
{
string filePath = Application.StartupPath + "\\test.xls";
string sqlconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
string sql = @"select * from [Sheet1$]";
try
{
using (OleDbConnection conn = new OleDbConnection(sqlconn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn))
{
System.Data.DataTable dt = new System.Data.DataTable();
adapter.Fill(dt);
this.dataGridView1.DataSource = dt;
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// MessageBox.Show(dt.Rows[i]["name"].ToString());//行集合.行【号】列【名】
// MessageBox.Show(dt.Rows[i]["age"].ToString());//行集合.行【号】列【名】
//}
}
}
}
catch (Exception ex)
{
MessageBox.Show("打开文件出错,错误信息:" + ex.Message.ToString(), "提示");
}
}
c# excel 导入
最新推荐文章于 2024-07-26 16:48:19 发布