Home > Backend Development > PHP Tutorial > A simple way to output excel in php (csv format)_PHP tutorial

A simple way to output excel in php (csv format)_PHP tutorial

WBOY
Release: 2016-07-13 10:49:43
Original
1100 people have browsed it

There is a professional excel processing class in php. We can use it to add, delete or process related data in excel. If we just want to generate excel, we can use the following method simply and quickly.

If you don’t want to read the data, we can just use the following code. You only need to export the relevant data to the excel table. Such a simple operation does not require the use of those class libraries. Just use the header method directly: header("Content-type:application/vnd.ms-excel");

The code is as follows
 代码如下 复制代码

header(“Content-type:application/vnd.ms-excel”);
header(‘Content-Disposition: attachment;filename=”yingcai.xls”‘);

?>










用户名 密码
admin yingcai

Copy code


代码如下 复制代码


header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment; filename=用户信息表.xls");
require_once('./class.db.php');
$db = new DB('localhost', 'app_vmovies', 'root', '123456');
$sql = 'select * from `user` order by `id` desc';
$ret = $db->get_results($sql);
 
// 输出内容如下:   
echo "ID"."t";
echo "昵称"."t";
echo "性别"."t";
echo "OpenId"."t";
echo "平台"."t";
echo "注册日期"."t";
echo "n";
 
$len = count($ret);
for ( $i = 0; $i < $len; $i++ ) {
echo $ret[$i]["id"]."t";
echo $ret[$i]["nickname"]."t";
echo $ret[$i]["gender"]."t";
echo $ret[$i]["openid"]."t";
echo $ret[$i]["pf"]."t";
echo $ret[$i]["regdate"]."t";
echo "n";
}

header(“Content-type:application/vnd.ms-excel”);

header(‘Content-Disposition: attachment;filename="yingcai.xls”‘);

?>

Username
 代码如下 复制代码

header("Content-Type:   application/msword");       
header("Content-Disposition:   attachment;   filename=doc.doc"); 

Password admin yingcai A simple and pure method of exporting Excel tables in PHP, without too many explanations and fancy decorations, you can understand it at a glance
The code is as follows Copy code
header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment; filename=user information table.xls"); require_once('./class.db.php'); $db = new DB('localhost', 'app_vmovies', 'root', '123456'); $sql = 'select * from `user` order by `id` desc'; $ret = $db->get_results($sql); //The output content is as follows: echo "ID"."t"; echo "nickname"."t"; echo "gender"."t"; echo "OpenId"."t"; echo "Platform"."t"; echo "Registration Date"."t"; echo "n"; $len = count($ret); for ( $i = 0; $i < $len; $i++ ) { echo $ret[$i]["id"]."t"; echo $ret[$i]["nickname"]."t"; echo $ret[$i]["gender"]."t"; echo $ret[$i]["openid"]."t"; echo $ret[$i]["pf"]."t"; echo $ret[$i]["regdate"]."t"; echo "n"; } If the program is encoded in UTF-8, you need to use the iconv function to transcode it, otherwise it will be garbled and garbled. The other word format import is similar, just specify the header:
The code is as follows Copy code
header("Content-Type: application/msword"); header("Content-Disposition: attachment; filename=doc.doc");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632698.htmlTechArticleThere is a professional excel processing class in php. We can use it to add, delete or process related data in excel. If we just want to generate excel, we can use the following method simply and quickly. Such as...
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