zoukankan      html  css  js  c++  java
  • 将excel表格导入到DataGridView

    using System.Data.OleDb;
    添加一个button控件,一个textBox控件,用于显示选择路径 
    private void loadxls()
            {           
               
                String  fileName = textBox1.Text;
                String connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Members.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";
    
                string strSQL = "SELECT * FROM [Sheet1$]";
    
                OleDbConnection excelConnection = new OleDbConnection(connectionString);
                excelConnection.Open();        // This code will open excel file.
    
                OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
    
                // create data table
                DataTable dTable = new DataTable();
                dataAdapter.Fill(dTable);
                dataGridView1.DataSource = dTable;
    
                dTable.Dispose();
                dataAdapter.Dispose();
                dbCommand.Dispose();
    
                excelConnection.Close();
                excelConnection.Dispose();            
            }
    
            private void button1_Click(object sender, EventArgs e)
            {          
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
    
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {               
                    textBox1.Text = openFileDialog1.FileName;
                    loadxls();
                }               
            }
  • 相关阅读:
    Linux ansible的group模块
    ansible copy 模块详解
    Linux centos yum仓库 自制
    ansible 的playbook脚本
    Linux centos 监控备份
    Linux centos nginx下载安装初步
    周总结5
    周总结4
    爬取
    结对开发
  • 原文地址:https://www.cnblogs.com/shouhouxiaomuwu/p/3389713.html
Copyright © 2011-2022 走看看