Dec 4, 2008

C# 操作 Excel

// 连接字符串
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;";
strConn += "Extended Properties='Excel 8.0;HDR=No; IMEX=1;';";
strConn += "data source=" + xlsPath;

// 查询语句
string cmdText = "SELECT * FROM [Sheet1$]";

OleDbConnection connDB = new OleDbConnection(strConn);
OleDbCommand cmdDB = new OleDbCommand(cmdText, connDB);
OleDbDataReader drdr;

// 打开Excel表格
connDB.Open();

// 执行 sql语句
drdr = cmdDB.ExecuteReader();


if(drdr.Read())
{
..............

drdr.GetValue(i)

...............

}

No comments:

Post a Comment

您的评论将使我blog更有动力~