---
title: "PHP 写入资料到EXCEL中"
author: "javaeyeren"
date: "2009-02-02T01:43:00Z"
tags: []
slug: "php-excel"
draft: false
lastmod: "2009-02-02T01:43:00Z"
id: 601349
url: "https://javaeyeren.iteye.com/blog/601349"
---

&lt;?php  
header("Content-type:application/vnd.ms-excel");  
header("Content-Disposition:filename=test.xls");  
echo "test1\\t";  
echo "test2\\t\\n";  
echo "test1\\t";  
echo "test2\\t\\n";  
echo "test1\\t";  
echo "test2\\t\\n";  
echo "test1\\t";  
echo "test2\\t\\n";  
echo "test1\\t";  
echo "test2\\t\\n";  
echo "test1\\t";  
echo "test2\\t\\n";

?&gt;

多种格式的写入

&lt;?php

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

header("Content-Disposition:filename=order.xls");  
//下面就可以处理一般的PHP文件一样处理，可以连接数据库，显示图象等操作，最后会显示在EXCEL文件中

?&gt;  
　　在php环境运行上面的代码，大家就可以看到浏览器询问用户是否下载excel文档，点击保存，硬盘上就多了一个excel的文件，使用excel打开就会看到最终的结果，怎幺样不错吧。  
　　其实在做真正的应用的时候，大家可以将资料从数据库中取出，然后按照每一列资料结束后加\\t,每一行资料结束后加\\n的方法echo出来，在php的开头用header("Content-type:application/vnd.ms-excel");表示输出的是excel文件，用header("Content-Disposition:filename=test.xls");表示输出的文件名为text.xls。这样就ok了。