PHP&XML มีปัญหาส่งออก XML(utf-8) เป็น XLS แล้วไม่เป็นภาษาไทย
แบบโค๊ดด้านล่าง ไม่ทราบว่าต้องแก้ไขยังไงครับ (อยากได้ด่วนครับ กำลังเขียนให้สำนักงาน)
<?
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=XML2EXCEL.xls ");
header("Content-Transfer-Encoding: binary ");
//function
function xlsHeader() {
echo pack( "ssssss" , 0x809 , 0x8 , 0x0 , 0x10 , 0x0 , 0x0 );
return;
}
function xlsClose() {
echo pack( "ss" , 0x0A , 0x00 );
return;
}
function xlsWriteNumber( $Row , $Col , $Value ) {
echo pack( "sssss" , 0x203 , 14 , $Row , $Col , 0x0 );
echo pack( "d" , $Value );
return;
}
function xlsWriteString( $Row , $Col , $Value ) {
$L = strlen( $Value );
echo pack( "ssssss" , 0x204 , 8 + $L , $Row , $Col , 0x0 , $L );
echo $Value;
return;
}
// End Function
xlsHeader();
$xmlfile = "example.xml"; // เป็นไฟล์ UTF-8
$dom = new DOMDocument;
$dom->load($xmlfile);
xlsWriteString( 0 , 0 , 'TITLE' );
xlsWriteString( 0 , 1 , 'NAME' );
xlsWriteString( 0 , 2 , 'LNAME' );
$numrow = 1;
foreach( $dom->getElementsByTagName('DATASET') as $m ) {
$title=$m->getElementsByTagName('TITLE');
$title_value=$title->item(0)->firstChild->nodeValue;
$name=$m->getElementsByTagName('NAME');
$name_value=$name->item(0)->firstChild->nodeValue;
$lname=$m->getElementsByTagName('LNAME');
$lname_value=$lname->item(0)->firstChild->nodeValue;
xlsWriteString( $numrow , 0 , $title_value );
xlsWriteString( $numrow , 1 , $name_value );
xlsWriteString( $numrow , 2 , $lname_value );
$numrow++;
}
xlsClose();
?>
<?
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=XML2EXCEL.xls ");
header("Content-Transfer-Encoding: binary ");
//function
function xlsHeader() {
echo pack( "ssssss" , 0x809 , 0x8 , 0x0 , 0x10 , 0x0 , 0x0 );
return;
}
function xlsClose() {
echo pack( "ss" , 0x0A , 0x00 );
return;
}
function xlsWriteNumber( $Row , $Col , $Value ) {
echo pack( "sssss" , 0x203 , 14 , $Row , $Col , 0x0 );
echo pack( "d" , $Value );
return;
}
function xlsWriteString( $Row , $Col , $Value ) {
$L = strlen( $Value );
echo pack( "ssssss" , 0x204 , 8 + $L , $Row , $Col , 0x0 , $L );
echo $Value;
return;
}
// End Function
xlsHeader();
$xmlfile = "example.xml"; // เป็นไฟล์ UTF-8
$dom = new DOMDocument;
$dom->load($xmlfile);
xlsWriteString( 0 , 0 , 'TITLE' );
xlsWriteString( 0 , 1 , 'NAME' );
xlsWriteString( 0 , 2 , 'LNAME' );
$numrow = 1;
foreach( $dom->getElementsByTagName('DATASET') as $m ) {
$title=$m->getElementsByTagName('TITLE');
$title_value=$title->item(0)->firstChild->nodeValue;
$name=$m->getElementsByTagName('NAME');
$name_value=$name->item(0)->firstChild->nodeValue;
$lname=$m->getElementsByTagName('LNAME');
$lname_value=$lname->item(0)->firstChild->nodeValue;
xlsWriteString( $numrow , 0 , $title_value );
xlsWriteString( $numrow , 1 , $name_value );
xlsWriteString( $numrow , 2 , $lname_value );
$numrow++;
}
xlsClose();
?>
ได้แล้วครับ เพิ่งหาเจอในบทความ
โดยใช้ฟังชั่นแปลง UTF2TIS นี่เอง
function utf2tis( $string )
{
$str = $string;
$res = "";
for ( $i = 0 ; $i < strlen( $str ) ; $i++ )
{
if ( ord( $str[$i] ) == 224 )
{
$unicode = ord( $str[$i+2] ) & 0x3F;
$unicode |= ( ord( $str[$i+1] ) & 0x3F) << 6;
$unicode |= ( ord( $str[$i] ) & 0x0F ) << 12;
$res .= chr( $unicode - 0x0E00 + 0xA0 );
$i += 2;
}
else
{
$res .= $str[$i];
};
};
return $res;
};