PHP 中基于 b2core 框架内部的网页上 Html 输出生成 Word 的处理

下面是部分的代码处理:

关键的函数是下面这几个,你要看懂这里的代码书写即可:

function report_data($yaml)
  {
/* Yaml 格式
-
  h1
  content
-
  table
  entity:uuid

-
  list
  entity:filed:val

*/
    foreach($yaml as $sec){
      $list = $unlist = $ndt = array();
      $tag = $sec[0];
      switch($tag){
        case 'table':
          $label = load('m/config_m')->key($sec[1]);
          $data = load('m/entity_m')->get_one($_POST[$sec[2]]);
          $dt = _decode($data['data']);
          if($sec[3]['unlist']){
            $unlist = explode(',',$sec[3]['unlist']);
            foreach($label['item'] as $k=>$v){
              if(!in_array($v['id'],$unlist)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          elseif($sec[3]['list'])
          {
            $list = explode(',',$sec[3]['list']);
            foreach($label['item'] as $k=>$v){
              if(in_array($v['id'],$list)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          $report1[] = array('table',array($label['item'],$dt));
          break;

        case 'list':
          $data  = $header = array();
          $label = load('m/config_m')->key($sec[1]);
          $rs = load('m/entity_m')->get_by_index($sec[1],$sec[2],$_POST[$sec[3]]);
          if($sec[4]['unlist']){
            $unlist = explode(',',$sec[4]['unlist']);
            foreach($label['item'] as $k=>$v){
              if(!in_array($v['id'],$unlist)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }
          elseif($sec[4]['list'])
          {
            $list = explode(',',$sec[4]['list']);
            foreach($label['item'] as $k=>$v){
              if(in_array($v['id'],$list)){
                $ndt[$k]= $v;
              }
            }
            $label['item'] = $ndt;
          }

          foreach($label['item'] as  $k => $label){
           switch($label['type']){
             case 'section':
               break;
             case 'system':
               $k = $k.'_label';
               $header[$k] = $label['name'];
               break;
             default:
               $header[$k] = $label['name'];
           }
          }

          $data[] = $header;
          foreach($rs as $line){
           $dt = _decode($line['data']);
           $tr = array();
           foreach($header as $k=>$label){
             $tr[] = $dt[$k];
           }
           $data[] = $tr;
          }
          $report1[] = array('list',$data);
          break;
        default:
           $report1[] = array($tag,$sec[1]);
     }
   }
    return $report1;
  }

  /*
    htmlt report :
    根据 数组数据 生成 html 格式报表
  */

  function html_report($report)
  {
    foreach($report as $r){
      $tag = $r[0];
      switch($tag){
        case 'h1':
        case 'h2':
        case 'h3':
        case 'h4':
          $out.= "<$tag>".$r[1]."</$tag>";
          break;
        case 'table':
          $data = $r[1];
          $label = $data[0];
          $dt = $data[1];
          $out.='<table class="print-table" >';
          foreach($label as $k=>$r){
            if($r['type']=='section')$out.='<tr><th colspan="2" >'.$r['name'].'</th></tr>';
            else $out.='<tr><td class="label" >'.$r['name'].'</td><td>'.$dt[$k].'</td></tr>';
          }
          $out.='</table>';
          break;
        case 'list':
          $data = $r[1];
          $no = '序号';
          $out.='<table class="print-table" >';
          foreach($data as $dt){
            $out.='<tr><td class="no" >'.$no.'</td>';
            foreach($dt as $d){
              $out.='<td>'.$d.'</td>';
            }
            $out.='</tr>';
            $no = ($no == '序号')?1:$no+1;
          }
          $out.='</table>';
          break;
        case 'textbox':
          $out.='<table class="print-table" ><tr><td> &nbsp; </td></tr></table>';
          break;
        case 'text':
          $out.='<div>'.$r[1].'</div>';
          break;
      }
    }
    return $out;
  }
}