php导入时设置不同的编码

程序猿 2021-02-26 21:46:29 1789浏览 加载中

我们经常在导入文件的时候会发现,原本正常的数据,加载到别的地方就发生了乱码,这对于我们的导入使用是非常不方便的。本篇主要针对的是两种编码utf8和gbk,一般来说在这两点上容易出错。下面我们就这两种不同编码的解决方法,分别带来介绍,一起来看看如何设置吧。

php导入到excel-支持utf8和gbk两种编码

1、utf-8编码案例

php导入到excel乱码,是因为utf8编码在xp系统不支持所有utf8编码,转码一下就完美解决了。

codelayui.code

  1. <?php
  2. header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
  3. header('Pragma: public');
  4. header('Expires: 0');
  5. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  6. header('Content-Type: application/force-download');
  7. header('Content-Type: application/octet-stream');
  8. header('Content-Type: application/download');
  9. header('Content-Disposition: attachment;filename=www.niutw.com.xls ');
  10. header('Content-Transfer-Encoding: binary ');
  11. ?>
  12. <?
  13. $filename='php导入到excel-utf-8编码';
  14. filename=iconv('utf-8', 'gb2312',filename=iconv(
  15.  utf−8
  16.  ,
  17.  gb2312
  18.  ,filename);
  19. echo $filename;
  20. ?>

2、gbk编码案例

codelayui.code

  1. <?php
  2. header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
  3. header('Pragma: public');
  4. header('Expires: 0');
  5. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  6. header('Content-Type: application/force-download');
  7. header('Content-Type: application/octet-stream');
  8. header('Content-Type: application/download');
  9. header('Content-Disposition: attachment;filename=www.iiwnet.com.xls ');
  10. header('Content-Transfer-Encoding: binary ');
  11. ?>
  12. <?
  13. $filename='php导入到excel-utf-8编码';
  14. echo $filename;
  15. ?>

访问网站的时候就下载到excel里面,要弄单元格区别的话,用table表格做网页的就可以了。

以上就是php导入时设置不同编码的方法,大家可以就这两种编码分别进行设置,然后在导入表格中,看看是否解决了乱码的问题。

标签: php
最后修改:2025-05-07 20:58:02

非特殊说明,本博所有文章均为博主原创。