zoukankan      html  css  js  c++  java
  • 将Excel的数据导入DataGridView中(转)

    https://www.cnblogs.com/lhxhappy/archive/2008/11/26/1341873.html

    /// <summary>
    
            /// 点击按钮导入数据
    
            /// 作者:lhxhappy
    
            /// </summary>
    
            /// <param name="sender"></param>
    
            /// <param name="e"></param>
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                //打开一个文件选择框
    
                OpenFileDialog ofd = new OpenFileDialog();
    
                ofd.Title = "Excel文件";
    
                ofd.FileName = "";
    
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹,可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举,其中可以定义要返回路径的哪个系统目录
    
                ofd.Filter = "Excel文件(*.xls)|*.xls";
    
                ofd.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
    
                ofd.CheckFileExists = true;  //验证路径有效性
    
                ofd.CheckPathExists = true; //验证文件有效性
    
    
    
    
    
                string strName = string.Empty;
    
                if (ofd.ShowDialog() == DialogResult.OK)
    
                {
    
                    strName = ofd.FileName;
    
                }
    
    
    
                if (strName == "")
    
                {
    
                    MessageBox.Show("没有选择Excel文件!无法进行数据导入");
    
                    return;
    
                }
    
                //调用导入数据方法
    
                EcxelToDataGridView(strName, this.hpGridView1);
    
            }

    Excel数据导入方法

    /// <summary>
    
            /// Excel数据导入方法
    
            /// 作者:lhxhappy
    
            /// </summary>
    
            /// <param name="filePath"></param>
    
            /// <param name="dgv"></param>
    
            public void EcxelToDataGridView(string filePath,DataGridView dgv)
    
            {
    
                //根据路径打开一个Excel文件并将数据填充到DataSet中
    
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
    
                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");
    
    
    
                //根据DataGridView的列构造一个新的DataTable
    
                DataTable tb = new DataTable();
    
                foreach (DataGridViewColumn dgvc in dgv.Columns)
    
                {
    
                    if (dgvc.Visible && dgvc.CellType != typeof(DataGridViewCheckBoxCell))
    
                    {
    
                        DataColumn dc = new DataColumn();
    
                        dc.ColumnName = dgvc.DataPropertyName;
    
                        //dc.DataType = dgvc.ValueType;//若需要限制导入时的数据类型则取消注释,前提是DataGridView必须先绑定一个数据源那怕是空的DataTable
    
                        tb.Columns.Add(dc);
    
                    }
    
                }
    
    
    
                //根据Excel的行逐一对上面构造的DataTable的列进行赋值
    
                foreach (DataRow excelRow in ds.Tables[0].Rows)
    
                {
    
                    int i = 0;
    
                    DataRow dr = tb.NewRow();
    
                    foreach (DataColumn dc in tb.Columns)
    
                    {
    
                        dr[dc] = excelRow[i];
    
                        i++;
    
                    }
    
                    tb.Rows.Add(dr);
    
                }
    
                //在DataGridView中显示导入的数据
    
                dgv.DataSource = tb;
    
            }
  • 相关阅读:
    大学毕业4年-回顾和总结(1)-钱,金钱观
    VR开发中性能问题—OculusWaitForGPU
    动态规划(一)
    POJ 2229 Sumsets
    hihoCoder #1122 : 二分图二•二分图最大匹配之匈牙利算法
    hihoCoder #1127 : 二分图二·二分图最小点覆盖和最大独立集
    hihoCoder #1121 : 二分图一•二分图判定
    HDU 1978 How many ways
    POJ1579 Function Run Fun
    POJ 2081 Recaman's Sequence
  • 原文地址:https://www.cnblogs.com/janghe/p/8882420.html
Copyright © 2011-2022 走看看