摘要:利用xml匯出固定格式word
適用於固定格式的報表功能。
step1:首先建一個word的xml範本
指定您的tag=>"$$test"
step2:測試輸出頁。
Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim aryRep As New ArrayList
Dim aryStr As New ArrayList
aryRep.Add("$$test") '您指定的tag
aryStr.Add("<![CDATA[測試測試]]>") 'tag裡面的值 <![CDATA[xxxx]]> 是xml字串輸出
print(aryRep, aryVal, "test")
Catch ex As Exception
End Try
End Sub
Protected Sub print(ByRef aryRep As ArrayList, ByRef arystr As ArrayList, ByVal savefilename As String)
Response.AppendHeader("Content-Disposition", "attachment;filename=" & savefilename & ".doc")
Response.ContentType = "Application/MsWord"
Response.Charset = "big-5"
Dim headstr As String = String.Empty
Dim sr As IO.StreamReader = New IO.StreamReader(Server.MapPath("~/xml/test.xml")) '您範本的路徑
headstr = sr.ReadToEnd()
sr.Close()
For i As Integer = 0 To aryRep.Count - 1 Step i + 1
If i < arystr.Count Then
headstr = headstr.Replace(aryRep(i), arystr(i))
Else
headstr = headstr.Replace(aryRep(i), "")
End If
Next
Response.Write(headstr)
Response.End()
End Sub
End Class
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim aryRep As New ArrayList
Dim aryStr As New ArrayList
aryRep.Add("$$test") '您指定的tag
aryStr.Add("<![CDATA[測試測試]]>") 'tag裡面的值 <![CDATA[xxxx]]> 是xml字串輸出
print(aryRep, aryVal, "test")
Catch ex As Exception
End Try
End Sub
Protected Sub print(ByRef aryRep As ArrayList, ByRef arystr As ArrayList, ByVal savefilename As String)
Response.AppendHeader("Content-Disposition", "attachment;filename=" & savefilename & ".doc")
Response.ContentType = "Application/MsWord"
Response.Charset = "big-5"
Dim headstr As String = String.Empty
Dim sr As IO.StreamReader = New IO.StreamReader(Server.MapPath("~/xml/test.xml")) '您範本的路徑
headstr = sr.ReadToEnd()
sr.Close()
For i As Integer = 0 To aryRep.Count - 1 Step i + 1
If i < arystr.Count Then
headstr = headstr.Replace(aryRep(i), arystr(i))
Else
headstr = headstr.Replace(aryRep(i), "")
End If
Next
Response.Write(headstr)
Response.End()
End Sub
End Class
也許大家在報表格式會更複雜些,可自行再修改。
小弟在此也提供一個不錯的xml編輯器,以利編修xml時的可看性。
http://sourceforge.net/project/showfiles.php?group_id=141776&package_id=155724