• PHP中导出Excel,将数据以Excel形式导出


    现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下:

     1 <?php 
     2     date_default_timezone_set('PRC');//设置时区
     3     
     4     /*设置head头信息*/
     5     Header("Content-Type:application/vnd.ms-excel;charset=UTF-8");
     6     Header("Accept-Ranges:bytes");
     7     Header("Content-Disposition:attachment;filename=".date('YmdHis').".xls");
     8     Header("Pragma:no-cache");
     9     Header("Expires:0");
    10     
    11     $str = $str2 = null;
    12     /*设置表格信息*/
    13     $str.= "ID"."	";
    14     $str.= "用户名"."	";
    15     $str.= "密码"."	";
    16     $str.= "邮箱"."	
    ";
    17     $info = iconv("UTF-8","GBK",$str);
    18     echo $info;
    19     
    20     /*查询内容导出数据库数据*/
    21     $link = @mysql_connect('localhost','root','') or die('Failed to connect to the MySQL'); 
    22     mysql_select_db('l_test',$link);
    23     mysql_set_charset('utf8',$link);
    24     $query = 'select * from lt_user';
    25     $result = mysql_query($query);
    26     while($row = mysql_fetch_assoc($result)){
    27         $list[] = $row; 
    28     }
    29     mysql_free_result($result);
    30     mysql_close($link);
    31     
    32     foreach($list as $v){
    33         $str2.= trim(iconv("UTF-8","GBK",$v['id']))."	";
    34         $str2.= trim(iconv("UTF-8","GBK",$v['username']))."	";
    35         $str2.= trim(iconv("UTF-8","GBK",$v['passwd']))."	";
    36         $str2.= trim(iconv("UTF-8","GBK",$v['email']))."	
    ";
    37     }
    38     echo $str2;
    39 ?> 

    这个方法相对简单,但在一些小的程序上,作为一个简单的方法还是不错的,希望能对大家有所帮助。

  • 相关阅读:
    删库了一定要跑路吗?爱情 36 技之记忆重生!
    程序员和他的朋友们!
    聊起 BigTable,让你不再胆怯
    [Ant Design] Warning: Instance created by `useForm` is not connected to any Form element. Forget to pass `form` prop?
    Git 常用命令
    Git 名词解释
    js中void 0和undefined的区别
    js运算符优先级
    搭建React项目(低配版)
    mac常用shell指令笔记
  • 原文地址:https://www.cnblogs.com/blogforly/p/5910266.html
Copyright © 2020-2023  润新知