首先要包含一个excel类:

事例代码:

  1. require_once("..../php-excel.class.php"); /*包含在相关目录*/
  2. $data_new= array(array('ip','','DateTime','Zone','Method','URL','Protocol','Code','Length','Referer','UA' ,'Cookie_id'),array('001','','20120202','','get','http://www.baidu.com','none','20','','ss','cc' ,'20012211'));
  3. $xls = new Excel_XML('utf8', false, 'Log Report');
  4. $xls->addArray($data_new);
  5. $xls->generateXML("diy_1");

注意:$data_new需要是二维数组。

相关类:

  1. <?php
  2.  
  3. /**
  4. * Simple excel generating from PHP5
  5. *
  6. * @package Utilities
  7. * @license http://www.opensource.org/licenses/mit-license.php
  8. * @author Oliver Schwarz <oliver.schwarz@gmail.com>
  9. * @version 1.0
  10. */
  11.  
  12. /**
  13. * Generating excel documents on-the-fly from PHP5
  14. *
  15. * Uses the excel XML-specification to generate a native
  16. * XML document, readable/processable by excel.
  17. *
  18. * @package Utilities
  19. * @subpackage Excel
  20. * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
  21. * @version 1.1
  22. *
  23. * @todo Issue #4: Internet Explorer 7 does not work well with the given header
  24. * @todo Add option to give out first line as header (bold text)
  25. * @todo Add option to give out last line as footer (bold text)
  26. * @todo Add option to write to file
  27. */
  28. class Excel_XML
  29. {
  30.  
  31. /**
  32. * Header (of document)
  33. * @var string
  34. */
  35. private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
  36.  
  37. /**
  38. * Footer (of document)
  39. * @var string
  40. */
  41. private $footer = "</Workbook>";
  42.  
  43. /**
  44. * Lines to output in the excel document
  45. * @var array
  46. */
  47. private $lines = array();
  48.  
  49. /**
  50. * Used encoding
  51. * @var string
  52. */
  53. private $sEncoding;
  54.  
  55. /**
  56. * Convert variable types
  57. * @var boolean
  58. */
  59. private $bConvertTypes;
  60.  
  61. /**
  62. * Worksheet title
  63. * @var string
  64. */
  65. private $sWorksheetTitle;
  66.  
  67. /**
  68. * Constructor
  69. *
  70. * The constructor allows the setting of some additional
  71. * parameters so that the library may be configured to
  72. * one's needs.
  73. *
  74. * On converting types:
  75. * When set to true, the library tries to identify the type of
  76. * the variable value and set the field specification for Excel
  77. * accordingly. Be careful with article numbers or postcodes
  78. * starting with a '0' (zero)!
  79. *
  80. * @param string $sEncoding Encoding to be used (defaults to UTF-8)
  81. * @param boolean $bConvertTypes Convert variables to field specification
  82. * @param string $sWorksheetTitle Title for the worksheet
  83. */
  84. public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
  85. {
  86. $this->bConvertTypes = $bConvertTypes;
  87. $this->setEncoding($sEncoding);
  88. $this->setWorksheetTitle($sWorksheetTitle);
  89. }
  90.  
  91. /**
  92. * Set encoding
  93. * @param string Encoding type to set
  94. */
  95. public function setEncoding($sEncoding)
  96. {
  97. $this->sEncoding = $sEncoding;
  98. }
  99.  
  100. /**
  101. * Set worksheet title
  102. *
  103. * Strips out not allowed characters and trims the
  104. * title to a maximum length of 31.
  105. *
  106. * @param string $title Title for worksheet
  107. */
  108. public function setWorksheetTitle ($title)
  109. {
  110. $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
  111. $title = substr ($title, 0, 31);
  112. $this->sWorksheetTitle = $title;
  113. }
  114.  
  115. /**
  116. * Add row
  117. *
  118. * Adds a single row to the document. If set to true, self::bConvertTypes
  119. * checks the type of variable and returns the specific field settings
  120. * for the cell.
  121. *
  122. * @param array $array One-dimensional array with row content
  123. */
  124. private function addRow ($array)
  125. {
  126. $cells = "";
  127. foreach ($array as $k => $v):
  128. $type = 'String';
  129. if ($this->bConvertTypes === true && is_numeric($v)):
  130. $type = 'Number';
  131. endif;
  132. $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
  133. $cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";
  134. endforeach;
  135. $this->lines[] = "<Row>\n" . $cells . "</Row>\n";
  136. }
  137.  
  138. /**
  139. * Add an array to the document
  140. * @param array 2-dimensional array
  141. */
  142. public function addArray ($array)
  143. {
  144. foreach ($array as $k => $v)
  145. $this->addRow ($v);
  146. }
  147.  
  148. /**
  149. * Generate the excel file
  150. * @param string $filename Name of excel file to generate (...xls)
  151. */
  152. public function generateXML ($filename = 'excel-export')
  153. {
  154. // correct/validate filename
  155. $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
  156.  
  157. // deliver header (as recommended in php manual)
  158. header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
  159. header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
  160.  
  161. // print out document to the browser
  162. // need to use stripslashes for the damn ">"
  163. echo stripslashes (sprintf($this->header, $this->sEncoding));
  164. echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";
  165. foreach ($this->lines as $line)
  166. echo $line;
  167.  
  168. echo "</Table>\n</Worksheet>\n";
  169. echo $this->footer;
  170. }
  171.  
  172. }
  173.  
  174. ?>

php以excel的形式将数据输出的更多相关文章

  1. 把数据输出到Word (组件形式)

    上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ...

  2. 把数据输出到Word (非插件形式)

    项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...

  3. python中将字典形式的数据循环插入Excel

    1.我们看到字典形式的数据如下所示 list=[["2891-1", "D"],["2892-1", "D"],[&qu ...

  4. .Net读取Excel文件时丢失数据的问题 (转载)

    相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...

  5. Python xlrd模块读取Excel表中的数据

    1.xlrd库的安装 直接使用pip工具进行安装(当然也可以使用pycharmIDE进行安装,这里就不详述了) pip install xlrd 2.xlrd模块的一些常用命令 ①打开excel文件并 ...

  6. 【matlab】将matlab中数据输出保存为txt或dat格式

    将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...

  7. 将matlab中数据输出保存为txt或dat格式

    :FID= FOPEN(filename,permission) 用指定的方式打开文件 FID=+N(N是正整数):表示文件打开成功,文件代号是N. FID=-1            : 表示文件打 ...

  8. ffmpeg 从内存中读取数据(或将数据输出到内存)

    更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...

  9. 将Excel上千条数据写入到数据库中

    简要说明:因工作需要,需要一张Excel表格中的所有数据导入到数据库中.如下表,当然这只是一部分,一共一千多条. 前期处理: 首先要保证上图中的Excel表格中的数据不能为空,如果有为空的数据,可以稍 ...

随机推荐

  1. Eclipse如何设置代码提示功能

    Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...

  2. 扫盲如何在ECLIPSE中使用条件断点

    有时候在编码的时候我们希望知道代码变量符合某个条件时,才中断点,其他的情况不中断点.   解决办法1:   我们写个代码 判断,符合条件在符合条件处进行断点,这个方法很麻烦,需要去修改代码,不要是还需 ...

  3. cf卡中,wtmp文件较大,导致磁盘空间满了

    看了一下,有一个wtmp 和wtmp.1的文件非常大.wtmp记录的是机器注销.启动的信息.由此可见,机器长时间的不断重启,造成该日志记录超级大,把cf的空间给占满了. wtmp日志可以用who和la ...

  4. Fence9

    题目大意: 求点(0,0),(n,m),(p,0)三点构成的三角形内部(不包括边界)整点的个数. 解题过程:1.直接枚举纵坐标,然后算出两条直线上纵坐标为y的点的横坐标,然后他们中间的点就是符合要求的 ...

  5. USB鼠标按键驱动

    现象:把USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有"驱动程序",为何能 ...

  6. JAVA异常体系

    1.异常体系 ----|Throwable 所有错误或异常的父类 --------|Error(错误) --------|Exception(异常)一般能通过代码处理 ------------|运行时 ...

  7. bzoj 1185 旋转卡壳 最小矩形覆盖

    题目大意 就是求一个最小矩形覆盖,逆时针输出其上面的点 这里可以看出,那个最小的矩形覆盖必然有一条边经过其中凸包上的两个点,另外三条边必然至少经过其中一个点,而这样的每一个点逆时针走一遍都满足单调性 ...

  8. 踏着前人的脚印学Hadoop——序列化,Writerable

    package org.apache.hadoop.io; import java.io.DataOutput;import java.io.DataInput;import java.io.IOEx ...

  9. JS页面打印,预览,设置,分页

    一)在HTML页中加载打印对象 <object id="WebBrowser" width="0" height="0" classi ...

  10. 记录一些容易忘记的属性 -- UIView

    一个视图原来添加在某个父视图上,然后再将它添加到另外的一个视图上,这个视图会从原来的某个父视图中移除,添加到新的视图上. 子视图对象指针存在父视图的subviews数组中,说明,一个视图可以有多个子视 ...