This article describes how to export an excel file containing Chinese characters in php. The example shows how to export an Excel file containing Chinese characters in php, for more information, see the example in this article. Share it with you for your reference. The details are as follows:
<? Php class toExcel {public $ link = null; function _ construct () {}/************************************* ************************************* $ mapping: array format header information $ map = array ('no', 'name', 'Email ', 'age'); * $ datalist: result set found by the database * $ fileName: excel file name * return: ************************************* * ***********************************/public function toExcel ($ mapping, $ datalist, $ fileName) {header ("Content-type: application/vnd. ms-excel "); header (" Content-Disposition: filename = ". iconv ('utf-8', 'gb2312 ', $ fileName ). ". xls "); echo'
'; Echo'
'; Echo'
'; If (is_array ($ mapping) {foreach ($ mapping as $ key => $ val) echo"
| ". $ Val ." | ";} Echo'
'; Foreach ($ datalist as $ k => $ v) {echo'
'; Foreach ($ v as $ key => $ val) {if (is_numeric ($ val) & strlen ($ val) >=14) {echo"
| ". $ Val ." | "; // Convert a number greater than 14 characters into a string output (such as ID card)} else {echo"
". $ Val ." | ";}} Echo'
';} Echo'
'; Echo''; Echo'';}}$ Map = array ('no', 'name', 'Email'); $ datal = array (1, 'admin ', 'admin @ 163.com '), array (2, 'Member', 'Member @ 163.com '); $ csv = new toExcel; $ csv-> toExcel ($ map, $ datal, "dataexport");?>
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 = "Chinese test"; $ 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 "; ?>
I hope this article will help you with php programming.