当前位置:澳门新葡亰登入 > 澳门新葡亰登入 > php生成 execel表格

php生成 execel表格

文章作者:澳门新葡亰登入 上传时间:2020-04-21

?phpclass Excel{ var $header = ?xml version=1.0 encoding=UTF-8?Workbook xmlns=urn:schemas-microsoft-com:office:spreadsheetxmlns:x=urn:schemas-microsoft-com:office:excelxmlns:ss=urn:schemas-microsoft-com:office:spreadsheetxmlns:html=; var $footer = /Workbook; var $lines = array (); var $worksheet_title = Table1; function addRow ($array) { // 初始化列 $cells = ; // foreach key

  • write value into cells foreach ($array as $k = $v): // 加个字符串与数字的判断 避免生成的 excel 出现数字以字符串存储的警告 if(is_numeric($v)) { // 防止首字母为 0 时生成 excel 后 0 丢失 if(substr($v, 0, 1) == 0) { $cells .= CellData ss:Type=String . $v . /Data/Cell ; } else { $cells .= CellData ss:Type=Number . $v . /Data/Cell ; } } else { $cells .= CellData ss:Type=String . $v . /Data/Cell ; } endforeach; // transform $cells content into one row $this-lines[] = Row . $cells . /Row ; } function addArray ($array) { // 返回数组并保存到单元格中去 foreach ($array as $k = $v): $this-addRow ($v); endforeach; } function setWorksheetTitle ($title) { $title = preg_replace (/[|:|/|?|*|[|]]/, , $title); // 取得标题长度 $title = substr ($title, 0, 31); // 赋值 $this-worksheet_title = $title; } function generateXML ($filename) { header(Content-Type: application/vnd.ms-excel; charset=UTF-8); header(Content-Disposition: inline; filename= . $filename . .xls); echo stripslashes ($this-header); echo Worksheet ss:Name= . $this-worksheet_title . Table ; echo Column ss:Index=1 ss:AutoFitWidth=0 ss:Width=110/ ; echo implode ( , $this-lines); echo /Table /Worksheet ; echo $this-footer; }}/*** CakePHP中使用方法* 注意 ** cakePHP 配置文件 define('DEBUG', 0);** vendor ('Excel');* $doc = array (* 0 = array ('中国', '中国人', '中国人民', '123456');* );* $xls = new Excel;* $xls-addArray ( $doc );* $xls-generateXML (mytest);*//*** 非框架使用方法** require_once('excel.php');* $doc = array (* 0 = array ('中国', '中国人', '中国人民', '123456');* );* $xls = new Excel;* $xls-addArray ( $doc );* $xls-generateXML (mytest);*/?

本文由澳门新葡亰登入发布于澳门新葡亰登入,转载请注明出处:php生成 execel表格

关键词: