search
Homephp教程PHP源码php导出生成excel表格几种方法介绍

利用php导出excel我们大多会直接生成 csv文件,这种方便快捷如果不是要求很高,完全可以利用csv 来实例了,

<script>ec(2);</script>

这是最简单的了

 代码如下 复制代码

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");

$tx=’表头’;  
echo   $tx."nn";  
//输出内容如下:  
echo   "姓名"."t";  
echo   "年龄"."t";  
echo   "学历"."t";  
echo   "n";  
echo   "张三"."t";  
echo   "25"."t";  
echo   "本科"."t";  
?>

如果你一定要输入xls标准的excel文件可参考下面方法

/**
 * 输出XLS的头信息
 * 注:使用此函数前后都不应有任何数据输出
 * @param $data Array 下载的数据 
 * @param $file_name String 下载的文件名
 */

 代码如下 复制代码

function outputXlsHeader($data,$file_name = 'export')
{
 header('Content-Type: text/xls');
 header ( "Content-type:application/vnd.ms-excel;charset=utf-8" );
 $str = mb_convert_encoding($file_name, 'gbk', 'utf-8');   
 header('Content-Disposition: attachment;filename="' .$str . '.xls"');     
 header('Cache-Control:must-revalidate,post-check=0,pre-check=0');       
 header('Expires:0');        
 header('Pragma:public');
 
 $table_data = '

'; 

 foreach ($data as $line)        
 {
  $table_data .= '

';
  foreach ($line as $key => &$item)
  {
   $item = mb_convert_encoding($item, 'gbk', 'utf-8');
   $table_data .= '';
  }
  $table_data .= '';
 }
 $table_data .='
' . $item . '
';
 echo $table_data;   
 die();
}

 

下面还推荐一下第三方的做法

引用google code中推荐的小类库(大体同方法一,比较复杂点)

http://code.google.com/p/php-excel/downloads/list
 

PHPEXCEL 类库,功能强大,支持win Excel2003 ,Win Excel2007

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment