How to export and generate word in php, _PHP tutorial
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
<?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 ?> <a href="#"><div onclick="window.location.href='word_con.php?id=x'">点击跳到下载页面</div></a> <?php }else{ echo "<a href=\"word_c.doc\">下载</a>"; } ?>
Word_fun.php exports word related functions
<?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); } } ?>
Word_show.php connects to the database and queries related data
<?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']; } ?>
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
