Home  >  Article  >  Backend Development  >  How to export and generate word in php, _PHP tutorial

How to export and generate word in php, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:02:48764browse

How to export and generate word from php,

The example in this article describes the method of exporting and generating word from php. Share it with everyone for your reference, the details are as follows:

PHP export word

(1) First, preview the html page, instantiate the object, and define the data to be exported
(2) Click the download page and pass the value to the id (any value is acceptable, only 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 cached output

Word_con.php Preview the html file to be exported

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

Word_fun.php exports word related functions

<&#63;php
class word
{
function start() //定义要保存数据的开始
{
    ob_start(); //开始输出缓冲
    //设置生成word的格式
    print '<html xmlns="urn:schemas-microsoft-comfficeffice"
    xmlns:w="urn:schemas-microsoft-comffice:word"
    xmlns="http://www.w3.org/TR/REC-html40">';
}
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);
}
}
&#63;>

Word_show.php connects to the database and queries related data

<&#63;php 
 include('conn.php'); //连接数据库
 $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['zf_content'];
 }
&#63;>

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Simple sample code for php to export word documents and excel spreadsheets
  • Code examples for php to export word format data
  • Principles and examples of using PHP to export Word documents
  • Code to export web pages into Word documents in PHP
  • Tutorial on using PHP to generate Word documents under Windows systems
  • php Back up the database code (generate word, excel, json, xml, sql)
  • php generates a word document from the web page in the program and provides the downloaded code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084535.htmlTechArticleHow to export and generate word from php. This article describes the method of exporting and generating word from php. Share it with everyone for your reference, the details are as follows: PHP export word (1) First, preview the html page,...
Statement:
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