php to excel - german Umlaute

前端 未结 2 464
暗喜
暗喜 2021-01-23 02:24

How can I alter this class to support German Umlaute (äöüÄÖÜ)? I tried to use utf8_encode($array) without succes. Also tried to change Content-Transfer-Encoding: binary to utf8<

相关标签:
2条回答
  • 2021-01-23 02:50
    foreach($v as $k1 => $v1 )
    {
        $file .= $this->xlsWriteLabel( $tr1, $nn1, mb_convert_encoding($v1, 'UTF-16LE', 'UTF-8') );
        $nn1++;
    }
    

    You can also convert it this way.

    0 讨论(0)
  • 2021-01-23 03:03

    OK, this is very rudimentary. Maybe too much simplified.

    First: You should use BIFF4 if you only serve a Worksheet with a BIFF file. So you will avoid the warnings while opening in Excel.

    The problem with the umlauts results from the fact that the older Excel BIFF formats uses Windows encodings rather than Unicode. So the following should work:

        class NYOS_ExcelWriter_Simple
        {
    
            public function CreatFile($array)
            {
            $file = $this->xlsBOF();
            $tr1 = 0;
    
                foreach($array as $k => $v )
                {
                $nn1 = 0;
                    foreach($v as $k1 => $v1 )
                    {
                    $file .= $this->xlsWriteLabel( $tr1, $nn1, mb_convert_encoding($v1, "Windows-1252", "auto") );
                    $nn1++;
                    }
                $tr1++;
                }
    
            $file .= $this->xlsEOF();
            return $file;
            }
    
            public function LoadFile($array,$filename)
            {
            header('Content-Type: application/download');
            header('Content-Disposition: attachment;filename='.$filename); 
            header('Content-Transfer-Encoding: binary '); 
            echo $this->CreatFile($array);
            }
    
            private function xlsBOF()
            {
            return pack("ssssss", 0x409, 0x8, 0x0, 0x10, 0x0, 0x0); //using BIFF4
            }
    
            function xlsEOF()
            {
            return pack("ss", 0x0A, 0x00);
            }
    
            private function xlsWriteNumber($Row, $Col, $Value)
            {
            return pack("sssss", 0x203, 14, $Row, $Col, 0x0).
                pack("d", $Value);
            }
    
            function xlsWriteLabel($Row, $Col, $Value)
            {
            $L = mb_strlen($Value);
            return pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L).
                $Value;
            }
        }
    
    $NY_excel_simple = new NYOS_ExcelWriter_Simple();
    
    $array = array(
                   array("foo", "bar", "hello", "world"),
                   array("für", "bär", "hellö", "wörld"),
             );
    
    $NY_excel_simple->LoadFile($array, "test.xls");
    

    But characters without glyphs in Windows-1252 will not be possible.

    Unicode is supported in BIFF8. But this can't be used in this manner.

    0 讨论(0)
提交回复
热议问题