ADO.NET 如何读取 Excel (上)

// 连接字符串

string xlsPath = Server.MapPath("~/app_data/somefile.xls"); // 绝对物理路径

string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +

"Extended Properties=Excel 8.0;" +

"data source=" + xlsPath;

// 查询语句

string sql = "SELECT * FROM [Sheet1$]";

DataSet ds = new DataSet();

OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);

da.Fill(ds);    // 填充DataSet

// 在这里对DataSet中的数据进行操作

// 输出,绑定数据

GridView1.DataSource = ds.Tables[0];

GridView1.DataBind();

(0)

相关推荐