表名的用[Sheet1$],可参考我写的文章http://blog.csdn.net/gdjlc/archive/2010/08/13/5810311.aspx
利用Excel模板生成Excel方法如下:
# //转换为物理路径
# string newFileName = HttpContext.Current.Server.MapPath("~/" + fileName);
# //根据模板正式生成该Excel文件
# File.Copy(HttpContext.Current.Server.MapPath("~/ContactTemplate.xls"), newFileName, true);
# //建立指向该Excel文件的数据库连接
# string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newFileName + ";Extended Properties='Excel 8.0;HDR=yes;IMEX=2'";
# OleDbConnection Conn = new OleDbConnection(strConn);
# //打开连接,为操作该文件做准备
# Conn.Open();
# OleDbCommand Cmd = new OleDbCommand("", Conn);
# foreach (DataRow DR in dt.Rows)
# {
# string XSqlString = "insert into [Sheet1$]";
# XSqlString += "([姓名],[性别],[联系电话]) values(";
# XSqlString += "'" + DR["ContactName"] + "',";
# XSqlString += "'" + (DR["ContactSex"].ToString() == "1" ? "男" : "女") + "',";
# XSqlString += "'" + DR["ContactPhone"] + "')";
# Cmd.CommandText = XSqlString;
# Cmd.ExecuteNonQuery();
# }