最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Converting text from HTML to MS Word - Stack Overflow

programmeradmin2浏览0评论

I'm trying to convert HTML (From CK Editor) to MS Word:

wordDoc = new ActiveXObject("Word.Application");

But the result shows all HTML tags (like span, strong) in the Word document.

How can I solve this problem?

I'm trying to convert HTML (From CK Editor) to MS Word:

wordDoc = new ActiveXObject("Word.Application");

But the result shows all HTML tags (like span, strong) in the Word document.

How can I solve this problem?

Share Improve this question edited May 17, 2013 at 3:49 Danny Beckett 20.9k26 gold badges113 silver badges142 bronze badges asked Feb 5, 2013 at 11:26 Apurba MondalApurba Mondal 631 silver badge7 bronze badges 5
  • Can you post the full code? – Danny Beckett Commented May 17, 2013 at 4:37
  • stackoverflow./questions/12895795/… – Rohit Kumar Commented May 17, 2013 at 8:41
  • if you can make well formed XHTML out of the CK Editor output, and are fortable using Java, then docx4j may help. – JasonPlutext Commented May 21, 2013 at 12:55
  • fun fact: if you rename a file named test.html to test.doc, and it's a plete HTML document, double-clicking the file will open it in word as expected, formatting, WYSIWYG and all. – dandavis Commented May 21, 2013 at 19:57
  • It matters what version of MS Word you are targeting. @dandavis is correct if your users are on > 2007. – Boggin Commented May 22, 2013 at 10:03
Add a ment  | 

5 Answers 5

Reset to default 0

There are some plete solutions for your problem. Try these ones:

  • http://phpexcel.codeplex./
  • http://www.phpdocx./
  • http://phpword.codeplex./
  • http://www.phplivedocx/articles/brief-introduction-to-phplivedocx/

While you're looking for a WORD solution, could a PDF solution work as well? Try http://www.fpdf if so. But again this doesn't work if you absolutely must have WORD as the end result. Unless you need the user to type on the document, PDFs are generally better as they prevent a user from modifying.

As per your question i understand that you want HTML to Word With All Style(Css Apply)

i have such requirenmnet i had implement below code it's perfactly work for me. please check below code may be it's help full to you

here i am create Word File from GridView and i had apply Style to that grid.it's create same Grid in Word file as it display in browser.you need to only change style (css) related tag for your work.

Protected Sub CreateHTMlToWord()
        Try

            Dim sHtml As New StringBuilder
            Dim htmlForm As New HtmlForm
            grdHTMLData.GridLines = GridLines.Both 'Grid View Fill with data

            Response.ClearContent()
            Response.AddHeader("content-disposition", "attachment; filename=estPage.doc")
            Response.ContentType = "application/vnd.doc"

            Dim str As New IO.StringWriter
            Dim htex As New HtmlTextWriter(str)
            htmlForm.Controls.Add(grdHTMLData)
            htmlForm.EnableViewState = False
            Me.Controls.Add(htmlForm)
            htmlForm.RenderControl(htex)

            sHtml.Append("<html>")
            sHtml.Append("<head>")
            sHtml.Append("<style type='text/css'>")
            sHtml.Append(".mGrid{width: 100%; background-color: #F8FCFE; margin: 5px 0 10px 0; border-collapse: collapse;}")
            sHtml.Append(".mGrid td{ padding: 2px; color: black;} .mGrid td a:link{ color: #000;}")
            sHtml.Append(".mGrid th{ padding: 4px 2px; color: #fff; background: #4A7298; font-size: 0.9em;}")
            sHtml.Append(".mGrid th a:link{ color: #fff; font-weight: bold;} .mGrid .alt{ background-color: #E1EEF7;}")
            sHtml.Append(".mGrid .pgr{ background: #E1EEF7;} .mGrid .pgr table{ margin: 5px 0;}")
            sHtml.Append(".mGrid .pgr td span{ border-width: 0; font-weight: bold; color: #666; line-height: 12px;}")
            sHtml.Append(".mGrid .pgr td a{ color: #666;} .mGrid .pgr td a:link{ color: #4A7298; padding: 0 5px; text-decoration: underline;}")
            sHtml.Append(".mGrid .pgr td a:active{ text-decoration: none; font-weight: bold;}")
            sHtml.Append(".mGrid .pgr td a:hover{ color: #fff; background-color: #4A7298; text-decoration: none;} .mGrid a:hover{ text-decoration: underline;}")
            sHtml.Append(".mGridHdr th{ text-align: left;}")
            sHtml.Append("</style>")
            sHtml.Append("</head>")
            sHtml.Append("<body>")
            sHtml.Append(str.ToString)
            sHtml.Append("</body>")
            sHtml.Append("</html>")

            Response.Write(sHtml.ToString)
            Response.Flush()
            Response.Close()

        Catch ex As Exception

        End Try
    End Sub

Pandoc allows conversion between html and word documents (among many other formats). Pandoc is an haskel library but there are bundled installer for all platform. To convert your document you will need to use this mand.

pandoc -o file.doc file.html

What I could imagine is like following steps:

  1. Translates your HTML into OpenXML,
  2. then create the files corresponding to the structure of the word document,
  3. Zip into docx.
发布评论

评论列表(0)

  1. 暂无评论