How to export word using PHP

墨辰丷
Release: 2023-03-29 20:22:01
Original
2351 people have browsed it

This article mainly introduces the method of exporting word with PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

PHP export word

(1) First, preview the html page, instantiate the object, and define the data to be exported
(2) Click the download page to Pass the id value (any value is acceptable, only used for judgment). If the id has a value, output the buffer file and save it in word format.
(3) After clicking download, (if it is a picture, use the absolute path when saving it as word, so that it can be displayed normally in the saved word)
(4) Turn off cache output

Word_con.php Preview the html file to be exported

<?php
if(@$_GET[id]!=&#39;&#39;)
{
 include(&#39;word_fun.php&#39;);
 $word=new word();//示例化对象
 $word->start();//定义要保存数据的开始
}
include(&#39;word_show.php&#39;);
 if(@$_GET[id]!=&#39;&#39;)
 {
   $word->save(&#39;word_c.doc&#39;);//定义要保存数据的结束,同时把数据保存到word中
 }
 if(@$_GET[id]==&#39;&#39;)
 {
 //超链接中的x仅仅是为了传一个值,确认下载,没有其他的实际yi
 ?>
 <a href="#"><p onclick="window.location.href=&#39;word_con.php?id=x&#39;">点击跳到下载页面</p></a>
 <?php
 }else{
 echo "<a href=\"word_c.doc\">下载</a>";
 }
?>
Copy after login

Word_fun.php Export word related functions

<?php
class word
{
function start() //定义要保存数据的开始
{
    ob_start(); //开始输出缓冲
    //设置生成word的格式
    print &#39;<html xmlns="urn:schemas-microsoft-comfficeffice"
    xmlns:w="urn:schemas-microsoft-comffice:word"
    xmlns="http://www.w3.org/TR/REC-html40">&#39;;
}
function save($path) //定义要保存数据的结束,同时把数据保存到word中  
//所要保存的数据必须限定在该类的start()和save()之间
{
print "</html>";
$data=ob_get_contents(); //返回内部缓冲的内容 即把输出变成字符串
ob_end_clean(); //结束输出缓冲,清洁(擦除)输出缓冲区并关闭输出缓冲
$this->wirtetoword($path,$data);
}
function wirtetoword($fn,$data) //将数据已二进制的形式保存到word中
{
$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}
}
?>
Copy after login

Word_show.php Connect to the database and query related data

<?php 
 include(&#39;conn.php&#39;); //连接数据库
 $sq="select zf_content from zf where `zf_id`=137";
 $sql=mysql_query($sq);
 while(($que=mysql_fetch_array($sql))!=false)
 {
  echo "<font color=\"red\">hahaahahha</font>";
  echo $que[&#39;zf_content&#39;];
 }
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

How to use PHP to achieve regular crawling of URLs in pages

Identity card verification code calculation method based on PHP

Comparison of running time of four basic sorting algorithms implemented in PHP (must read)

The above is the detailed content of How to export word using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template