如何更改此课程以支持德语Umlaute(äöüÄÖÜ)? 我尝试使用utf8_encode($ array)而没有成功。还尝试将Content-Transfer-Encoding:binary更改为utf8
感谢您指出我正确的方向。
更新:或者这段代码是什么破坏了变形金刚?
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();
答案 0 :(得分:2)
第一: 如果您只提供带有BIFF文件的工作表,则应使用BIFF4。因此,在Excel中打开时,您将避免出现警告。
变音符号的问题源于旧的Excel BIFF格式使用Windows编码而不是Unicode。所以以下内容应该有效:
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中没有字形的字符将无法使用。
BIFF8支持Unicode。但这不能以这种方式使用。
答案 1 :(得分:0)
foreach($v as $k1 => $v1 )
{
$file .= $this->xlsWriteLabel( $tr1, $nn1, mb_convert_encoding($v1, 'UTF-16LE', 'UTF-8') );
$nn1++;
}
您也可以这样转换。