根据客户的查询条件 导出相应的数据  用Repeaterprivate void Button2_Click(object sender, System.EventArgs e)
{
ToExcel(Repeater1); //Repeater1 为Repeater ID
}
private void ToExcel(System.Web.UI.Control ctl)  
{  
Response.Charset="GB2312";  
Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");   

Response.ContentEncoding = System.Text.Encoding.UTF8;     

Response.ContentType = "application/ms-excel";   ctl.Page.EnableViewState = false;  
System.IO.StringWriter tw = new 
System.IO.StringWriter();  
System.Web.UI.HtmlTextWriter hw = new 
HtmlTextWriter(tw);   ctl.RenderControl(hw);   Response.Write(tw.ToString());  
Response.End();  
}

解决方案 »

  1.   

    http://sz.luohuedu.net/xml/ShowDetail.asp?id=6AFBF00B-459D-4642-AD14-8A4765FFAFCC
      

  2.   

    Dim strDown As String = " SELECT *"  & _
                                    " FROM tabA"
            strHeadText.Append(Chr(13))
            Dim sadDown As New SqlDataAdapter(strDown, strCnReadOnly)
            Dim dsDown As New DataSet()
            sadDown.Fill(dsDown, "Down")
            Dim intTempRow As Integer
            Dim intTempCol As Integer
            Dim trTempRow As DataRow
            Dim intMaxCol As Integer = dsDown.Tables("Down").Columns.Count
            Dim strCellContent As String   
            For intTempRow = 0 To dsDown.Tables("Down").Rows.Count - 1
                trTempRow = dsDown.Tables("Down").Rows(intTempRow)
                For intTempCol = 0 To intMaxCol - 1
                    strCellContent = trTempRow.Item(intTempCol).ToString()
                    strCellContent = Replace(strCellContent, Chr(9), "")
                    strCellContent = Replace(strCellContent, Chr(10), "")
                    strCellContent = Replace(strCellContent, Chr(13), "")
                    strHeadText.Append(strCellContent & Chr(9))
                Next
                strHeadText.Append(Chr(13))
            Next
            Response.Clear()
            Response.AppendHeader("Content-Disposition", "attachment;filename=" & strMainTable.Substring(4) & ".xls")
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.ContentType = "application/octet-stream"
            Response.Write(strHeadText)
            Response.End()