免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2867 | 回复: 0
打印 上一主题 下一主题

将PHP数据导出到excel文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-12 13:40 |只看该作者 |倒序浏览
将数据导出excel
  1. <?php
  2. /**
  3. * @author Jceee
  4. */
  5. class excel_tool
  6. {
  7.     public static $readerObj;
  8.     public static $charset = 'utf-8';
  9.   
  10.     /**
  11.      * 输出切换编码
  12.      */
  13.     public static function excelExportIconv($output){

  14.         return iconv(self::$charset, 'GBK', $output);
  15.     }

  16.     /**
  17.      * 导出文件
  18.      * @param $fileName  string  
  19.      * @param $title     string
  20.      * @param $firstRow  array      
  21.      *          如:array('name'=>'名字', 'title' => '标题') 键名与后面的数组$data的子元素键名关联
  22.      * @param $data      array
  23.      */
  24.     public static function exportFile($fileName, $title = '', $firstRow = array(), $data = array())
  25.     {
  26.         header('Content-Type: application/vnd.ms-execl');
  27.         header('Content-Disposition: attachment; filename=' . $fileName . '.xls');
  28.         header('Pragma: no-cache');
  29.         header('Expires: 0');

  30.         if (!empty($title)) {
  31.             echo self::excelExportIconv($title) . "\t\n";
  32.         }

  33.         /**
  34.          *  第一行与后面的数据以键名关联
  35.          */
  36.         if (!empty($firstRow) && is_array($firstRow)) {

  37.             //输出第一行内容
  38.             foreach ($firstRow as $first) {
  39.                 echo self::excelExportIconv($first) . "\t";
  40.             }
  41.             echo "\n";

  42.             if (!empty($data) && is_array($data)) {
  43.                 foreach ($data as $item) {
  44.                     foreach ($firstRow as $_key => $_val) {
  45.                         if (isset($item[$_key])) {
  46.                             echo self::excelExportIconv($item[$_key]) . "\t";
  47.                         } else {
  48.                             echo self::excelExportIconv('') . "\t";
  49.                         }
  50.                     }
  51.                     echo "\n";
  52.                 }
  53.             }
  54.         } else {

  55.             if (!empty($data) && is_array($data)) {
  56.                 foreach ($data as $item) {
  57.                     foreach ($item as $val) {
  58.                         echo self::excelExportIconv($val) . "\t";
  59.                     }
  60.                     echo "\n";
  61.                 }
  62.                 echo "\n";
  63.             }
  64.         }

  65.     }
  66. }


  67. /**
  68. * example:
  69. */
  70. $fileName = 'example';
  71. $title = 'This is title';
  72. $firstRow = array(
  73.   'id' => 'ID',
  74.   'name' => '名字',
  75.   'title' => '标题'
  76.   );
  77. $data = array(
  78.   array('id' => 1, 'name' => '名字1', 'title' => '标题1'),
  79.   array('id' => 2, 'name' => '名字2', 'title' => '标题2'),
  80.   array('id' => 3, 'name' => '名字3', 'title' => '标题3'),
  81.   array('id' => 4, 'name' => '名字4', 'title' => '标题4'),
  82.   );
  83. Excel_tool::exportFile($fileName,$title,$firstRow,$data);
  84. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP