zoukankan      html  css  js  c++  java
  • 把数据库里的数据用Excel文件的格式显示在浏览器中

    页面的载入事件:
     private void Page_Load(object sender, System.EventArgs e)
      {
       DataSet objDataset = new DataSet();
       SqlConnection objConn = new SqlConnection();
       objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
       objConn.Open();
       SqlDataAdapter objAdapter = new SqlDataAdapter("Select top 5 * from customers where country='USA'",objConn);
       objAdapter.Fill(objDataset); 
       DataView oView = new DataView(objDataset.Tables[0]);
       dgExcel.DataSource = oView;
       dgExcel.DataBind();
       objConn.Close();
       objConn.Dispose();
       objConn = null;
       if(Request.QueryString["bExcel"] == "1")
       {
        Response.ContentType = "application/vnd.ms-excel";
        
        Response.Charset = "";

        //关闭 ViewState
        EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
        //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
        //获取control的HTML
        dgExcel.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
        // 把HTML写回浏览器
        Response.Write(tw.ToString());
        Response.End();
       }

    在页面上按Button,触发Button_Click 事件,实现功能
    private void btnGetExcel_Click(object sender, System.EventArgs e)
      {
       Response.Redirect("excel.aspx?bExcel=1");
      }

  • 相关阅读:
    Codeforces Round #627 (Div. 3) 总结
    [IOI1994] 时钟
    收集一些优秀的甲方安全开源项目
    python基础——对时间进行加减
    JSFinder:一个在js文件中提取URL和子域名的脚本
    python对齐输出
    python使用smtplib发送邮件
    任务2:扫描渗透测试(50分)[2019年信息安全管理与评估赛题答案-01]
    记一次Xmrig挖矿木马排查过程
    Bypass xss过滤的测试方法
  • 原文地址:https://www.cnblogs.com/conquer/p/553355.html
Copyright © 2011-2022 走看看