如何将数据从excel发送到datagrid视图? [英] how to send data from excel to datagrid view?

查看:81
本文介绍了如何将数据从excel发送到datagrid视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从excel发送到datagriview,该怎么办?谢谢.

ı want to send data from excel to datagriview how can ı do?thanks.

推荐答案

我希望以下代码对您有用.只需为excel文件设置连接字符串,它将像charm一样工作.

I hope below code will work for you. Just set the connection strings for excel file and it will work like charm.

public partial class Form1 : Form
   {
       string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Documents and Settings\\Test\\Desktop\\my\\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
       OleDbConnection con = new OleDbConnection();
       OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1


"); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dtExcel = GetDataFromExcel(); datagrid1.DataSource = dtExcel; } public DataTable GetDataFromExcel() { DataTable dt = new DataTable(); con.ConnectionString = connectionString; cmd.Connection = con; OleDbDataAdapter dap = new OleDbDataAdapter(); dap.SelectCommand = cmd; dap.Fill(dt); return dt; } }
"); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable dtExcel = GetDataFromExcel(); datagrid1.DataSource = dtExcel; } public DataTable GetDataFromExcel() { DataTable dt = new DataTable(); con.ConnectionString = connectionString; cmd.Connection = con; OleDbDataAdapter dap = new OleDbDataAdapter(); dap.SelectCommand = cmd; dap.Fill(dt); return dt; } }


请参见

这篇关于如何将数据从excel发送到datagrid视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆