php以excel的形式将数据输出
首先要包含一个excel类:
事例代码:
- require_once("..../php-excel.class.php"); /*包含在相关目录*/
- $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'));
- $xls = new Excel_XML('utf8', false, 'Log Report');
- $xls->addArray($data_new);
- $xls->generateXML("diy_1");
注意:$data_new需要是二维数组。
相关类:
- <?php
- /**
- * Simple excel generating from PHP5
- *
- * @package Utilities
- * @license http://www.opensource.org/licenses/mit-license.php
- * @author Oliver Schwarz <oliver.schwarz@gmail.com>
- * @version 1.0
- */
- /**
- * Generating excel documents on-the-fly from PHP5
- *
- * Uses the excel XML-specification to generate a native
- * XML document, readable/processable by excel.
- *
- * @package Utilities
- * @subpackage Excel
- * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
- * @version 1.1
- *
- * @todo Issue #4: Internet Explorer 7 does not work well with the given header
- * @todo Add option to give out first line as header (bold text)
- * @todo Add option to give out last line as footer (bold text)
- * @todo Add option to write to file
- */
- class Excel_XML
- {
- /**
- * Header (of document)
- * @var string
- */
- 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\">";
- /**
- * Footer (of document)
- * @var string
- */
- private $footer = "</Workbook>";
- /**
- * Lines to output in the excel document
- * @var array
- */
- private $lines = array();
- /**
- * Used encoding
- * @var string
- */
- private $sEncoding;
- /**
- * Convert variable types
- * @var boolean
- */
- private $bConvertTypes;
- /**
- * Worksheet title
- * @var string
- */
- private $sWorksheetTitle;
- /**
- * Constructor
- *
- * The constructor allows the setting of some additional
- * parameters so that the library may be configured to
- * one's needs.
- *
- * On converting types:
- * When set to true, the library tries to identify the type of
- * the variable value and set the field specification for Excel
- * accordingly. Be careful with article numbers or postcodes
- * starting with a '0' (zero)!
- *
- * @param string $sEncoding Encoding to be used (defaults to UTF-8)
- * @param boolean $bConvertTypes Convert variables to field specification
- * @param string $sWorksheetTitle Title for the worksheet
- */
- public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
- {
- $this->bConvertTypes = $bConvertTypes;
- $this->setEncoding($sEncoding);
- $this->setWorksheetTitle($sWorksheetTitle);
- }
- /**
- * Set encoding
- * @param string Encoding type to set
- */
- public function setEncoding($sEncoding)
- {
- $this->sEncoding = $sEncoding;
- }
- /**
- * Set worksheet title
- *
- * Strips out not allowed characters and trims the
- * title to a maximum length of 31.
- *
- * @param string $title Title for worksheet
- */
- public function setWorksheetTitle ($title)
- {
- $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
- $title = substr ($title, 0, 31);
- $this->sWorksheetTitle = $title;
- }
- /**
- * Add row
- *
- * Adds a single row to the document. If set to true, self::bConvertTypes
- * checks the type of variable and returns the specific field settings
- * for the cell.
- *
- * @param array $array One-dimensional array with row content
- */
- private function addRow ($array)
- {
- $cells = "";
- foreach ($array as $k => $v):
- $type = 'String';
- if ($this->bConvertTypes === true && is_numeric($v)):
- $type = 'Number';
- endif;
- $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
- $cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";
- endforeach;
- $this->lines[] = "<Row>\n" . $cells . "</Row>\n";
- }
- /**
- * Add an array to the document
- * @param array 2-dimensional array
- */
- public function addArray ($array)
- {
- foreach ($array as $k => $v)
- $this->addRow ($v);
- }
- /**
- * Generate the excel file
- * @param string $filename Name of excel file to generate (...xls)
- */
- public function generateXML ($filename = 'excel-export')
- {
- // correct/validate filename
- $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
- // deliver header (as recommended in php manual)
- header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
- header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
- // print out document to the browser
- // need to use stripslashes for the damn ">"
- echo stripslashes (sprintf($this->header, $this->sEncoding));
- echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";
- foreach ($this->lines as $line)
- echo $line;
- echo "</Table>\n</Worksheet>\n";
- echo $this->footer;
- }
- }
- ?>
php以excel的形式将数据输出的更多相关文章
- 把数据输出到Word (组件形式)
上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ...
- 把数据输出到Word (非插件形式)
项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...
- python中将字典形式的数据循环插入Excel
1.我们看到字典形式的数据如下所示 list=[["2891-1", "D"],["2892-1", "D"],[&qu ...
- .Net读取Excel文件时丢失数据的问题 (转载)
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
- Python xlrd模块读取Excel表中的数据
1.xlrd库的安装 直接使用pip工具进行安装(当然也可以使用pycharmIDE进行安装,这里就不详述了) pip install xlrd 2.xlrd模块的一些常用命令 ①打开excel文件并 ...
- 【matlab】将matlab中数据输出保存为txt或dat格式
将matlab中数据输出保存为txt或dat格式 总结网上各大论坛,主要有三种方法. 第一种方法:save(最简单基本的) 具体的命令是:用save *.txt -ascii x x为变量 *.txt ...
- 将matlab中数据输出保存为txt或dat格式
:FID= FOPEN(filename,permission) 用指定的方式打开文件 FID=+N(N是正整数):表示文件打开成功,文件代号是N. FID=-1 : 表示文件打 ...
- ffmpeg 从内存中读取数据(或将数据输出到内存)
更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...
- 将Excel上千条数据写入到数据库中
简要说明:因工作需要,需要一张Excel表格中的所有数据导入到数据库中.如下表,当然这只是一部分,一共一千多条. 前期处理: 首先要保证上图中的Excel表格中的数据不能为空,如果有为空的数据,可以稍 ...
随机推荐
- Eclipse如何设置代码提示功能
Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...
- 扫盲如何在ECLIPSE中使用条件断点
有时候在编码的时候我们希望知道代码变量符合某个条件时,才中断点,其他的情况不中断点. 解决办法1: 我们写个代码 判断,符合条件在符合条件处进行断点,这个方法很麻烦,需要去修改代码,不要是还需 ...
- cf卡中,wtmp文件较大,导致磁盘空间满了
看了一下,有一个wtmp 和wtmp.1的文件非常大.wtmp记录的是机器注销.启动的信息.由此可见,机器长时间的不断重启,造成该日志记录超级大,把cf的空间给占满了. wtmp日志可以用who和la ...
- Fence9
题目大意: 求点(0,0),(n,m),(p,0)三点构成的三角形内部(不包括边界)整点的个数. 解题过程:1.直接枚举纵坐标,然后算出两条直线上纵坐标为y的点的横坐标,然后他们中间的点就是符合要求的 ...
- USB鼠标按键驱动
现象:把USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有"驱动程序",为何能 ...
- JAVA异常体系
1.异常体系 ----|Throwable 所有错误或异常的父类 --------|Error(错误) --------|Exception(异常)一般能通过代码处理 ------------|运行时 ...
- bzoj 1185 旋转卡壳 最小矩形覆盖
题目大意 就是求一个最小矩形覆盖,逆时针输出其上面的点 这里可以看出,那个最小的矩形覆盖必然有一条边经过其中凸包上的两个点,另外三条边必然至少经过其中一个点,而这样的每一个点逆时针走一遍都满足单调性 ...
- 踏着前人的脚印学Hadoop——序列化,Writerable
package org.apache.hadoop.io; import java.io.DataOutput;import java.io.DataInput;import java.io.IOEx ...
- JS页面打印,预览,设置,分页
一)在HTML页中加载打印对象 <object id="WebBrowser" width="0" height="0" classi ...
- 记录一些容易忘记的属性 -- UIView
一个视图原来添加在某个父视图上,然后再将它添加到另外的一个视图上,这个视图会从原来的某个父视图中移除,添加到新的视图上. 子视图对象指针存在父视图的subviews数组中,说明,一个视图可以有多个子视 ...