从 .net Web 应用程序创建文档文件

发布于 2024-11-13 12:01:16 字数 961 浏览 0 评论 0原文

我有一个 vb.net Web 应用程序,我需要为我的用户提供下载 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

...但我不知道如何使文本加粗,或创建一个表格。我确信有更好的方法。

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>".ToString())

        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-11-20 12:01:16

很抱歉回答我自己的问题,但我刚刚遇到这个: 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文