我有一个vb.net网络应用程序,我需要为我的用户提供下载ms-word .doc文件的工具。此文件需要动态创建,并且应包含一些粗体文本和表格。
我遇到过这段代码,它构建了一个.doc文件,并允许你下载它:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String = "GenerateDocument" + ".doc"
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName)
Dim strHTMLContent As StringBuilder = New StringBuilder()
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/msword"
strHTMLContent.Append("<p align='Center'>MY CONTENT GOES HERE</p>".ToString())
HttpContext.Current.Response.Write(strHTMLContent)
HttpContext.Current.Response.End()
HttpContext.Current.Response.Flush()
End Sub
...但我不知道如何使文字变粗,或者创建一个表格。我确信有更好的方法。
答案 0 :(得分:0)
很抱歉回答我自己的问题,但我刚刚遇到过这个问题:http://www.codeproject.com/KB/office/Wordyna.aspx
它对我来说很完美,因为它允许HTML输入。所以表格很轻松。
Public Sub Page_Load(sender as Object, e as EventArgs)
'build the content for the dynamic Word document
'in HTML alongwith some Office specific style properties.
Dim strBody As New System.Text.StringBuilder("")
strBody.Append("<html " & _
"xmlns:o='urn:schemas-microsoft-com:office:office' " & _
"xmlns:w='urn:schemas-microsoft-com:office:word'" & _
"xmlns='http://www.w3.org/TR/REC-html40'>" & _
"<head><title>Time</title>")
'The setting specifies document's view after it is downloaded as Print
'instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" & _
"<xml>" & _
"<w:WordDocument>" & _
"<w:View>Print</w:View>" & _
"<w:Zoom>90</w:Zoom>" & _
"<w:DoNotOptimizeForBrowser/>" & _
"</w:WordDocument>" & _
"</xml>" & _
"<![endif]-->")
strBody.Append("<style>" & _
"<!-- /* Style Definitions */" & _
"@page Section1" & _
" {size:8.5in 11.0in; " & _
" margin:1.0in 1.25in 1.0in 1.25in ; " & _
" mso-header-margin:.5in; " & _
" mso-footer-margin:.5in; mso-paper-source:0;}" & _
" div.Section1" & _
" {page:Section1;}" & _
"-->" & _
"</style></head>")
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" & _
"<div class=Section1>" & _
"<h1>Time and tide wait for none</h1>" & _
"<p style='color:red'><I>" & _
DateTime.Now & "</I></p>" & _
"</div>" & _
"<div>" & _
"<table border=1>" & _
"<tr>" & _
"<td>1</td>" & _
"<td>2</td>" & _
"<td>3</td>" & _
"</tr>" & _
"<tr>" & _
"<td>a</td>" & _
"<td>b</td>" & _
"<td>c</td>" & _
"</tr>" & _
"</table>" & _
"</div>" & _
"</body></html>")
'Force this content to be downloaded
'as a Word document with the name of your choice
Response.AppendHeader("Content-Type", "application/msword")
Response.AppendHeader ("Content-disposition", _
"attachment; filename=myword.doc")
Response.Write(strBody)
End Sub