ASP.NET 使用meta content= text/html 方式,將 Gridview export to Excel


meta content= text/html and  gridview export to excel 

資料匯出

由於是meta content= text/html 格式,所以報錯

格式與網頁一樣



.ASP
......................
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="btnExport" runat="server" Text="匯出" Font-Size="X-Large" Width="100px" />      
                <asp:GridView ID="GridView1" runat="server" />  
            </ContentTemplate>
            <triggers>
                 <asp:postbacktrigger controlid="btnExport" />
            </triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>

.ASP.VB
......................
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            DatatableBindToGridview()
        End If
    End Sub

    Private Sub DatatableBindToGridview()
        Dim dt As New DataTable
        With dt
            .Columns.Add("格式1", GetType(String))
            .Columns.Add("格式2", GetType(String))

            For i As Integer = 0 To 10
                .Rows.Add()
                .Rows(i).Item(0) = Format(i, "00")
                .Rows(i).Item(1) = Format(i, "00")
            Next
        End With

        With GridView1
            .DataSource = dt
            .DataBind()
            .HorizontalAlign = HorizontalAlign.Center
            .GridLines = GridLines.Both
            .HeaderStyle.BorderColor = Drawing.Color.Black
            .HeaderStyle.BackColor = Drawing.Color.Gray
            .HeaderStyle.ForeColor = Drawing.Color.White
            .Font.Size = 14
        End With
    End Sub

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        If GridView1.Rows.Count < 0 Then Exit Sub

        If e.Row.RowType = DataControlRowType.DataRow Then
            If e.Row.RowIndex Mod 2 = 0 Then
                e.Row.BackColor = Drawing.Color.LemonChiffon
            Else
                e.Row.BackColor = Drawing.Color.AliceBlue
            End If
        End If
    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        '少了這個Sub
        'GridView1.RenderControl 時會產生
        '型別 'GridView' 的控制項 
        'GridView1 必須置於有 runat=server 的表單標記之中。
    End Sub

    Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExport.Click
        Dim sw As New System.IO.StringWriter
        Dim hw As New System.Web.UI.HtmlTextWriter(sw)
        GridView1.RenderControl(hw)

        With Response
            .ClearContent()
            .AppendHeader("Content-Disposition", "attachment; filename=" & Format(Now, "yyyyMMdd HHmmss") & ".xls")
            .ContentType = "application/vnd.ms-excel"
            .Write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
            .Write("<style type=text/css>")
            .Write("td{mso-number-format:\@;}")
            .Write("</style>")
            .Write(sw.ToString())
            .End()
        End With
    End Sub

沒有留言:

張貼留言