ASP.NET ,EXT.NET都适用的Excel导出,主要使用List集合转化为Excel输出

1             函数的定义

Public Sub DataBindTitleExcel(pPage As Page, ByVal objList As List(Of prjECWSDB.MODEL.clsWeb_vwPublicPlatformBySection), ExcelTitle As String, strUserMsg As String, Header As String)
        Dim response As HttpResponse = pPage.Response
        Dim Count As Integer
        Dim strHeader() As String
        Dim strHeadering() As String
        Dim strValue() As String
        Dim value As String
        Dim objListHeader As New List(Of prjDotNetPublicClass.clsExtNetModule.clsNameAndProperty)
        If objList.Count = 1 Then
            Exit Sub
        End If
        'objListHeader = prjDotNetPublicClass.clsExtNetModule.getListOfProperty(objList(0))
        strHeader = Split(Header, ",")
        'Count = objListHeader.Count
        Count = strHeader.Count
        If objList.Count = 0 Then
            response.Write("<script>alert('对不起,没有可用于导出的数据!')</script>")
            response.[End]()
        End If
        response.ContentEncoding = Encoding.GetEncoding("GB2312")
        response.ContentType = "application/ms-excel"
        response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls")
        Dim builder As New StringBuilder()
        builder.Append("<html><head>" & vbLf)
        builder.Append("<meta http-equiv=""Content-Language"" content=""zh-cn"">" & vbLf)
        builder.Append("<meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312"">" & vbLf)
        builder.Append("</head>" & vbLf)
        builder.Append("<table border=1>")
        builder.Append("<tr>" & vbLf)
        For i As Integer = 0 To Count - 1
            strHeadering = Split(strHeader(i), "=")
            builder.Append("<td bgcolor=#CCFFCC><b>" & strHeadering(1) & "</b></td>" & vbLf)
        Next

        For i As Integer = 0 To objList.Count - 1
            builder.Append("<tr>")
            For j As Integer = 0 To Count - 1
                strValue = Split(strHeader(j), "=")
                value = strValue(0)

                'builder.Append("<td style='vnd.ms-excel.numberformat:@'>" & getObjectValueByName(objList.Item(i), value) & "</td>")
                builder.Append("<td >" & getObjectValueByName(objList.Item(i), value) & "</td>")
            Next
            builder.Append("</tr>" & vbLf)
        Next
        builder.Append("</table>" & vbLf)
        response.Write(builder.ToString())
        response.[End]()
    End Sub



2 引用到的函数  getObjectValueByName  定义  


Public Function getObjectValueByName(ByRef obj As Object, ByVal strPropName As String) As Object
        Dim objType As Type = obj.GetType
        Dim pInfo As System.Reflection.PropertyInfo = objType.GetProperty(strPropName.Trim)
        Return pInfo.GetValue(obj, Nothing)
    End Function


3  函数的调用

 Private Sub btnTest_DirectClick(sender As Object, e As DirectEventArgs) Handles btnTest.DirectClick
        LoadWebPlatform()
        DataBindTitleExcel(Page, mobjLstOfvwWebPlatform, "公共平台信息导出", getUser.UID, strAliasList)
    End Sub


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值