php到excel - 德国Umlaute [英] php to excel - german Umlaute

查看:84
本文介绍了php到excel - 德国Umlaute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改这个课程来支持德语Umlaute(äöüÄÖÜ)?
我尝试使用utf8_encode($ array)没有成功。还尝试更改内容传输编码:二进制到utf8

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

感谢您指向正确的方向。

Thanks for pointing me in the right direction.

更新:或者这个代码是破坏Umlaute的原因?

UPDATE: OR what of this code would be the reason that destroys the Umlaute?

    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, $v1 );
                $nn1++;
                }
            $tr1++;
            }

        $file .= $this->xlsEOF(); //çàêàí÷èâàåì ñîáèðàòü
        return $file;
        }

        public function LoadFile($array,$filename)
        {
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream');
        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", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
        }

        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 = strlen($Value);
        return pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L).
            $Value;
        }
    }

$NY_excel_simple = new NYOS_ExcelWriter_Simple();


推荐答案

好的,这是非常初步的。可能太简单了。

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

首先:
如果您仅为BIFF文件提供工作表,则应使用BIFF4。所以你可以在Excel中打开时避免出现这样的警告。

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.

由于旧的Excel BIFF格式使用Windows编码而不是Unicode,事实上这个变音符的问题。所以以下内容应该是有效的:

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");

但Windows-1252中没有字形的字符将无法进行。

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

BIFF8支持Unicode。但这不能以这种方式使用。

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

这篇关于php到excel - 德国Umlaute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆