【问题标题】:Wrong encode string when export the excel using PHP使用 PHP 导出 excel 时编码字符串错误
【发布时间】:2016-03-21 08:22:24
【问题描述】:

目前我有一个用于导出到 excel 的类,它非常适合英语,但是,在其他编码中,例如汉字编码错误

https://github.com/bcit-ci/CodeIgniter/wiki/Export-to-Excel-2013

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * Excel library for Code Igniter applications
 * Based on: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
 * Tweaked by: Moving.Paper June 2013
 */

class Export {

    function to_excel($array, $filename) {
        header('Content-type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename=' . $filename . '.xls');

        //Filter all keys, they'll be table headers
        $h = array();
        foreach ($array->result_array() as $row) {
            foreach ($row as $key => $val) {
                if (!in_array($key, $h)) {
                    $h[] = $key;
                }
            }
        }
        //echo the entire table headers
        echo '<table><tr>';
        foreach ($h as $key) {
            $key = ucwords($key);
            echo '<th>' . $key . '</th>';
        }
        echo '</tr>';

        foreach ($array->result_array() as $row) {
            echo '<tr>';
            foreach ($row as $val)
                $this->writeRow($val);
        }
        echo '</tr>';
        echo '</table>';
    }

    function writeRow($val) {
        echo '<td>' . utf8_decode($val) . '</td>';
    }

}

尝试添加

header("Content-type: text/html; charset=utf-8");

在函数的开始但没有运气

非常感谢您的帮助。

【问题讨论】:

    标签: php excel codeigniter csv encoding


    【解决方案1】:

    使用 PHPExcel 代替自定义类,完美运行

    $this->load->library('PHPExcel');
            $this->load->library('PHPExcel/IOFactory');
    
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->getProperties()->setTitle("Trial Report");
    
            // Assign cell values
            $objPHPExcel->setActiveSheetIndex(0);
    
            //Filter all keys, they'll be table headers
            $h = array();
            foreach ($result as $row) {
                foreach ($row as $key => $val) {
                    if (!in_array($key, $h)) {
                        $h[] = $key;
                    }
                }
            }
    
            //Writing the first header row
            foreach ($h as $key => $val) {
                $val = ucwords($val);
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($key, 1, $val); //first row is 1
            }
    
            $pos = 0;
            foreach ($result as $r_key => $row) {
                foreach ($row as $col) {
                    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($pos, ($r_key + 2), $col); //skip the first row
                    $pos++;
                }
                $pos = 0;
            }
    
            $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
    
            header('Content-type: application/vnd.ms-excel');
            header('Content-Disposition: attachment; filename="export.xls"');
            $objWriter->save('php://output');
    

    【讨论】:

      【解决方案2】:

      繁体中文(Big5)

      header("Content-type: text/html; charset=big5");
      

      还有一些:

      Chinese Simplified (EUC)
          ##charset=EUC-CN 
      Chinese Simplified (GB2312)
          ##charset=gb2312 
      Chinese Simplified (HZ)
          ##charset=hz-gb-2312 
      Chinese Simplified (Mac)
          ##charset=x-mac-chinesesimp 
      Chinese Traditional (Big5)
          ##charset=big5 
      Chinese Traditional (CNS)
          ##charset=x-Chinese-CNS 
      Chinese Traditional (Eten)
          ##charset=x-Chinese-Eten 
      Chinese Traditional (Mac)
          ##charset=x-mac-chinesetrad 
          ##charset=950 
      

      here is some fixing details and how to tut ...

      【讨论】:

      • 感谢您的帮助。添加但没有运气,仍然显示“??”汉字
      • 兄弟,试试我添加的所有中文字符集,你可能会在这些中添加一些其他中文字符集
      • 从我添加的链接中阅读,它有关于字符集使用的大量教程
      • link :chinesetop100.com header : 成功了,有 7/8 charset try all for it.no utf8 不会
      • 是的,有一种方法可以检查,phpmyadmin允许以各种格式导出,导出为ex​​cel并查看结果
      猜你喜欢
      • 2018-07-23
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多