C# vb.net 下载到excel

前台
                        ImageUrl="~/paReport/Images/sucaiwcom7804000eyu.gif" 
                        οnclick="btnDownLoad_Click1" Width="16px" />




C#的下载到excel
string filename = "SSL_WIP_Data_" + DateTime.Now.ToString("yyyy-MM-dd");
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "utf-8";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);


        tw.Write("");
        this.GridView1.RenderControl(hw);
        Response.Write(tw.ToString());


        tw.Write("");


        Response.End();


vb.net的下载到excel
       Response.Clear()
        Response.Buffer = True
        Response.Charset = "GB2312"


        Response.AddHeader("content-disposition", "attachment;filename=DataQuery_" & DateTime.Now.ToString("yyyy-MM-dd") & ".xls")
        Response.ContentType = "application/vnd.ms-excel"
        Dim sw As New IO.StringWriter()
        Dim hw As New HtmlTextWriter(sw)


        GridView1.AllowPaging = False
        CommTools.GridViewDataBind(GridView1, ds)


        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")




        For i As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1
            GridView1.HeaderRow.Cells(i).Style.Add("background-color", "gray")
        Next


        GridView1.RenderControl(hw)


        Dim style As String = ""
        Response.Write(style)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()


注意事项:------------------------------------------------------------------------------
1、在导出界面中重载
 C#   
          public   override   void   VerifyRenderingInServerForm(Control   control)   
          {   
                  //base.VerifyRenderingInServerForm(control);   
          }   
VB.net
  Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    End Sub
 2、在web.config中修改   
  或在页面aspx中添加 EnableEventValidation="false" 
 

你可能感兴趣的:(.net,Excel)