PHP operates Excel files with Chinese content and exports files

墨辰丷
Release: 2023-03-31 06:40:02
Original
1402 people have browsed it

This article mainly introduces the PHP operation of Excel files with Chinese content and file export. Interested friends can refer to it. I hope it will be helpful to everyone.

The example of this article describes the export of Chinese content excel file class by php, the details are as follows:

<?php 
class toExcel{ 
 public $link = null; 
 function __construct(){ 
 }  
 /*************************************************************************** 
 * $mapping:数组格式头信息$map=array(&#39;No&#39;,&#39;Name&#39;,&#39;Email&#39;,&#39;Age&#39;); 
 * $datalist:数据库查出来的结果集 
 * $fileName:Excel文件名称 
 * return:Excel格式文件 
 **************************************************************************/ 
 public function toExcel($mapping,$datalist,$fileName) { 
  header("Content-type:application/vnd.ms-excel"); 
  header("Content-Disposition:filename=".iconv(&#39;utf-8&#39;, &#39;gb2312&#39;, $fileName).".xls"); 
  echo&#39;<html xmlns:o="urn:schemas-microsoft-com:office:office" 
  xmlns:x="urn:schemas-microsoft-com:office:excel" 
  xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]"> 
  <head> 
  <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT"> 
  <meta http-equiv=Content-Type content="text/html; charset=UTF-8"> 
  <!--[if gte mso 9]><xml> 
  <x:ExcelWorkbook> 
  <x:ExcelWorksheets> 
  <x:ExcelWorksheet> 
  <x:Name></x:Name> 
  <x:WorksheetOptions> 
  <x:DisplayGridlines/> 
  </x:WorksheetOptions> 
  </x:ExcelWorksheet> 
  </x:ExcelWorksheets> 
  </x:ExcelWorkbook> 
  </xml><![endif]--> 
  </head> 
  <body link=blue vlink=purple leftmargin=0 topmargin=0>&#39;; 
  echo&#39;<table border="0" cellspacing="0" cellpadding="0">&#39;; 
  echo&#39;<tr>&#39;; 
  if(is_array($mapping)) { 
   foreach($mapping as $key=>$val) 
   echo"<td style=&#39;background-color:#09F;font-weight:bold;&#39;>".$val."</td>"; 
  } 
  echo&#39;</tr>&#39;; 
  foreach($datalist as $k=>$v){ 
   echo&#39;<tr>&#39;; 
   foreach($v as $key=>$val){ 
    if(is_numeric($val) && strlen($val)>=14){ 
     echo"<td style=&#39;vnd.ms-excel.numberformat:@&#39;>".$val."</td>"; //大于14位的数字转换成字符串输出(如身份证) 
    }else{ 
     echo"<td>".$val."</td>"; 
    } 
   } 
   echo&#39;</tr>&#39;; 
  } 
  echo&#39;</table>&#39;; 
  echo&#39;</body>&#39;; 
  echo&#39;</html>&#39;; 
 } 
} 
$map=array(&#39;No&#39;,&#39;Name&#39;,&#39;Email&#39;); 
$datal=array(array(1, &#39;管理员&#39;, &#39;admin@163.com&#39;), array(2, &#39;member&#39;, &#39;member@163.com&#39;));; 
$csv=new toExcel; 
$csv->toExcel($map,$datal,"dataexport"); 
?>
Copy after login

Method 2

<?php 
header("Content-Type: application/vnd.ms-execl"); 
header("Content-Disposition: attachment; filename=myExcel.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
/*first line*/ 
$data1= "中文测试"; 
$data1=mb_convert_encoding($data1,"GB2312","UTF-8"); 
echo $data1."\t"; 
echo "world"."\t"; 
echo "\t\n"; 
/*start of second line*/ 
echo "this is second line"."\t"; 
echo "Hi,pretty girl"."\t"; 
echo "\t\n"; 
?>
Copy after login

Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.

Related recommendations:

PHP examples analyze the usage skills of mysql transaction processing

php is implemented based on curl Multi-threaded crawling

php creates linked lists and adds, deletes, updates and traverses linked list nodes

The above is the detailed content of PHP operates Excel files with Chinese content and exports files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!