Example of exporting Chinese content excel files using php

Source: Internet
Author: User
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"
    ";} Echo'
   '; Foreach ($ datalist as $ k => $ v) {echo'
   
    
'; Foreach ($ v as $ key => $ val) {if (is_numeric ($ val) & strlen ($ val) >=14) {echo"
    "; // Convert a number greater than 14 characters into a string output (such as ID card)} else {echo"
    ";}} Echo'
   ';} Echo'
  
". $ Val ."
". $ Val ."". $ Val ."
'; 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.