 |
php เอ็กเซล การ export file to excel ให้อ่าน font ภาษาไทย (Thai language) บน server |
|
 |
|
|
 |
 |
|
พี่ๆครับ
ถ้าผมกำหนด character set =utf-8 แล้วใน code php แต่พออัพขึ้น server ( Free BSD) แล้วสั่ง export file to excel แล้ว มันไม่อ่านฟิลด์ที่เป็นภาษาไทยเลยอะครับ
ผมต้องปรับแก้อะไรที่บน server ด้วยมั๊ยครับ ทำไมมันไม่อ่านภาษาไทยให้ผมอะคร๊าบบบบ
อันนี้โค้ดหน้าที่ให้ export เป็น excel ครับ
Code (PHP)
<?
ob_start();
session_start();
require( "../initial.inc.php" );
mysql_query("SET character_set_results=utf-8");
//mysql_query("SET collation_connection = UTF8-UNICODE-CI", $this->MyConn);
mysql_query("SET NAMES 'utf-8'");
require_once (SECOUND_PATH . CLASS_PATH . "class.writeexcel_workbook.inc.php");
require_once (SECOUND_PATH . CLASS_PATH . "class.writeexcel_worksheet.inc.php");
$token = md5( uniqid(rand(), true ) );
$fname= "../tmp/$token.xls";
$workbook =& new writeexcel_workbook($fname);
$worksheet =& $workbook->addworksheet("customer");
$header =& $workbook->addformat();
$header->set_font("tahoma");
$header->set_bold();
$header->set_align('center');
$header->set_size(12);
$header->set_color('navy');
$header->set_fg_color('silver');
# fix Row 1
$worksheet->freeze_panes(1, 0);
$StrSql = "select C.`CustCode`, C.`CompanyName_EN`, C.`CompanyName_TH`, C.`CustPhone`
from $G_table C
LEFT JOIN business_type B ON B.BTypeID = C.BTypeID
LEFT JOIN customer_type T ON T.CTypeID = C.CTypeID
$_SESSION[CUSTCond]
order by C.`CustCode` asc";
$result=mysql_query($StrSql) or die(mysql_error() . ": " . $StrSql);
$worksheet->write(0, 0, "Code", $header);
$worksheet->write(0, 1, "Company name(EN)", $header);
$worksheet->write(0, 2, "Company name(TH)", $header);
$worksheet->write(0, 3, "Phone", $header);
$worksheet->set_column('A:B', 5);
$worksheet->set_column('B:C', 30);
$worksheet->set_column('C:D', 25);
$worksheet->set_column('D:E', 15);
$format1 =& $workbook->addformat();
$format1->set_align('left');
$format1->set_size(11);
$format1->set_font("tahoma");
$columns = 4;
$i=0;
while($row = mysql_fetch_array($result)){
for($j=0; $j<$columns; $j++){
$worksheet->write($i+1, $j, $row[$j], $format1);
}
$i++;
}
$workbook->close();
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=".basename("$table.xls").";");
header("Content-Transfer-Encoding: binary ");
header("Content-Length: ".filesize($fname));
readfile($fname);
unlink($fname);
exit();
?>
อันนี้ output ตอนเป็น excel แล้วครับ

***ถ้ากระทู้ยาวไป ต้องขอโทษด้วยครับ**
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-06-16 14:17:33 |
By :
maruk |
View :
43719 |
Reply :
18 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มะมีใครตอบเลยแหง่ะ เศร้า
|
 |
 |
 |
 |
Date :
2010-06-17 08:26:03 |
By :
maruk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยน charset เป็น tis620 ตรงด้านบนของโค้ดดูครับ
ผมเคยทำ แบบนี้แหล่ะ
แก้ปัญหาเฉพาะหน้าไปก่อน
ในเรื่องภาษาไทยใน Excel
ไม่เข้าใจเหมือนกันว่าทำไมต้องเปลี่ยนเป็น tis620 ทั้งๆ ที่ ข้อมูลใน db ก็เป็น utf8
|
 |
 |
 |
 |
Date :
2010-06-17 08:26:34 |
By :
yomaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปัญหานี้ผมก็เป็นยังแก้ไม่ได้เลยครับ เพราะว่าผม export มาจากเมลเป็น csvมันไม่แสดงภาษาไทยให้เซ็งเป็ด
|
 |
 |
 |
 |
Date :
2010-06-17 08:44:59 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคุณ YoYo ครับ แต่ว่าผมลองทำตามแล้ว ก็ยังอ่านภาษาไทยไม่ได้เหมือนเดิมครับ เศร้าเหมือนเดิมครับ
|
 |
 |
 |
 |
Date :
2010-06-17 08:49:32 |
By :
maruk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีพี่คนนึงเค้าตอบไว้ให้ในอีกกระทู้แบบนี้ ว่าแต่พอผมใช้ มันก็ error ถ้าผมจะใช้ fopen ผมต้องใช้แบบนี้ป่าวครับ
Code (PHP)
$fXls= "../tmp/$token.xls";
$fname = fopen($fXls,"w");
อันนี้โค้ดที่พี่เค้าให้ไว้ครับ
Code (PHP)
<?
$report='
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<table border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td>สวัสดีครับ</td>
</tr>
<tr>
<td>สบายดีไหม</td>
</tr>
</table> ';
echo $report ;
$filexls = fopen("export_xls.xls","w");
fputs($filexls,$report);
/*$fileword = fopen("export_doc.doc","w");
fputs($fileword,$report);*/
?>
|
 |
 |
 |
 |
Date :
2010-06-17 11:37:27 |
By :
maruk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบ No. 5 ผมตอบไว้เองนะ
แบบนี้ก็ได้ แล้วดู $token ด้วยนะ ต้องมีค่าด้วยนะ
Code (PHP)
<?
$fXls= "../tmp/$token.xls";
$filexls = fopen($fXls,"w");
fputs($filexls,$report);
?>
|
 |
 |
 |
 |
Date :
2010-06-17 13:51:51 |
By :
heng |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมยังทำไม่ได้เลยครับ พี่น้องคร๊าบบบ
|
 |
 |
 |
 |
Date :
2010-07-05 11:43:51 |
By :
maruk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนดูครับ ได้ไม่ได้อย่างไรขออภัยครับ
Code (PHP)
while($row = mysql_fetch_array($result)){
for($j=0; $j<$columns; $j++){
$worksheet->write($i+1, $j, iconv('UTF-8','TIS-620',$row[$j]), $format1);
}
$i++;
}
|
 |
 |
 |
 |
Date :
2010-07-05 11:52:03 |
By :
ไวยวิทย์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี่ครับ
Code (PHP)
mysql_query("SET NAMES TIS620");// Set Char
ได้ชัวครับ ผมใช้อยู่
|
 |
 |
 |
 |
Date :
2010-07-05 11:54:11 |
By :
50121680 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คุณ Gusto ครับ ผมลองทำแล้วอัพขึ้น service มันก็อ่านไทยไม่ได้เหมือนเดิมครับ
ส่วนแบบของคุณ Nico ผมยังไม่ได้ลอง แต่พอดีไปเจอบทความในเว็บนึง ผมลองเอามาใช้ พระเจ้า!!
ใช้ได้จริงครับ ผมเลยเอามาแบ่งปันเผือใครมีปัญหาเหมือนผมครับ
ต้องขอขอบคุณผู้เขียนบทความนี้เป็นอย่างสูงเลยครับ http://www.memo8.com/toolkits/archives/78
Code (PHP)
<?php
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="MyXls.xls"');#ชื่อไฟล์
?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<HTML>
<HEAD>
<meta http-equiv="Content-type" content="text/html;charset=tis-620" />
</HEAD><BODY>
<TABLE x:str BORDER="1">
<TR>
<TD><b>AAA</b></TD>
<TD><b>AAA</b></TD>
<TD><b>AAA</b></TD>
</TR>
<TR>
<TD>ภาษาไทย</TD>
<TD>ภาษาไทย</TD>
<TD>ภาษาไทย</TD>
</TR>
</TABLE>
</BODY>
</HTML>
|
 |
 |
 |
 |
Date :
2010-07-07 15:05:54 |
By :
maruk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ No.10 มาก ๆ ค่ะ พบปัญหา เหมือนกัน ลองแล้วใช้ได้เลยค่ะ ^_______^
|
 |
 |
 |
 |
Date :
2011-07-06 11:27:35 |
By :
Oiler.Com |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษค่ะ อัพรูปผิด จะลบ รูปโตโต้ ออกยังไงค่ะ
|
 |
 |
 |
 |
Date :
2011-07-06 11:29:38 |
By :
Oiler.Com |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้ายังไม่ได้อาจจะต้องใช้ iconv() ครับ 
|
 |
 |
 |
 |
Date :
2011-07-06 11:58:57 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ต้องแก้อะไรให้ยุ่งยาก ถ้า charset เป็น utf-8 อยู่แล้ว
ง่ายๆ แค่ ..
เปิด text editor
Save as..
แก้ Encoding เป็น ANSI
อัพโหลด จบ.
|
 |
 |
 |
 |
Date :
2012-07-26 14:31:13 |
By :
pchoti |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมยังไม่ขึ้นเลย ครับ ไม่สามารถ อ่านภาษาไทยได้
|
 |
 |
 |
 |
Date :
2014-09-11 17:02:37 |
By :
wahahaboy |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$fp = fopen($fileName, 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
foreach ($list as $key=>$fields) {
fputcsv($fp, $fields);
}
fclose($fp);
|
 |
 |
 |
 |
Date :
2015-02-19 17:00:18 |
By :
Tatum |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
putenv("NLS_LANG=AMERICAN_AMERICA.TH8TISASCII");
header("Content-Type:........
ลองใส่ putenv("NLS_LANG=AMERICAN_AMERICA.TH8TISASCII ไว้บนสุดก่อน header
ดูค่ะ แก้มาเกือบเดือน เพิ่งออก
|
 |
 |
 |
 |
Date :
2016-12-09 15:19:39 |
By :
^^ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|