excel表格

<?php
// -------------------------------excel表格----------------------------------
$json = '[{"hospitalname":"北京协和医院","levelname":"三级甲等"},{"hospitalname":"医科院肿瘤医院","levelname":"三级甲等"},{"hospitalname":"阜外心血管病医院","levelname":"三级甲等"},{"hospitalname":"北京市大兴区妇幼保健院","levelname":"二级甲等"},{"hospitalname":"北京市大兴区人民医院","levelname":"二级甲等"},{"hospitalname":"北京市大兴区红星医院","levelname":"二级综合医院"},{"hospitalname":"北京普祥中医肿瘤医院","levelname":"二级"},{"hospitalname":"北京市仁和医院","levelname":"二级甲等"},{"hospitalname":"中国中医科学院广安门医院南区","levelname":"三级甲等"},{"hospitalname":"北京市大兴区精神病医院","levelname":"二级甲等"}]';

$data = json_decode($json,true);
$result = ["医院名字","等级"];
$filename = "数据表";

excel($data,$result,$filename);
function excel($data,$result = '',$filename = '表格'){
	if($result){
		$result = implode(",",$result)."\r\n";
	}
	foreach ($data as $key => $value) {
		$result .= implode(',',$value)."\r\n";
	}
	ob_end_clean();
	header('Content-Encoding: none');
      header('Content-Type: application/octet-stream');
      // header('Content-Type: applicationnd.ms-excel;charset=GB2312');
      header('Content-Disposition: attachment;filename="'.$filename.'.csv"');
      header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
      header("Content-Type: application/force-download");
      $result = mb_convert_encoding($result, "GB2312", "UTF-8");
      echo $result;	
}

你可能感兴趣的:(excel表格)