private void Button1_Click(object sender, System.EventArgs e)
{
//this.BindDataGrid();      
Response.AddHeader("Content-Disposition", "attachment; filename=rsamember"); 
Response.Charset = "UTF-8"; 
Response.ContentType = "application/vnd.ms-excel"; 
//Remove the charset from the Content-Type header. 
Response.Charset = ""; 
//Turn off the view state. 
this.EnableViewState= false; 
System.IO.StringWriter tw = new System.IO.StringWriter(); 
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
//Get the HTML for the control. 
this.DataGrid1.RenderControl(hw); 
//Write the HTML back to the browser. 
Response.Write(tw.ToString()); 
//End the response. 
Response.End(); }

解决方案 »

  1.   

    好像是在head里面加上什么?
    就可以制定时下载还是打开
    选择应用程序我想是客户端设置的问题
    我没有遇到过这样的问题
    不好意思
    帮你up吧
      

  2.   

    if you have Excel installed, ty to remove this lineResponse.AddHeader("Content-Disposition", "attachment; filename=rsamember");
      

  3.   

    回saucer(思归) 去掉了Response.AddHeader("Content-Disposition", "attachment; filename=rsamember");
    的话
    出来的EXCEL没有内容
    请教 谢谢
      

  4.   

    我想也许是我的机子又是服务器 又调式 有EXCEL 
    我去掉EXCEL应该可以了
      

  5.   

    sorry, tryResponse.AddHeader("Content-Disposition", "inline; filename=rsamember.xls");
      

  6.   

    感谢您使用微软产品。建议您参阅下面的微软知识库文章:Q317719 HOW TO: Export Data in DataGrid on an ASP. NET WebForm to Excel
    http://support.microsoft.com/support/kb/articles/q317/7/19.asp希望对您有所帮助。-微软全球技术中心  -zgh本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  7.   

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.IO
    Imports System.Text
    Public Class OutportData
        Inherits System.Web.UI.Page
        Protected WithEvents CBL_Field As System.Web.UI.WebControls.CheckBoxList
        Protected WithEvents Btn_All As System.Web.UI.WebControls.Button
        Protected WithEvents Btn_Part As System.Web.UI.WebControls.Button
        Protected WithEvents Txt_TiaoJian As System.Web.UI.WebControls.TextBox
        Protected WithEvents Label2 As System.Web.UI.WebControls.Label
        Protected WithEvents Lbl_Message As System.Web.UI.WebControls.Label
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            Dim ConString As SqlConnection
            Dim DA As SqlDataAdapter
            Dim Ds As DataSet = New DataSet("Myset")
            Dim Temp As String
            Dim i As Integer
            If Session("username") Is Nothing Or Session("usertype") Is Nothing Then
                Response.Redirect("login.aspx")
            End If
            If Not IsPostBack Then
                ViewState("SourceTable") = "jy"
                ConString = New SqlConnection(Application("SqlConString"))
                DA = New SqlDataAdapter("Select * from " + CStr(ViewState("SourceTable")), ConString)
                DA.Fill(Ds, "SqlTable")
                For i = 0 To Ds.Tables("Sqltable").Columns.Count - 1
                    Temp = Ds.Tables("Sqltable").Columns(i).ColumnName
                    CBL_Field.Items.Add(Temp)
                Next
                Ds.Clear()
                ConString.Close()
            End If
        End Sub
        Private Sub Btn_All_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_All.Click
            On Error Resume Next
            Dim ZiDuan As String
            Dim I As Integer        Dim DA As SqlDataAdapter
            Dim Ds As DataSet = New DataSet("Myset")
            Dim ConString As SqlConnection
            Dim Dt As DataTable
            Dim Dr As DataRow
            Dim J As Integer
            Dim Excel As String
            Dim temp As String
            ConString = New SqlConnection(Application("SqlConString"))
            DA = New SqlDataAdapter("Select * from " + CStr(ViewState("SourceTable")) + " " + Txt_TiaoJian.Text, ConString)
            DA.Fill(Ds, "SqlTable")
            Dt = Ds.Tables("SqlTable")
            Excel = ""
            For I = 0 To Dt.Columns.Count - 1
                Excel = Excel + Dt.Columns(I).ColumnName + Chr(9)
            Next
            Excel = Excel + Chr(10)
            For I = 0 To Dt.Rows.Count - 1
                Dr = Dt.Rows(I)
                temp = ""
                For J = 0 To Dt.Columns.Count - 1
                    temp = temp + Trim(CStr(Dr(J))) + Chr(9)
                Next
                temp = temp + Chr(10)
                Excel = Excel + temp
            Next
            Ds.Clear()
            ConString.Close()
            Dim FileName As String
            FileName = Session("Username") + ".xls"
            Dim b() As Byte = Encoding.Default.GetBytes(Excel)
            Dim fs As New FileStream(Server.MapPath(".") + "\dbf\" + FileName, FileMode.OpenOrCreate)
            fs.Write(b, 0, b.Length)
            fs.Close()
            temp = Application("HttpPath") + "/dbf/" + FileName
            Response.Redirect(temp)    End Sub    Private Sub Btn_Part_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Part.Click
            On Error Resume Next
            Dim ZiDuan As String
            Dim I As Integer
            Dim DA As SqlDataAdapter
            Dim Ds As DataSet = New DataSet("Myset")
            Dim ConString As SqlConnection
            Dim Dt As DataTable
            Dim Dr As DataRow
            Dim J As Integer
            Dim Excel As String
            Dim temp As String        For I = 0 To CBL_Field.Items.Count - 1
                If CBL_Field.Items(I).Selected Then
                    temp = temp + CBL_Field.Items(I).Text + ","
                End If
            Next        If Len(temp) = 0 Then
                Lbl_Message.Style("color") = "red"
                Lbl_Message.Text = "请挑选字段"
                Exit Sub
            End If
            temp = Left(temp, Len(temp) - 1)
            ConString = New SqlConnection(Application("SqlConString"))
            DA = New SqlDataAdapter("Select " + temp + " from " + CStr(ViewState("SourceTable")) + " " + Txt_TiaoJian.Text, ConString)
            DA.Fill(Ds, "SqlTable")
            Dt = Ds.Tables("SqlTable")
            Excel = ""
            For I = 0 To Dt.Columns.Count - 1
                Excel = Excel + Dt.Columns(I).ColumnName + Chr(9)
            Next
            Excel = Excel + Chr(10)
            For I = 0 To Dt.Rows.Count - 1
                Dr = Dt.Rows(I)
                temp = ""
                For J = 0 To Dt.Columns.Count - 1
                    temp = temp + Trim(CStr(Dr(J))) + Chr(9)
                Next
                temp = temp + Chr(10)
                Excel = Excel + temp
            Next
            Ds.Clear()
            ConString.Close()
            Dim FileName As String
            FileName = Session("Username") + ".xls"
            Dim b() As Byte = Encoding.Default.GetBytes(Excel)
            Dim fs As New FileStream(Server.MapPath(".") + "\dbf\" + FileName, FileMode.OpenOrCreate)
            fs.Write(b, 0, b.Length)
            fs.Close()
            temp = Application("Httppath") + "/dbf/" + FileName
            Response.Redirect(temp)
        End Sub
    End Class