读书人

[] 超简略的XmlExcelExport-生成Excel

发布时间: 2012-10-07 17:28:51 作者: rapoo

[] 超简单的XmlExcelExport-生成Excel

<?php// 数据导出 类文件 -- 作者 色色/** * 导出 xml格式的 excel 数据 */class xmlexcelexport{/** * 文档头标签 *  * @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\">";/** * 文档尾标签 *  * @var string */private $footer = "</workbook>";/** * 内容编码 * @var string */private $sencoding;/** * 是否转换特定字段值的类型 *  * @var boolean */private $bconverttypes;/** * 生成的excel内工作簿的个数 *  * @var int */private $dworksheetcount = 0;/** * 构造函数 *  * 使用类型转换时要确保:页码和邮编号以'0'开头 * * @param string $sencoding 内容编码 * @param boolean $bconverttypes 是否转换特定字段值的类型 */function __construct($sencoding = 'utf-8', $bconverttypes = false){$this->bconverttypes = $bconverttypes;$this->sencoding = $sencoding;}/** * 返回工作簿标题,最大 字符数为 31 * * @param string $title 工作簿标题 * @return string */function getworksheettitle($title = 'table1'){$title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", empty($title) ? 'table' . ($this->dworksheetcount + 1) : $title);return substr($title, 0, 31);}/** * 向客户端发送excel头信息 * * @param string $filename 文件名称,不能是中文 */function generatexmlheader($filename){$filename = preg_replace('/[^aa-zz0-9\_\-]/', '', $filename);$filename = urlencode($filename);// 中文名称使用urlencode编码后在ie中打开能保存成中文名称的文件,但是在ff上却是乱码header("pragma: public");   header("expires: 0");header("cache-control: must-revalidate, post-check=0, pre-check=0");header("content-type: application/force-download");header("content-type: application/vnd.ms-excel; charset={$this->sencoding}");header("content-transfer-encoding: binary");header("content-disposition: attachment; filename={$filename}.xls");echo stripslashes(sprintf($this->header, $this->sencoding));}/** * 向客户端发送excel结束标签 * * @param string $filename 文件名称,不能是中文 */function generatexmlfoot(){echo $this->footer;}/** * 开启工作簿 *  * @param string $title */function worksheetstart($title){$this->dworksheetcount ++;echo "\n<worksheet ss:name=\"" . $this->getworksheettitle($title) . "\">\n<table>\n";}/** * 结束工作簿 */function worksheetend(){echo "</table>\n</worksheet>\n";}/** * 设置表头信息 *  * @param array $header */function settableheader(array $header){echo $this->_parserow($header);}/** * 设置表内行记录数据 *  * @param array $rows 多行记录 */function settablerows(array $rows){foreach ($rows as $row) echo $this->_parserow($row);}/** * 将传人的单行记录数组转换成 xml 标签形式 * * @param array $array 单行记录数组 */private function _parserow(array $row){$cells = "";foreach ($row as $k => $v){$type = 'string';if ($this->bconverttypes === true && is_numeric($v))$type = 'number';$v = htmlentities($v, ent_compat, $this->sencoding);$cells .= "<cell><data ss:type=\"$type\">" . $v . "</data></cell>\n";}return "<row>\n" . $cells . "</row>\n";}}
??
?
经过测试在excel2007 ,? 单工作簿支持 10w条 以上的数据....
?
对于大数据量的导出,一次取出全部数据不太现实,容易内存溢出,建议分段提取,每次 提取 200 条数据
?
?
此类配合 我之前发的 coredb 类 性能还好 哈哈哈
?
<img src="http://dl.iteye.com/upload/attachment/546527/22e9dc8b-d267-3b7a-ac09-86cdbd9880a9.jpg" alt="[] 超简略的XmlExcelExport-生成Excel" width="516" height="314">

读书人网 >XML SOAP

热点推荐