一个简单的数据导出excel类
2016-03-15 16:56
429 查看
<?php /** * 生成excel文件操作 */ class Excel { private $limit = 10000; public function download($data, $fileName) { $fileName = $this->_charset($fileName); header("Content-Type: application/vnd.ms-excel; charset=gbk"); header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\""); echo "<?xml version=\"1.0\" encoding=\"gbk\"?>\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\">"; echo "\n<Worksheet ss:Name=\"" . $fileName . "\">\n<Table>\n"; $guard = 0; foreach($data as $v) { $guard++; if($guard==$this->limit) { ob_flush(); flush(); $guard = 0; } echo $this->_addRow($this->_charset($v)); } echo "</Table>\n</Worksheet>\n</Workbook>"; } private function _addRow($row) { $cells = ""; foreach ($row as $k => $v) { $cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n"; } return "<Row>\n" . $cells . "</Row>\n"; } private function _charset($data) { if(!$data) { return false; } if(is_array($data)) { foreach($data as $k=>$v) { $data[$k] = $this->_charset($v); } return $data; } return iconv('utf-8', 'gbk', $data); } } //使用方法 $excel = new Excel(); $data = array( array('姓名','标题','文章','价格','数据5','数据6','数据7'), array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'), array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'), array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'), array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'), array('数据1','数据2','数据3','数据4','数据5','数据6','数据7') ); $excel->download($data, '这是一个测试'); ?>
相关文章推荐
- 使用Python生成Excel格式的图片
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- Mootools 1.2教程 函数
- VBA将excel数据表生成JSON文件
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数