private void InsertToExcel()
{
string strCon =“ Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+strSaveFileName+";Extended Properties=Excel 12.0 xml;HDR=Yes;";
StringBuilder strbSql = new StringBuilder();
OleDbConnection oleDbCon = new OleDbConnection(strCon);
OleDbCommand oleDbCom = new OleDbCommand();
oleDbCom.Connection = oleDbCon;
oleDbCon.Open();
string strSql = "create table MySheet (队号 int, 队名 nchar(10), 队长 varchar(50), 老师 int,论文题目 nchar(10)) ";
oleDbCom.CommandText = strSql;
oleDbCom.ExecuteNonQuery();
for (int i = 0; i < dsSqlServer.Tables[0].Rows.Count; i++)
{
strbSql.Append("insert into [MySheet$] values('");
for (int j = 0; j < 3; j++)
{
strbSql.Append(dsSqlServer.Tables[0].Rows[i].ItemArray[j].ToString() + "','");
}
strbSql.Append(dsSqlServer.Tables[0].Rows[i].ItemArray[3].ToString() + "')");
strSql = strbSql.ToString();
oleDbCom.CommandText = strSql;
oleDbCom.ExecuteNonQuery();
strbSql.Remove(0, strbSql.Length);
}
注册Excel ISAM在“运行”对话框中:Regsvr32 C:\Windows\SysWOW64\msexcl40.dll 这个也注册成功了