2011-10-31 48 views
0

我有一個像波紋管如何從HTML製作.doc/.docx文件(字符串)?

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><head><style><!-- 

        /*paged media */ div.header {display: none } 
        div.footer {display: none } /*@media print { */ 


        @page { size: A4; margin: 10%; @top-center { 
        content: element(header) } @bottom-center { 
        content: element(footer) } } 


        /*font definitions*/ 

        /*element styles*/ .del 
        {text-decoration:line-through;color:red;} 

          .ins {text-decoration:none;background:#c0ffc0;padding:1px;} 



        /* Word style definitions */ 

/* TABLE STYLES */ 

/* PARAGRAPH STYLES */ 
.DocDefaults {display:block;space-after: 4mm;line-height: 115%;font-family: Calibri;font-size: 11.0pt;} 
.Normal {display:block;} 

/* CHARACTER STYLES */ .DefaultParagraphFont {display:inline;} 


        /* TABLE CELL STYLES */ 
        --></style><script type="text/javascript"> 

       function toggleDiv(divid){ 
        if(document.getElementById(divid).style.display == 'none'){ 
         document.getElementById(divid).style.display = 'block'; 
        }else{ 
         document.getElementById(divid).style.display = 'none'; 
        } 
       } 

      </script></head><body> 

    <!-- userBodyTop goes here --> 



    <div class="document"> 

    <p class="Normal DocDefaults "><span style="font-weight: bold;">Hi</span><span style="white-space:pre-wrap;"> </span>[name]<span style="white-space:pre-wrap;">. </span><span style="font-weight: bold;color: #FF0000;">your</span><span style="white-space:pre-wrap;"> ac is</span><span style="white-space:pre-wrap;"> </span>[acc_no]<span style="white-space:pre-wrap;">, and </span><span style="font-weight: bold;color: #FF0000;">your</span><span style="white-space:pre-wrap;"> password </span><span style="white-space:pre-wrap;">is </span>[password].</p> 

    <p class="Normal DocDefaults ">Thanks you.</p></div> 







    <!-- userBodyTail goes here --> 

    </body></html> 

我想要使用此字符串.doc或.docx文件以HTML格式srting。 doc或docx應該保存在目錄和數據庫中的鏈接下載。要做什麼?

+0

你嘗試過什麼嗎?你有沒有見過http://www.phpdocx.com/? – Chris

+0

phpdocx需要服務器中的pdf和zip模塊。但我無法安裝這些模塊。所以我正在爲核心PHP代碼進行接口。上面的字符串可以導出爲doc並且用戶可以保存它。但我的要求是將其保存在服務器目錄和目錄鏈接到數據庫以供以後下載 – user989184

+0

當我在瀏覽器中顯示HTML(文檔保存爲網頁)時,文檔格式正常顯示。當我將它導出到doc時,樣式顯示不正確。有什麼問題? – user989184

回答

0

我是一名iOs開發者,但我能夠將html轉換爲.docx。 請更換&添加以下代碼到你的HTML文件&請改變文件擴展名.dcox /,DOC

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\"    xmlns=\"http://www.w3.org/TR/REC-html40\"><body>" 

希望這會爲你工作。

+0

您可以讓網頁顯示.docx文件。這是你正在嘗試做什麼。 –

0
/* HTML to Microsoft Word Export 
* This code demonstrates how to export an html element to Microsoft Word 
* with CSS styles to set page orientation and paper size. 
* Tested with Word 2010, 2013 and FireFox, Chrome, Opera, IE10-11 
* Fails in legacy browsers (IE<10) that lack window.Blob object 
*/ 
function saveDoc() { 

    if (!window.Blob) { 
    alert('Your legacy browser does not support this action.'); 
    return; 
    } 

    var html, link, blob, url, css; 

    // EU A4 use: size: 841.95pt 595.35pt; 
    // US Letter use: size:11.0in 8.5in; 

    css = ('\ 
    <style>\ 
    @page WordSection1{size: 841.95pt 595.35pt;mso-page-orientation: portrait;}\ 
    div.WordSection1 {page: WordSection1;}\ 
    h1 {font-family: "Times New Roman", Georgia, Serif; font-size: 16pt;}\ 
    p {font-family: "Times New Roman", Georgia, Serif; font-size: 14pt;}\ 
    </style>\ 
    '); 

    var rightAligned = document.getElementsByClassName("sm-align-right"); 
    for (var i=0, max=rightAligned.length; i < max; i++) { 
    rightAligned[i].style = "text-align: right;" 
    } 

    var centerAligned = document.getElementsByClassName("sm-align-center"); 
    for (var i=0, max=centerAligned.length; i < max; i++) { 
    centerAligned[i].style = "text-align: center;" 
    } 

    html = document.getElementById('text').innerHTML; 
    html = '\ 
    <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>Document Title</title>\ 
    <xml>\ 
     <w:worddocument xmlns:w="#unknown">\ 
     <w:view>Print</w:view>\ 
     <w:zoom>90</w:zoom>\ 
     <w:donotoptimizeforbrowser />\ 
     </w:worddocument>\ 
    </xml>\ 
    </head>\ 
    <body lang=RU-ru style="tab-interval:.5in">\ 
    <div class="Section1">' + html + '</div>\ 
    </body>\ 
    </html>' 

    blob = new Blob(['\ufeff', css + html], { 
    type: 'application/msword' 
    }); 

    url = URL.createObjectURL(blob); 
    link = document.createElement('A'); 
    link.href = url; 

    filename = 'filename'; 

    // Set default file name. 
    // Word will append file extension - do not add an extension here. 
    link.download = filename; 

    document.body.appendChild(link); 

    if (navigator.msSaveOrOpenBlob) { 
    navigator.msSaveOrOpenBlob(blob, filename + '.doc'); // IE10-11 
    } else { 
    link.click(); // other browsers 
    } 

    document.body.removeChild(link); 
};