如何在c#中的datagridview中打开excel文件 [英] How to open excel file in datagridview in c#

查看:448
本文介绍了如何在c#中的datagridview中打开excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在datagridview中打开excel文件,我选择文件时的问题是不会在datagridview中显示所有文件?

I'm trying to open excel file in datagridview ,the problem when I choose file doesn't show all file in datagridview ?

OpenFileDialog op = new OpenFileDialog(); //tool to open the file

            op.Filter = "ALL Files |*.*|Excel Files |*.XLSX"; //select the type of file that want open it

            if (op.ShowDialog() == DialogResult.OK) //user chose the file and press ok
            {
                con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + op.FileName + ";Extended Properties=Excel 12.0"); //path of file(excel),and specifec the connection

                da = new OleDbDataAdapter("Select * from[sheet1$]", con);//get the data

                dt = new DataTable();

                da.Fill(dt);//save or put the data in data table
                this.dataGridView1.DataSource = dt;//show the table in grid view

推荐答案

,con); // 获取数据

dt = new DataTable();

da.Fill(dt); // 将数据保存或放入数据表
.dataGridView1.DataSource = dt; / / 在网格视图中显示表格
", con);//get the data dt = new DataTable(); da.Fill(dt);//save or put the data in data table this.dataGridView1.DataSource = dt;//show the table in grid view


Excel表格看起来像什么?



L只有整个表在A1中开始,数据列之间没有空列且数据行之间没有空行,Excel表中的oading数据才有效。



如果您试图在整个地方加载包含数据和文本的整个工作表,那么它就不会起作用。它只会加载看起来像表格的数据。
What does you Excel sheet look like?

Loading data from an Excel sheet will only work so long as the entire table starts in A1, has no empty columns between columns of data and no empty rows between rows of data.

If you're trying to load the entire sheet with data and text all over the place, it's not going to work. It will only load data that looks like a table.


这篇关于如何在c#中的datagridview中打开excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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