如何在C#中将数据从Excel工作表获取到DataGridView [英] How to get data from excel sheet to datagridview in c#

查看:61
本文介绍了如何在C#中将数据从Excel工作表获取到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生您好,

在我的项目中,我必须从Excel电子表格中导入数据,并将其与datagridview控件绑定.在那我必须选择特定的工作表名称(数字).我使用过opendialog throw,它将打开excel工作表,请给我代码.

如何在可以附加到Datagridview的数据表中获取Excel工作表?请给我发送代码逻辑.

谢谢与问候

Vasu Hajare.

Hello sir,

In my Project I have to import data from an Excel spreadsheet and bind it with a datagridview control. In that I have to select particular sheet name(numbers). I have used opendialog throw which it will open excel sheet and please give me code.

How to take excel sheet in datatable that I can attach to the Datagridview? Please send me code logic.

Thanks & Regards

Vasu Hajare.

推荐答案

protected void Page_Load(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
        // Create the connection object 
        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {
            // Open connection
            oledbConn.Open();
 
            // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1


", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "Employees"); // Bind the data to the GridView GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } catch { } finally { // Close connection oledbConn.Close(); } }
", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "Employees"); // Bind the data to the GridView GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } catch { } finally { // Close connection oledbConn.Close(); } }




请从 Excel数据读取器-读取Excel文件 [
Hi

Please download this Dll(free to use) from Excel Data Reader - Read Excel files[^]

Use this C# code :
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file (''97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

//5. Data Reader methods
while (excelReader.Read())
{
	//excelReader.GetInt32(0);
}

//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();



您可以将该数据集分配给datagridview.



you can assign That dataset to datagridview.

datagridview.DataSource = result;
datagridview.DataBind();



编码愉快.



Happy coding.


这篇关于如何在C#中将数据从Excel工作表获取到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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