惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - lhx

C# 非活动窗口截屏 用32位应用程序读取64位注册表中的Office key WMI 查找所有物理网卡 [转:IE编程] 如何设置IE8的WebBrowser控件(MSHTML) 的渲染模式 C#实现根据图片的EXIF自动调整图片方向&lt;转&gt; memory released when you minimize the app 自定义控件属性的特性大全&lt;转&gt; 详解自定义托管宿主WCF解决方案开发配置过程(1)【转】 对比两个同类型的List<T>返回差异List<T>集合 - lhx - 博客园 .NET 特性Attribute 如何给DataGridViewComboBoxColumn写事件 将DataGridView选中行的值填充到符合命名规则的控件中[原创] Oracle 存储过程 例子~! oracle 存储过程的基本语法(转) 在linux下配置Struts中的Oracle 数据源(原创) 如何给linux添加新硬盘(转) linux 下 mysql-5.0.22. 的安装(转) jQuery Ajax 全解析 (转) tomcat6_jdk1.6_安装配置_开启自动运行,普通用户执行 (转)
将Excel的数据导入DataGridView中[原创]
lhx · 2008-11-26 · via 博客园 - lhx

作者:lhxhappy

连接:http://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;
        }