ASP.NET匯出檔案(excel)

摘要:ASP.NET匯出檔案(excel)

 

 

excelReport.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="excelReport.aspx.vb" Inherits="excelReport" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>excel</title>
</head>
<body>
 
    $$list
 
</body>
</html> 

 

 

 excelReport.vb

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         aryRep.Add("$$list")
         aryStr.Add(strlist)
         print(aryRep, aryStr)
    End Sub

    Protected Sub print(ByRef aryRep As ArrayList, ByRef arystr As ArrayList)
       'aryRep=>tag array
       'arystr =>Value array

        Response.AddHeader("content-disposition", "attachment; filename=您的檔名.xls")
        Response.ContentType = "application/ms-excel"
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>")
        Dim tw As IO.StringWriter = New System.IO.StringWriter()
        Dim hw As HtmlTextWriter = New HtmlTextWriter(tw)
        RenderControl(hw)
        Dim output As String = tw.ToString

        For inti As Integer = 0 To aryRep.Count - 1
            output = output.Replace(aryRep(inti), arystr(inti))
        Next

        Response.Write(output)
        Response.End()
    End Sub