簡體   English   中英

html標記在Microsoft Word文檔中不起作用

[英]html tags are not working in microsoft word document

最近,我在使用“在php中創建Word文檔文件”時遇到問題。

在我的網站上,有一個表格。 其中包括10個字段,例如“姓名”,“電話”,“地址”等。

當用戶填寫這些字段並提交表單時,會向管理員發送一封電子郵件,其中包含該表單和相應的字段值,說明“先生xxx最近提交了此表單”。

我為此使用文件處理程序。 但是當我使用html標記使文件在word doc中看起來更好時,html在那里不起作用,而是將它作為文本打印在該doc文件中。

這是我的代碼--->

$fh = fopen(JPATH_BASE.'/files/form_'.$user->id.'_'.$form_id.'_'.$submit_id.'.doc',"w+");
        //print_r($post); exit;
        //ob_start();
        //$data = ob_get_clean();
        //$data = '';
        $data = $emailreceipttext;
        foreach($fieldsInArray as $fields)
        {
            foreach($post as $key=>$p)
            {

                if($key==$fields->name)
                {

                if(is_array($p))
                {
                    $p = implode(",",$p);
                }
                $data = str_replace("{".$fields->name."}", $p, $data);

                }
            }
        }
        //fwrite($fh, $data);

        //print_r($data);exit;
    //}


    /*print_r($data);
    exit;*/
    fwrite($fh, $data); 
    $file = 'form_'.$user->id.'_'.$form_id.'_'.$submit_id.'.doc';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/msword');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0,  pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
        }
        fclose($fh);
        //echo hi; print_r($data); exit;
    $this->send_mail($data,$submit_id);

$ data是由郵件正文組成的變量。 我正在使用str_replace存儲該表單的字段的值。

但是文檔顯示如下--->

<p><label>name</label>:<b>arnas sinha</b><br/>
<label>phone</label>:<b>9878790989</b><br/>
 <label>address</label>:<b>india</b><br/>

等等

如何使用HTML格式創建Word文檔文件?

您可以使用http標頭解決此問題:

<?php
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=document_name.doc");

    echo "<html>";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    echo "<body>";
    echo "<b>My first document</b>";
    echo "</body>";
    echo "</html>";
?>

上述標頭添加到您的文件中,即可解決問題。

另一種方法是通過COM對象。 但是以上一項就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM