public void GetData(string sqlstr,bool ifexcel) {

    //导出excel格式
        if (ifexcel)
        {
             HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";

            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/vnd.xls";
       
        }

        //SqlConnection scon = new SqlConnection("server=(locaL)\\DIH;uid=cronfuser;password=cronfuser;database=cronf2");
        SqlConnection scon = new SqlConnection(sqlcon);
        SqlCommand scm = new SqlCommand(sqlstr, scon);
        scm.CommandTimeout = 400;
        SqlDataAdapter sqldata = new SqlDataAdapter(scm);
sqldata.SelectCommand.CommandTimeout = 400;//设置查询sql的超时时间
//sqldata.InsertCommand.CommandTimeout = 180;//设置插入sql的超时时间
//sqldata.UpdateCommand.CommandTimeout = 180;//设置修改sql的超时时间
//sqldata.DeleteCommand.CommandTimeout = 180;//设置删除sql的超时时间

//数据以table形式输出
        DataSet ds = new DataSet();
        sqldata.Fill(ds);

        DataTable dt = ds.Tables[0];
        Response.Write("<table cellspacing='0' cellpadding='0' border='1' class='resulttb'><tr>");

        foreach (DataColumn dc in dt.Columns)
        {
            Response.Write("<th class='resultth'>" + dc.ColumnName + "</th>");
        }
        Response.Write("</tr>");

        foreach (DataRow dr in dt.Rows)
        {
            Response.Write("<tr>");
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                Response.Write("<td class='resulttd'>" + dr[i].ToString() + "</td>");
            }
            Response.Write("</tr>");
        }
        Response.Write("</table>");
    }

声明:此文系舞林cuznwww.wulinlw.org)原创稿件,转载请保留版权

et_highlighter