如何通过php将数据库里值放到word模版中?

雪月天霜 2014-01-24 01:12:35
比如说我有个现有的word模版test.doc
里面的内容是今天是 天气。
然后我把从数据库里的值:晴天。输到这个test.doc的下划线位置上。。
这样可以实现吗?
如果可以?该怎么做呢?
请各位老师指点。







------------
勤学+苦练。
...全文
196 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
鱼为情 2014-01-24
  • 打赏
  • 举报
回复
真不会,留个记号,解决了回复一下啊。加这个群:153436140
码无边 2014-01-24
  • 打赏
  • 举报
回复
你太天真,除非你把word文档保存一份,整合数据库变量,再读取出来。
雪月天霜 2014-01-24
  • 打赏
  • 举报
回复
上述两位老师给出的方法,都不能保存现有的模版,运行php后马上就会刷新word文档本身,那么手动做的保存就会消失。。 比如把:今天是 天气。在word中把模版内容改成:昨天是 天。现在需要在数据库中取出相应数据填上。这样的话,上面老位老师给出的办法就行不通了,最好有办法在word中的这个下划线上设置个变量,然后把数据库的值传到这个变量上。。。 ------- ps:难道真的是我天真了吗。
tottyandbaty 2014-01-24
  • 打赏
  • 举报
回复
http://phpword.codeplex.com/
tottyandbaty 2014-01-24
  • 打赏
  • 举报
回复


/*****************************************************************
This approach uses detection of NUL (chr(00)) and end line (chr(13))
to decide where the text is:
- divide the file contents up by chr(13)
- reject any slices containing a NUL
- stitch the rest together again
- clean up with a regular expression
*****************************************************************/

function parseWord($userDoc) 
{
    $fileHandle = fopen($userDoc, "r");
    $word_text = @fread($fileHandle, filesize($userDoc));
    $line = "";
    $tam = filesize($userDoc);
    $nulos = 0;
    $caracteres = 0;
    for($i=1536; $i<$tam; $i++)
    {
        $line .= $word_text[$i];

        if( $word_text[$i] == 0)
        {
            $nulos++;
        }
        else
        {
            $nulos=0;
            $caracteres++;
        }

        if( $nulos>1996)
        {   
            break;  
        }
    }

    //echo $caracteres;

    $lines = explode(chr(0x0D),$line);
    //$outtext = "<pre>";

    $outtext = "";
    foreach($lines as $thisline)
    {
        $tam = strlen($thisline);
        if( !$tam )
        {
            continue;
        }

        $new_line = ""; 
        for($i=0; $i<$tam; $i++)
        {
            $onechar = $thisline[$i];
            if( $onechar > chr(240) )
            {
                continue;
            }

            if( $onechar >= chr(0x20) )
            {
                $caracteres++;
                $new_line .= $onechar;
            }

            if( $onechar == chr(0x14) )
            {
                $new_line .= "</a>";
            }

            if( $onechar == chr(0x07) )
            {
                $new_line .= "\t";
                if( isset($thisline[$i+1]) )
                {
                    if( $thisline[$i+1] == chr(0x07) )
                    {
                        $new_line .= "\n";
                    }
                }
            }
        }
        //troca por hiperlink
        $new_line = str_replace("HYPERLINK" ,"<a href=",$new_line); 
        $new_line = str_replace("\o" ,">",$new_line); 
        $new_line .= "\n";

        //link de imagens
        $new_line = str_replace("INCLUDEPICTURE" ,"<br><img src=",$new_line); 
        $new_line = str_replace("\*" ,"><br>",$new_line); 
        $new_line = str_replace("MERGEFORMATINET" ,"",$new_line); 


        $outtext .= nl2br($new_line);
    }

 return $outtext;
} 

$userDoc = "custo.doc";
$userDoc = "Cultura.doc";
$text = parseWord($userDoc);

echo $text;

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧