winform中excel怎么绑定到dataGridView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform中excel怎么绑定到dataGridView相关的知识,希望对你有一定的参考价值。
3个button,单击button1时将excel文件book1.cls导入dataGridView1
单击button2时book2.cls导入dataGridView1
单击button3时book3.cls导入dataGridView1
book1的路径是D:\WORK\练习\book1.cls。网上找的别人的代码里有一个ofd控件,如下,不要ofd,要直接单击button就在datagridview里显示对应数据,求修改下代码。分数不多,谢谢帮忙!初学者...
代码:http://blog.sina.com.cn/s/blog_4c9ae6fc0100e0ai.html
初学的超级新手..还没涉及到连接数据库.静态的就成..请说具体点吧,有代码最好,谢谢了
可以使用winform编程的方式实现,步骤如下:
读取这个excel文件然后生成Datable 。
在winform前台界面选择生成的Datable 。
然后填写以下代码:
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace 读Excel文件
public partial class Form1 : Form
public Form1()
InitializeComponent();
/// <summary>
/// 选择文件按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
this.textBox1.Text = this.openFileDialog1.FileName;
/// <summary>
/// 点击导出excel按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
string File = this.openFileDialog1.FileName;
DataTable dt = ExcelUp(File);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = dt;
/// <summary>
/// 读取指定excel表中的内容返回datatable
/// </summary>
/// <param name="fileName">文件地址</param>
/// <returns>表中内容</returns>
public DataTable ExcelUp(string fileName)
string filePath = fileName;//读取excel文件路径;
DataTable dt = GetDataTable("Sheet1", filePath);
return dt;
/// <summary>
/// 读取excel指定页中的内容
/// </summary>
/// <param name="strSheetName">页名</param>
/// <param name="strExcelFileName">excel路径</param>
/// <returns></returns>
protected DataTable GetDataTable(string strSheetName, string strExcelFileName)
//源的定义
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=0;" + "Extended Properties=\'Excel 8.0;HDR=NO;IMEX=1\';", strExcelFileName);
//Sql语句
string strExcel = string.Format("select * from [0$]", strSheetName);
//定义存放的数据表
DataSet ds = new DataSet();
//连接数据源
OleDbConnection conn = new OleDbConnection(strConn);
try
conn.Open();
//适配到数据源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
catch (Exception e)
throw e;
finally
conn.Close();
return ds.Tables[strSheetName];
public void ExcelToDGV(string path)
DataSet m_ds = new DataSet();
string strConn = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = "
+ path + ";Extended Properties=Excel 8.0";
string strSheetName = "sheet1"; //默认sheet1
string strExcel = string.Format("select * from [0$]", strSheetName);
using (OleDbConnection conn = new OleDbConnection(strConn))
try
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(m_ds, strSheetName);
catch (Exception ex)
MessageBox.Show(ex.ToString());
finally
conn.Close();
this.dataGridView1.DataSource = m_ds.Tables[strSheetName];
#endregion
以上是关于winform中excel怎么绑定到dataGridView的主要内容,如果未能解决你的问题,请参考以下文章
winform中dataGridView上怎么修改、保存数据啊,急用啊?
winforms DataGridViewComboBoxColumn,设置 DataPropertyName 时 ComboBox 不可点击
C# 自己写的Winform程序批量导入Excel文件到Oracle数据库的过程中,程序运行会很慢!而且Winform窗体会卡