Home  >  Article  >  php教程  >  用php生成excel文件

用php生成excel文件

WBOY
WBOYOriginal
2016-06-06 19:55:581727browse

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 相信大家在PHP开发过程中经常遇到导出excel文件的功能,这里共享一段简单代码可实现此功能。 大家来看代码: ? header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  相信大家在PHP开发过程中经常遇到导出excel文件的功能,这里共享一段简单代码可实现此功能。

  大家来看代码:

  

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

  header("Content-Disposition:filename=test.xls");

  echo "test1    ";

  echo "test2    ";

  echo "test1    ";

  echo "test2    ";

  echo "test1    ";

  echo "test2    ";

  echo "test1    ";

  echo "test2    ";

  echo "test1    ";

  echo "test2    ";

  echo "test1    ";

  echo "test2    ";

  ?>

  在php环境运行上面的代码,大家就可以看到浏览器询问用户是否下载excel文档,点击保存,硬盘上就多了一个excel的文件,使用excel打开就会看到最终的结果,怎么样不错吧。

  其实在做真正的应用的时候,大家可以将数据从数据库中取出,然后按照每一列数据结束后加,每一行数据结束后加的方法echo出来,在php的开头用header("Content-type:application/vnd.ms-excel");表示输出的是excel文件,用header("Content-Disposition:filename=test.xls");表示输出的文件名为text.xls.

  这样就ok了。我们更可以修改header让他输出更多格式的文件,这样php在处理各种类型文件方面就更加方便了。

用php生成excel文件

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