开发者

Creating a doc file from .net web app

I have a vb.net web app, and I need to give my users the facility to download a ms-word .doc file. This file needs to be created dynamically, and should contain some bold text and a table.

I've come across this code, which builds a .doc file, and lets you download it:

  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>".ToStri开发者_如何转开发ng())

        HttpContext.Current.Response.Write(strHTMLContent)
        HttpContext.Current.Response.End()
        HttpContext.Current.Response.Flush()
  End Sub

...but I don't know how to make the text bold, or create a table. I'm sure there's a better way.


Sorry to answer my own question, but I've just come across this: http://www.codeproject.com/KB/office/Wordyna.aspx

It works perfectly for me, because it allows HTML input. So tables are a doddle.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜