日期:2014-05-16 浏览次数:20788 次
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog og = new OpenFileDialog();
//过滤,只能选择excel文档
//og.Filter = "Excel文件(*.csv)|*.csv";
//og.Filter = "Excel Sheet(*.csv)|*.csv|All Files(*.*)|*.*";
//og.DefaultExt = "csv";
//og.Filter = "Excel文件|*.csv";
//点击确定而不是取消
if (og.ShowDialog() == DialogResult.OK)
{
//获得要导入的文件名字
string fileName = og.FileName;
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'";
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
System.Data.DataTable dt1 = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = dt1.Rows[0]["Table_Name"].ToString();
string sql = "select * from [" + tableName + "]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
System.Data.DataTable dt = new System.Data.DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
BindGrid(dt);
con.Close();
&n