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