使用步奏:
1.导入插件using System.Data.OleDb;
2.获取文件路径
3.连接字符串
4.实例化Excel读取连接对象
5.打开连接对象
6.实例化Excel操作对象
7.使用while循环去除数据
示例:
//获取文件路径
string url = Server.MapPath("~/Excel/students.xls");
//连接字符串
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + url;
//实例化Excel读取连接对象
using (OleDbConnection con = new OleDbConnection(constr))
{
//打开连接
con.Open();
//实例化Excel操作对象
using (OleDbCommand com = new OleDbCommand("select * from [Sheet1$]", con))
{
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
var num = reader[0];
var name = reader[1];
Response.Write(num + " " + name + "<br>");
}
}
}