日期:2014-05-17 浏览次数:20468 次
protected void Export_Click1(object sender, EventArgs e)
{
sql11 = "select * ....";
SqlConnection mycon = da.sqlcon;
SqlDataAdapter sqlcmd = new SqlDataAdapter(sql11, mycon);
DataSet ds = new DataSet(); //将查询结果放到DataSet数据集中
sqlcmd.Fill(ds, "Photo"); //这个Photo是ds名称,并不是数据表名,可以随便填,但为了方便,一般都用数据表名。
DataTable DT = ds.Tables[0];
//生成将要存放结果的Excel文件的名称
string NewFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
//转换为物理路径
NewFileName = Server.MapPath("../Temp/" + NewFileName);
//根据模板正式生成该Excel文件
File.Copy(Server.MapPath("../PeaceScore_End.xls"), NewFileName, true);
//建立指向该Excel文件的数据库连接
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NewFileName + ";Extended Properties='Excel 8.0;'";
OleDbConnection Conn = new OleDbConnection(strConn);
//打开连接,为操作该文件做准备
Conn.Open();
//OleDbCommand Cmd = new OleDbCommand("", Conn);
string XSqlString;
foreach (DataRow DR in DT.Rows)
{
XSqlString = "insert into [Sheet1$]";
XSqlString += "([登录名],[成绩]) values(";
XSqlString += "'" + DR["StudentName"] + "',";
XSqlString += "'" + DR["PeaceScore"] + "')";
&nbs