zoukankan      html  css  js  c++  java
  • PHP将数据导出Excel表中(投机型)

    1、简介

      如何利用最简单粗糙暴力的方法将数据写入Excel文件中呢?

      因为ms word和excel的文档都支持html文本格式,因此我们可以基于这个原理采用html文本格式进行数据的输出。

      在html中,我们只需要将数据照着所想要的顺序放进相应的html表格中即可。

      我们采用PHP进行数据获取整理以及构造相应的html文本,最后通过字节流输出下载到用户本地。

    2、代码

    直接上代码,这是一个很简单的程序,里面都带有注释了。

    ExportExcel.class.php文件

     1 <?php
     2 class ExportExcel{
     3     /**
     4     * @desc   将数据导出到Excel中
     5     * @param  $data array 设置表格数据 
     6     * @param  $titlename string 设置head 
     7     * @param  $title string 设置表头 
     8     * @param  $filename 设置默认文件名
     9     * @return 将字符串输出,即输出字节流,下载Excel文件
    10     */ 
    11     public function excelData($data,$titlename,$title,$filename){ 
    12         #xmlns即是xml的命名空间
    13         $str = "<html xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns="http://www.w3.org/TR/REC-html40">
    <head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    </head>
    <body>"; 
    14         #以下构建一个html类型格式的表格
    15         $str .= $title; 
    16         $str .="<table border=1><head>".$titlename."</head>"; 
    17         foreach ($data  as $key=> $rt ) 
    18         { 
    19             $str .= "<tr>"; 
    20             foreach ( $rt as $k => $v ) 
    21             { 
    22                 $str .= "<td>{$v}</td>"; 
    23             } 
    24             $str .= "</tr>
    "; 
    25         } 
    26         $str .= "</table></body></html>"; 
    27         header( "Content-Type: application/vnd.ms-excel; name='excel'" );   #类型
    28         header( "Content-type: application/octet-stream" );     #告诉浏览器响应的对象的类型(字节流、浏览器默认使用下载方式处理)
    29         header( "Content-Disposition: attachment; filename=".$filename );   #不打开此文件,刺激浏览器弹出下载窗口、下载文件默认命名
    30         header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); 
    31         header( "Pragma: no-cache" );   #保证不被缓存或者说保证获取的是最新的数据
    32         header( "Expires: 0" ); 
    33         exit( $str ); 
    34     }
    35 
    36 
    37 }
    38 
    39 
    40 ?>
     1 <?php
     2 $obj=new ExportExcel();
     3 
     4 $data = array(
     5     array('a11','a22','a33'),
     6     array('b11','b22','b33'),
     7     array('c11','c22','c33'),
     8     array('d11','d22','d33'),
     9     array('e11','e22','e33'),
    10     array('f11','f22','f33'),
    11     );      
    12 $excelHead = "这个是Excel表格标题"; 
    13 $title = "我的Excel表";   #文件命名
    14 $headtitle= "<tr><th  colspan='3' >{$excelHead}</th></tr>"; 
    15 $titlename = "<tr> 
    16                <th style='70px;'>表格1</th> 
    17                <th style='70px;'>表格2</th> 
    18                <th style='70px;'>表格3</th> 
    19             </tr>"; 
    20 $filename = $title.".xls"; 
    21 $obj->excelData($data,$titlename,$headtitle,$filename); 
    22 
    23 
    24 ?>

    3、测试

    点击访问:

    下载该Excel文件

    成功后查看该文件:

    进入后Excel提示说该文件格式与后缀名不一致,这也间接说明了我们所导出来的Excel文件仅仅只是个外表是Excel(实质是html文件),格式上并不是Excel文件。

    点击是进入查看里面的内容,上看去挺像Excel的嘛,哈哈。就酱紫

    更改后缀名为html进入查看:

    你瞧,实质就是html文件嘛,只是Excel支持该格式而已。

    4、参考文献

    1. 《PHP导出Excel》 

    (以上是自己的一些见解,若有不足或者错误的地方请各位指出)

     作者:那一叶随风   http://www.cnblogs.com/phpstudy2015-6/

     原文地址:http://www.cnblogs.com/phpstudy2015-6/p/7260208.html

     声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接

  • 相关阅读:
    SQL Server 2008R2中取得详细日期到毫秒级
    SQL Server Analysis Services无法启动
    SQL Server 2008R2编写脚本时智能提示功能丢失的处理
    SQL Server使用Linkserver连接Oracle
    好用的eclipse快捷键
    今天项目出现的问题总结
    8.2微信小页面问题总结
    SpringBoot整合rabbitmq
    初学Netty(杰哥好久不见)
    消息队列RabbitMQ学习
  • 原文地址:https://www.cnblogs.com/phpstudy2015-6/p/7260208.html
Copyright © 2011-2022 走看看