如何将excel中的数据读入数据集 并显示在gridview上 (C# 做应用程序)

2024-12-23 13:54:35
推荐回答(1个)
回答1:

头文件:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
代码:
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=xls路径;"+
@"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Excel的Sheet名$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);
this.dataGrid1.DataSource=myDataSet;
this.dataGrid1.DataMember=myDataSet.Tables[0].TableName;