用Simple Excel导出xls实现方法

2015-01-24信息快讯网

因为前几天写了篇文章,用php-excel-reader类导入excel内容,顺便说些excel导出问题,需要的朋友可以了解下

因为前几天写了篇文章,用php-excel-reader类导入excel内容,顺便说些excel导出问题,我用的是simple excel,一个很简单的导出xls类,特好用!
simple excel源码如下:
 
<?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 
*/ 
class Excel_Xml 
{ 
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\">"; 
private $footer = "</Workbook>"; 
private $lines = array(); 
private $sEncoding; 
private $bConvertTypes; 
private $sWorksheetTitle; 
public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1') 
{ 
$this->bConvertTypes = $bConvertTypes; 
$this->setEncoding($sEncoding); 
$this->setWorksheetTitle($sWorksheetTitle); 
} 
public function setEncoding($sEncoding) 
{ 
$this->sEncoding = $sEncoding; 
} 
public function setWorksheetTitle ($title) 
{ 
$title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title); 
$title = substr ($title, 0, 31); 
$this->sWorksheetTitle = $title; 
} 
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"; 
} 
public function addArray ($array) 
{ 
foreach ($array as $k => $v) 
$this->addRow ($v); 
} 
public function generateXML ($filename = 'excel-export') 
{ 
$filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename); 
header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding); 
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\""); 
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案例如下:
 
<?php 
/** 
* @author mckee 
* @blog www.phpddt.com 
*/ 
require_once 'excel.class.php'; 
$xls = new Excel_Xml('UTF-8',false,'测试'); 
$data = array( 
1 => array('名称','地址'), 
2 => array('php点点通','www.phpddt.com'), 
3 => array('百度','www.baidu.com') 
); 
$xls->addArray($data); 
$xls->generateXML('name4test'); 

?> 

导出结果如下图:
用Simple Excel导出xls实现方法_信息快讯网
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
处理(php-cgi.exe - FastCGI 进程超过了配置的请求超时时限)的问题
解析array splice的移除数组中指定键的值,返回一个新的数组
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
基于simple_html_dom的使用小结
mongo Table类文件 获取MongoCursor(游标)的实现方法分析
file_get_contents("php://input", "r")实例介绍
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
php selectradio和checkbox默认选择的实现方法详解
php setcookie(name, value, expires, path, domain, secure) 参数详解
领悟php接口中interface存在的意义
比较strtr, str_replace和preg_replace三个函数的效率
基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
php 备份数据库代码(生成word,excel,json,xml,sql)
基于PHPExcel的常用方法总结
eAccelerator的安装与使用详解
windows下zendframework项目环境搭建(通过命令行配置)
php读取EXCEL文件 php excelreader读取excel文件
php excel reader读取excel内容存入数据库实现代码
PHPMailer使用教程(PHPMailer发送邮件实例分析)
PHP提示Notice: Undefined variable的解决办法
php如何调用webservice应用介绍
使用 PHPMAILER 发送邮件实例应用
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
php中explode与split的区别介绍
PHP explode()函数用法、切分字符串
php FLEA中二叉树数组的遍历输出
php中判断文件存在是用file_exists还是is_file的整理
PHPExcel读取Excel文件的实现代码
ThinkPHP与PHPExcel冲突解决方法
PHP导出MySQL数据到Excel文件(fputcsv)
©2014-2024 dbsqp.com