實現將DataGrid中資料倒出Excel檔案並下載

來源:互聯網
上載者:User
datagrid|excel|資料|下載 Imports System
Imports System.Text
Namespace toExcel

    '功能:將asp.net中DataGrid產生Excel檔案下載。
    'Mountains改進:1、支援中文 2、隱藏列不顯示
    '日期:2002.10.30
    Public Class DataGridToCSV

        Public Function GenerateFile(ByRef Page As System.Web.UI.Page, ByVal MyDataGrid As System.Web.UI.WebControls.DataGrid, ByVal FileName As String) As String

            Dim resp As HttpResponse
            Dim colCount As Integer = MyDataGrid.Columns.Count - 1

            resp = Page.Response

            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312") '解決中文亂碼之關鍵
            'resp.Charset = "utf-8"
            'resp.AddFileDependency(FileName)
            'resp.ContentType = "Text/HTML"
            ''resp.AppendHeader("Content-Type", "text/html; charset=gb2312")

            resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName)  '必要,做成下載檔案
   

            Dim colHeaders As String = ""
            Dim strItems As StringBuilder = New StringBuilder()

            Dim myCol As DataGridColumn

            Dim i As Integer

            For i = 0 To colCount
                myCol = MyDataGrid.Columns(i)
                If myCol.Visible = True Then
                    colHeaders = colHeaders & myCol.HeaderText.ToString & ","
                End If
            Next

            If colHeaders.Length > 0 Then
                colHeaders = colHeaders.Substring(0, colHeaders.LastIndexOf(","))
            End If

            colHeaders = colHeaders & Chr(13) & Chr(10)



            resp.Write(colHeaders)

            Dim colRow As String

            Dim item As DataGridItem

            For Each item In MyDataGrid.Items
                resp.Write(FormatExportRow(colCount, item, MyDataGrid))
            Next item

            resp.End()

        End Function

        Private Function FormatExportRow(ByVal colCount As Integer, ByVal Item As DataGridItem, ByVal MyDataGrid As System.Web.UI.WebControls.DataGrid) As String
            Dim strItem As String
            Dim i As Integer

            For i = 0 To colCount
                If MyDataGrid.Columns(i).Visible = True Then
                    If Item.Cells(i).Text Is System.DBNull.Value Then
                        Item.Cells(i).Text = ""
                    End If
                    If i = colCount Then
                        strItem += Item.Cells(i).Text.ToString & Chr(13) & Chr(10)
                    Else
                        strItem += Item.Cells(i).Text.ToString & ","
                    End If
                End If
            Next
            strItem = Replace(strItem, " ", " ")
            Return strItem
        End Function


    End Class

End Namespace


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。