输出word EXCEL xml html处理---H_print.php
2012-01-17 15:33
603 查看
<?php /************************************* * 输出word EXCEL xml html处理---H_print.php * 创建人: huang_xiang * 创建时间:2008-12-25 9:29 * 更新时间:2009-2-18 13:38 * Email:yin273642232@163.com QQ:273642232 *************************************/ /************************************* 实例代码 include_once("H_print.php"); //引入文件 $H_print = new H_print(); //产生对象 $H_ary = array( array("id","name//姓名","age//年龄"), array("1","姓名1","23"), array("2","姓名2|bgcolor=yellow,align=right,colspan=2|b=b,color=red,size=19px"), array("3","姓名3","18"), array("4","姓名4","20") ); //数据源 $H_fname = '测试'; //文件名(自动命名后缀名) $H_print -> H_table($H_ary); //返回:生成表格 $H_print -> H_sxml($H_ary); //返回:生成XML $H_print -> H_xml($H_ary,$H_fname,$H_char="gb2312"); //返回:输出xml 文件 $H_print -> H_json(); //返回:无-编写中 $H_print -> H_excel($H_ary,$H_fname,$H_char="gb2312"); //返回:生成EXCEL $H_print -> H_word($H_ary,$H_fname,$H_char="gb2312"); //返回:生成WORD $H_print -> H_html($H_ary,$H_fname,$H_char="gb2312"); //返回:生成HTML $H_print -> H_txt($H_ary,$H_fname,$H_char="gb2312"); //返回:生成TXT $H_print -> H_html_excel($H_url,$H_fname,$H_char="gb2312"); //返回:生成EXCEL $H_print -> H_html_word($H_url,$H_fname,$H_char="gb2312"); //返回:生成WORD *************************************/ //Start Class class H_print{ public $H_debug = false; public $H_desce; //描述 public $H_ROOT_DIR; //文件地址路径 public $H_tbbor=0; //表格边框 public $H_tbbrc="#000000"; //表格边框色 public $H_tbwid=0; //表格宽度 public $H_tbhei=0; //表格高度 public $H_tbbgc="#FFFFFF"; //表格背景色 public $H_tbali = ""; //表格align public $H_tbrul = ""; //表格rules public function __construct(){ //当实例化一个对象的时候,这个对象的这个方法首先被调用 return ''; } public function __destruct(){ //当删除一个对象或对象操作终止的时候,调用该方法 return ''; } public function __get($key){ //当试图读取一个并不存在的属性的时候被调用 return '['.$key.'] Variable not find'; } public function __set($key,$val){ //当试图向一个并不存在的属性写入值的时候被调用 return '['.$key.'] Variable not find'; } public function __call($key,$args){ //当试图调用一个对象并不存在的方法时,调用该方法 return '['.$key.'] Function not find'; } public function __toString(){ //当打印一个对象的时候被调用 return $this -> H_desce(); } public function __clone(){ //当对象被克隆时,被调用 return "clone"; } public function H_desce(){ //返回描述 $this -> H_desce .= '类名:H_print-输出word EXCEL xml html处理;'; $this -> H_desce .= '函数:H_align($H_fg),返回:横向排列方式,参数:$H_fg-排列的方式;'; $this -> H_desce .= '函数:H_valgn($H_vfg),返回:纵向排列方式,参数:$H_vfg-排列的方式;'; $this -> H_desce .= '函数:H_table($H_ary),返回:生成表格,参数:$H_ary-生成表格的数据数组;'; $this -> H_desce .= '函数:H_sxml($H_ary),返回:生成XML,参数:$H_ary-生成XML的数据数组;'; $this -> H_desce .= '函数:H_xml($H_ary,$H_fname,$H_char="gb2312"),返回:输出xml 文件,参数:$H_ary-输出xml文件的数据数组.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_json(),返回:无,参数:无;'; $this -> H_desce .= '函数:H_excel($H_ary,$H_fname,$H_char="gb2312"),返回:生成EXCEL,参数:$H_ary-生成EXCEL的数据数组.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_word($H_ary,$H_fname,$H_char="gb2312"),返回:生成WORD,参数:$H_ary-生成WORD的数据数组.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_html($H_ary,$H_fname,$H_char="gb2312"),返回:生成HTML,参数:$H_ary-生成HTML的数据数组.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_txt($H_ary,$H_fname,$H_char="gb2312"),返回:生成TXT,参数:$H_ary-生成HTML的数据数组.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_html_excel($H_url,$H_fname,$H_char="gb2312"),返回:生成EXCEL,参数:$H_url-生成EXCEL的网页超连接.$H_fname-生成文件名称,$H_char-编码格式;'; $this -> H_desce .= '函数:H_html_word($H_url,$H_fname,$H_char="gb2312"),返回:生成WORD,参数:$H_url-生成WORD的网页超连接.$H_fname-生成文件名称,$H_char-编码格式;'; return $this -> H_desce; } public function H_align($H_fg){ //返回横向排列方式 switch($H_fg){ case "1" : { //居左 return ' align="left"'; break; } case "2" : { //居中 return ' align="center"'; break; } case "3" : { //居右 return ' align="right"'; break; } default : return ''; } } public function H_valgn($H_vfg){ //返回纵向排列方式 switch($H_vfg){ case "1" : { //居上 return ' valign="top"'; break; } case "2" : { //居正 return ' valign="middle"'; break; } case "3" : { //居下 return ' valign="bottom"'; break; } default : return ''; } } public function H_table($H_ary){ //生成表格 $H_sxtd = array("rowspan","colspan","align","valign","width","height","boder","bodercolor","bgcolor"); $H_sxwz = array("b","color","size"); $H_ei = count($H_ary); $H_tbstr = ""; if($this -> H_tbbor != ""){ $H_tbstr .= ' border="'.$this -> H_tbbor.'"'; $H_tbstr .= $this -> H_tbbrc != "" ? ' bordercolor="'.$this -> H_tbbrc.'"':''; } $H_tbstr .= $this -> H_tbali != "" ? ' align="'.$this -> H_tbali.'"':''; $H_tbstr .= $this -> H_tbwid > 0 ? ' width="'.$this -> H_tbwid.'"':''; $H_tbstr .= $this -> H_tbhei > 0 ? ' height="'.$this -> H_tbhei.'"':''; $H_tbstr .= $this -> H_tbbgc != "" ? ' bgcolor="'.$this -> H_tbbgc.'"':''; //设置表格属性 $H_estr = $H_ei > 0 ? '<table'.$H_tbstr.'>':''; for($H_i = 0;$H_i < $H_ei;$H_i++){ $H_estr .= '<tr>'; $H_ej = count($H_ary[$H_i]); for($H_j = 0;$H_j < $H_ej;$H_j++){ $H_td = ""; $H_tdwz = ""; //标题(处理注释) $H_hdtm = explode("//",$H_ary[$H_i][$H_j]); $H_zstr = $H_hdtm[0].($H_hdtm[1] != ''? '('.$H_hdtm[1].')':''); $H_temp = explode("|",$H_zstr); //处理单格属性 $H_tmtd = explode(",",$H_temp[1]); for($i = 0;$i < count($H_tmtd);$i++){ $H_tm = explode("=",$H_tmtd[$i]); $H_tm[0] = strtolower($H_tm[0]); //转成小写 if(in_array($H_tm[0],$H_sxtd,true)) $H_td .= ' '.$H_tm[0].'="'.$H_tm[1].'"'; } //处理单格文字属性 $H_tmtd = explode(",",$H_temp[2]); for($i = 0;$i < count($H_tmtd);$i++){ $H_tm = explode("=",$H_tmtd[$i]); $H_tm[0] = strtolower($H_tm[0]); //转成小写 if(in_array($H_tm[0],$H_sxwz,true)){ if($H_tm[0] == "b"){ $H_temp[0] = '<b>'.$H_temp[0].'</b>'; }else{ $H_tdwz .= ' '.$H_tm[0].'="'.$H_tm[1].'"'; } } } $H_temp[0] = $H_tdwz != ''? '<font'.$H_tdwz.'>'.$H_temp[0].'</font>':$H_temp[0]; $H_estr .= '<td'.$H_td.'>'.$H_temp[0].'</td>'; $H_tdwz = ""; $H_td = ""; } $H_estr .= '</tr>'; } $H_estr .= $H_ei > 0 ? '</table>':''; return $H_estr; } public function H_sxml($H_ary){ //生成XML $H_xstr = '<xml>'; $H_xml = array(); //节点标签 $H_xmlzs = array(); //注释 $H_ei = count($H_ary); for($H_i = 0;$H_i < $H_ei;$H_i++){ $H_xstr .= $H_i > 0 ? '<value>':''; $H_ej = count($H_ary[$H_i]); for($H_j = 0;$H_j < $H_ej;$H_j++){ //第一次所以处理为标签 if($H_i == 0){ $H_tm = explode("//",$H_ary[$H_i][$H_j]); $H_xml[$H_j] = $H_tm[0]; $H_xmlzs[$H_j] = $H_tm[1]; }else{ if($H_xml[$H_j]=="" ||$H_xml[$H_j]=="null") $H_xml[$H_j]== "items".$H_j; $H_tm = explode("|",$H_ary[$H_i][$H_j]); $H_xstr .='<'.$H_xml[$H_j].'>'.$H_tm[0].'</'.$H_xml[$H_j].'>'.(($H_i == 1 && $H_xmlzs[$H_j]!="") ? '<!--'.$H_xmlzs[$H_j].'-->':''); } } $H_xstr .= $H_i > 0 ? '</value>':''; } $H_xstr .= '</xml>'; return $H_xstr; } public function H_xml($H_ary,$H_fname,$H_char="gb2312"){ //输出xml 文件 $H_xml = '<?xml version="1.0"'.($H_char == ''?'':' encoding="'.$H_char.'"').'?>'; $H_xml .= $this -> H_sxml($H_ary); $H_xsize = strlen($H_xml); //设置头信息 @Header('Content-type: application/octet-stream'); @header('Content-Type: text/xml'); @Header('Accept-Ranges: bytes'); @Header('Accept-Length: '.$H_xsize); @Header('Content-Disposition: attachment; filename='.$H_fname.'.xml'); echo $H_xml; //输出 } public function H_json(){ //输出json 文件 } public function H_excel($H_ary,$H_fname,$H_char="gb2312"){ //输出excel 文件 //设置头信息 @header('Content-type:Application\vnd.ms-execl'); @header('content-Disposition:filename='.$H_fname.'.xls'); $H_fstr = ""; $H_fstr .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv="expires" content="Mon, 08 Dec 2008 00:00:01 GMT"/><meta http-equiv=Content-Type content="text/html; charset='.$H_char.'"/></head><body link="blue" vlink="purple" leftmargin="0" topmargin="0">'; $H_fstr .= $this -> H_table($H_ary); $H_fstr .= '</body>'; $H_fstr .= '</html>'; echo $H_fstr; //输出 flush(); } public function H_word($H_ary,$H_fname,$H_char="gb2312"){ //输出word 文件 //设置头信息 @header('Content-type: application/doc'); @header('Content-Disposition: attachment; filename="'.$H_fname.'.doc"'); $H_fstr = ""; $H_fstr .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv="Content-Type" content="text/html; charset='.$H_char.'"/><title>'.$H_fname.'</title></head><body>'; $H_fstr .= $this -> H_table($H_ary); $H_fstr .= '</body>'; $H_fstr .= '</html>'; echo $H_fstr; //输出 flush(); } public function H_html_excel($H_url,$H_fname,$H_char="gb2312"){ //html输出excel 文件 //设置头信息 @header('Content-type:Application\vnd.ms-execl'); @header('content-Disposition:filename='.$H_fname.'.xls'); $H_fstr = file_get_contents($H_url); /* $H_fstr .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv="expires" content="Mon, 08 Dec 2008 00:00:01 GMT"/><meta http-equiv=Content-Type content="text/html; charset='.$H_char.'"/></head><body link="blue" vlink="purple" leftmargin="0" topmargin="0">'; $H_fstr .= $this -> H_table($H_ary); $H_fstr .= '</body>'; $H_fstr .= '</html>'; */ echo $H_fstr; //输出 flush(); } public function H_html_word($H_url,$H_fname,$H_char="gb2312"){ //html输出word 文件 //设置头信息 @header('Content-type: application/doc'); @header('Content-Disposition: attachment; filename="'.$H_fname.'.doc"'); $H_fstr = file_get_contents($H_url); /* $H_fstr .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv="Content-Type" content="text/html; charset='.$H_char.'"/><title>'.$H_fname.'</title></head><body>'; $H_fstr .= $this -> H_table($H_ary); $H_fstr .= '</body>'; $H_fstr .= '</html>'; */ echo $H_fstr; //输出 flush(); } public function H_html($H_ary,$H_fname,$H_char="gb2312"){ //输出html 文件 $H_fstr = ""; $H_fstr .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'; $H_fstr .= '<head>'; $H_fstr .= '<title> '.$H_fname.' </title>'; $H_fstr .= '<meta http-equiv="Content-Type" content="text/html; charset='.$H_char.'" />'; $H_fstr .= $this -> H_table($H_ary); $H_fstr .= '</body>'; $H_fstr .= '</html>'; $H_fsize = strlen($H_fstr); //设置头信息 @Header('Content-type: application/octet-stream'); @header('Content-Type: text/html'); @Header('Accept-Ranges: bytes'); @Header('Accept-Length: '.$H_fsize); @Header('Content-Disposition: attachment; filename='.$H_fname.'.html'); echo $H_fstr; //输出 flush(); } public function H_txt($H_ary,$H_fname,$H_char="gb2312"){ //输出txt 文件 $H_fstr = ""; $H_fstr .= $this -> H_table($H_ary); $H_fsize = strlen($H_fstr); //设置头信息 @Header('Content-type: application/octet-stream'); @header('Content-Type: text/html'); @Header('Accept-Ranges: bytes'); @Header('Accept-Length: '.$H_fsize); @Header('Content-Disposition: attachment; filename='.$H_fname.'.txt'); echo $H_fstr; //输出 flush(); } }//End Class ?>
相关文章推荐
- PHP中基于b2core框架内部的网页上Html输出生成Word的处理
- DataGrid输出到Excel(word)并进行格式化处理(转)
- php 备份数据库(生成word,excel,json,xml,sql)
- php 备份数据库(生成word,excel,json,xml,sql)
- php 备份数据库代码(生成word,excel,json,xml,sql)
- freemarker生成excel、word、html、xml实例教程
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- freemarker生成excel、word、html、xml实例教程
- PHP使用xmllint命令处理xml与html的方法
- ASP.NET中将数据输出到Excel,TXT, Word, Html
- freemarker生成excel、word、html、xml实例教程
- php html 模板输出 excel 例子
- php数据库导出类 导出JSON,XML,WORD,EXCEL
- DataGrid输出到Excel(word)并进行格式化处理
- 浏览器处理由带BOM的utf-8格式的php文件输出的HTML问题
- ireport导出各种格式(pdf,excel,word,html,print)
- 几个DataSet数据导出到XML Word Excel TXT HTML的函数
- php数据库导出类 导出JSON,XML,WORD,EXCEL
- php 备份数据库(生成word,excel,json,xml,sql)
- freemarker生成excel、word、html、xml实例教程