码迷,mamicode.com
首页 > Windows程序 > 详细

将Excel数据读入DataGridView

时间:2017-08-22 15:57:14      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:command   provider   led   file   open   dial   ==   tag   cti   

OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx";//过滤一下,只要表格格式的
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            openFileDialog.AddExtension = true;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            openFileDialog.ShowHelp = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + openFileDialog.FileName + ";Extended Properties=Excel 12.0";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                DataSet ds = null;
                strExcel = "select * from [Sheet1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                ds = new DataSet();
                myCommand.Fill(ds, "table1");
                dgv.DataSource = ds.Tables[0];
            }

  

将Excel数据读入DataGridView

标签:command   provider   led   file   open   dial   ==   tag   cti   

原文地址:http://www.cnblogs.com/guxia/p/7411666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!