Home > Backend Development > PHP Tutorial > Introduction to how to export excel with php and solve the problem of garbled characters

Introduction to how to export excel with php and solve the problem of garbled characters

WBOY
Release: 2016-07-25 08:58:57
Original
1196 people have browsed it
  1. header("Content-type:application/vnd.ms-excel");
  2. header("Content-Disposition:attachment;filename=export_data.xls");
  3. echo "Name" ."t";
  4. echo "traditional"."t";
  5. echo "blog"."t";
  6. echo "n";
  7. echo "jason"."t";
  8. echo "@"."t" ;
  9. echo "javaeye"."t";
  10. ?>
Copy code

You can consider adding a character set to the header header("Content-type:application/vnd.ms-excel;charset=UTF-8");

Question: This only tells the browser what character set to choose for viewing. In the end, my requirement is to generate an xls file. Some friends suggest using iconv to transcode.

  1. echo iconv("current encoding","GB18030","this blog comes from javaeye, by jason");
Copy code

Question: In this way, the Chinese character encoding in the file is GB18030, but How does Excel know which encoding to use to open it? It can only rely entirely on the OS default. But if you do this when encountering Traditional BIG5, the code will still be garbled.

Finally, adopt the approach of phpMyAdmin and use HTMLExcel.

  1. xmlns:x="urn:schemas-microsoft-com:office:excel"
  2. xmlns="http: //www.w3.org/TR/REC-html40">
  3. < ;td class=xl2216681 nowrap>Robbin can spit
  4. 1234
    5678javaeye website< ;/td>
  • Copy the code

    In this way, you can echo directly, There is no need for iconv transcoding, as long as the Content-type in HTML is set (UTF-8 is used here), of course the header still needs to be added:

    1. header("Content-type:application/vnd.ms-excel");
    2. header("Content-Disposition:attachment;filename=export_data.xls");
    Copy the code

    That's it That's it. Friends who are interested, please test it quickly and see if there is no longer a problem of garbled characters when exporting Chinese content to Excel? !



    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