ส่งข้อมูลไฟล์ .php ไปยัง Excel เป็นภาษาต่างด้าวแก้ยังไงครับ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
nott
PHP Newbie
PHP Newbie
โพสต์: 7
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

ส่งข้อมูลไฟล์ .php ไปยัง Excel เป็นภาษาต่างด้าวแก้ยังไงครับ

โพสต์ที่ยังไม่ได้อ่าน โดย nott »

เป็นเฉพาะภาษาไทยครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ส่ง อะไร ยังไง ครับ
ขอรายละเอียด หน่อย ครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
nott
PHP Newbie
PHP Newbie
โพสต์: 7
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย nott »

คือ ผมต้องการ แสดงข้อมูลจากฐานข้อมูลที่ใช้phpเขียน ให้ออกมาในรูปของ Excel ผมใช้ spreadsheet ของ excel ข้อมูลที่เป็นภาษาอังกฤษ สามารถแสดงผลได้แต่ข้อมูลที่เป็นภาษาไทยแสดงออกเป็นภาษาต่างด้าวอ่ะคับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ลอง เอา โค้ด ที่ แหลง เป็น .xls มาดู ซิครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
nott
PHP Newbie
PHP Newbie
โพสต์: 7
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย nott »

<?php
require_once 'Spreadsheet/Excel/Writer.php';
// Creating a workbook
require_once('../include/constant.php');
function connectDB_ORA($as_dsn) {
$o_db = DB::connect($as_dsn);
if (DB::isError($o_db)) {
echo $o_db->getMessage();}
return $o_db;
}
require_once("DB.php");
$ConnDB = connectDB_ORA(DSN_ORA);
//$DaySQL = "SELECT DISTINCT AD_DATE ,AD_CODE FROM ST_ADTRAN WHERE AD_DATE >='19-DEC-06' and AD_DATE<='19-DEC-06' ";
//$ResultDay = $ConnDB->getAll($DaySQL, DB_FETCHMODE_ASSOC);
$DataSQL = "SELECT AD_BRN , AD_TRCODE,AD_NO , AD_A01QTY,AD_A02QTY,AD_A04QTY,AD_NAME,AD_CODE ,AD_DATE FROM ST_ADTRAN WHERE AD_COMP ='A' AND AD_BRN='212' AND AD_DATE >='1-NOV-06' and AD_DATE<='19-DEC-06' order by AD_NO,AD_CODE ";
$ResultData = $ConnDB->getAll($DataSQL, DB_FETCHMODE_ASSOC);
$ConnDB->disconnect();
$workbook = new Spreadsheet_Excel_Writer();
// sending HTTP headers
$workbook->send('test.xls');
// Creating a worksheet
$worksheet =& $workbook->addWorksheet('My first worksheet');
// The actual data
//Image('../image/ptlogo.jpg', 70, 8, 8);
$w =array('Desel Oil','Bensin 95','bensin 91');
$worksheet->write(0, 0, 'List');
$worksheet->write(0, 1, 'Type');
$worksheet->write(0, 2, 'Count');
for($i=1,$a=1;$i<=count($ResultData) *2;$i++,$a++)
{
$j =0;
//for($j=0;$j<1;$j++)
//{
if ($ResultData[$a-1]['AD_CODE'] =='100001' )
{
$worksheet->write($i, $j, $w[0]);
$worksheet->write($i, $j+1, $ResultData[$a-1]['AD_NAME']);
$worksheet->write($i, $j+2, $ResultData[$a-1]['AD_A01QTY']);
}
if ($ResultData[$a-1]['AD_CODE'] =='100002' )
{
$worksheet->write($i, $j, $w[1]);
$worksheet->write($i, $j+1, $ResultData[$a-1]['AD_NAME']);
$worksheet->write($i, $j+2, $ResultData[$a-1]['AD_A02QTY']);
$worksheet->write($i+1, $j, $w[2]);
$worksheet->write($i+1, $j+1, $ResultData[$a-1]['AD_NAME']);
$worksheet->write($i+1, $j+2, $ResultData[$a-1]['AD_A04QTY']);
$i++;
}
//if ($this->aHeadr['ResultData'][$i-1]['AD_CODE'] =='100002' )
//{
//$worksheet->write($i, $j, $ResultDay[$i-1]['AD_NAME']);
//$worksheet->write($i, $j+1, $ResultDay[$i-1]['AD_A02QTY']);
//}

//}
//$worksheet->write(1, 0, 'John Smith');
//$worksheet->write(1, 1, 30);
//$worksheet->write(2, 0, 'Johann Schmidt');
//$worksheet->write(2, 1, 31);
//$worksheet->write(3, 0, 'Juan Herrera');
//$worksheet->write(3, 1, 32);

}
// Let's send the file
$workbook->close();
?>


ถ้าผมเปลี่ยน ค่า desel->ดีเซล มันก็จะแสดงเป็นภาษาต่างด้าวอ่ะคับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

อันนี้ เป็น class ที่ เอา ของคนอื่นมา อีกทีใช่มั๊ยคับ
ต้อง ลอง ดู คู่ มี อธิบาย เกี่ยวกับ การ กำหนด headers นะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ถ้า ไม่มีอธิบายไว้ ลอง ใช้ ของ ที่ นี่ ดูครับ

เครื่องมือสำหรับการสร้างไฟล์ Excel

1. PHP WriteExcel Class สำหรับสร้างไฟล์ Excel ดาวน์โหลดได้จาก http://www.bettina-attack.de/jonny/view ... writeexcel



วิธีการเขียนให้ PHP สามารถ Export ออกมาเป็น Excel

1. เมื่อดาวน์โหลดไฟล์ PHP WriteExcel Class มาแล้วก็ทำการแตกลง Directory ไหนก็ได้นะครับยกตัวอย่างเช่น /home/phpexcel

2. ใน Directory /home/phpexcel ให้เอาแต่ไฟล์ที่มีชื่อขึ้นต้นด้วย class.* มานอกนั้นลบไปได้เลยครับ

3. ในไฟล์ class.writeexcel_worksheet.inc.php ให้ไปหาคำว่า $fh=fopen(tempnam พอเจอแล้ว Comment ด้วย # ด้านหน้าสุดของบรรทัดเลยครับ

4. ในไฟล์ class.writeexcel_worksheet.inc.php ให้หาคำว่า $this->_print_gridlines = 1; ให้แก้เป็น $this->_print_gridlines = 0;

5. วิธีการเขียนโปรแกรมให้ Export ออกมาเป็น Excel



<?

require_once "/home/phpexcel/class.writeexcel_workbook.inc.php";

require_once "/home/phpexcel/class.writeexcel_worksheet.inc.php";

$token = md5(uniqid(rand(), true));

$fname= "../tmp/$token.xls";

$workbook =& new writeexcel_workbook($fname);



$worksheet =& $workbook->addworksheet("ข้อมูลนักศึกษาที่ลงทะเบียน");

$worksheet->set_margin_right(0.50);

$worksheet->set_margin_bottom(1.10);



## Set Format ##

$xlscelldesc_header =& $workbook->addformat();

$xlscelldesc_header->set_font('Angsana New');

$xlscelldesc_header->set_size(18);

$xlscelldesc_header->set_color('black');

$xlscelldesc_header->set_bold(1);

$xlscelldesc_header->set_text_v_align(1);

$xlscelldesc_header->set_merge(1);



$xlsCellDesc =& $workbook->addformat();

$xlsCellDesc->set_font('Angsana New');

$xlsCellDesc->set_size(14);

$xlsCellDesc->set_color('black');

$xlsCellDesc->set_bold(1);

$xlsCellDesc->set_align('left');

$xlsCellDesc->set_text_v_align(1);

## End of Set Format ##



## Set Column Width & Height กำหนดความกว้างของ Cell

$worksheet->set_column('A:B', 2);

$worksheet->set_column('B:C', 4);

$worksheet->set_column('C:D', 11.29);

$worksheet->set_column('D:E', 21);

$worksheet->set_column('E:F', 15);

$worksheet->set_column('F:G', 32);

$celldesc_h = 16.50;



## Writing Data เพิ่มข้อมูลลงใน Cellง

$worksheet->write_blank(A1,$xlscelldesc_header);

$worksheet->write(B1,"ข้อมูลนักศึกษาที่ลงทะเบียน ภาคเรียนที่ $semester ปีการศึกษา $year", $xlscelldesc_header);

$worksheet->write_blank(C1,$xlscelldesc_header);

$worksheet->write_blank(D1,$xlscelldesc_header);

$worksheet->write_blank(E1,$xlscelldesc_header);

$worksheet->write_blank(F1,$xlscelldesc_header);



# กำหนดความสูงของ Cell

$worksheet->set_row(1, $celldesc_h);

$worksheet->set_row(2, $celldesc_h);

$worksheet->set_row(3, $celldesc_h);

$worksheet->set_row(4, $celldesc_h);

$worksheet->set_row(5, $celldesc_h);



$worksheet->write(B4, " รหัสวิชา :", $xlscelldesc_header);

$worksheet->write(D4, " $courseno ", $xlscelldesc_header);



$worksheet->write(B6,"ที่", $xlscelldesc_header);

$worksheet->write(C6,"รหัสนักศึกษา", $xlscelldesc_header);

$worksheet->write(D6,"ชื่อ - นามสกุล", $xlscelldesc_header);

$worksheet->write_blank(E6,$xlscelldesc_header);

$worksheet->write(F6," ", $xlscelldesc_header);

$xlsRow = 7;



# ตรงนี้คือดึงข้อมูลจาก mysql มาใส่ใน Cell

while(list($id,$prename,$name,$sname)=mysql_fetch_row($result)) {

++$i;

$worksheet->set_row($xlsRow, 19.80);

$worksheet->write("B$xlsRow", "$i", $xlsCellDesc);

$worksheet->write("C$xlsRow", "$id", $xlsCellDesc);

$worksheet->write("D$xlsRow", "$prename$name", $xlsCellDesc);

$worksheet->write("E$xlsRow", "$sname", $xlsCellDesc);

$worksheet->write("F$xlsRow", "......................................................................", $xlsCellDesc);

$xlsRow++;

}



# เสร็จแล้วก็ส่งไฟล์ไปยัง Browser ครับแค่นี้ก็เสร็จแล้ว

$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("ชื่อไฟล์.xls").";");

header("Content-Transfer-Encoding: binary\r\n");

header("Content-Length: ".filesize($fname));

readfile($fname);

unlink($fname);

exit();

?>





6. เพียงเท่านี้ท่านก็สามารถทำ Report ที่ Print ออกมาสวยหรูแถมยังเอาไปใช้คำณวณได้ด้วย Excel

7. สำหรับใครต้องการให้แสดงเป็นรูปแบบใดก็สามารถพลิกแพลงได้ตามใจชอบครับ ลองดูตัวอย่างที่ผมทำไว้ใช้งานจริงได้ที่

http://www3.reg.cmu.ac.th/regist246/pub ... =precourse ให้ลองกดตรงปุ่มดาวน์โหลดดูนะครับ จะมีการสร้าง Report หลายรูปแบบเอาไว้ ลองปริ้นออกมาดูแต่ละแบบก็ได้นะครับจะเห็นข้อแตกต่าง คราวหน้าผมจะมาพูดถึงรูปแบบอื่นๆ อีก

ที่มา : บอร์ดไทยแอดมิน
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
nott
PHP Newbie
PHP Newbie
โพสต์: 7
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย nott »

ผมทำตามที่บอกแล้วครับแต่มันยังไม่ได้ มันบอกว่า error fputs($this->_filehandle, $unknown3);ของไฟชื่อ class.writeexcel_olewriter.inc error หลายบรรทัดครับ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

fputs เป็น function ในการเขียนไฟล์ นะครับ คิดว่า อาจจะเป็น ที่ สิทธิ ในการเขียนไฟล์
ลอง เอามาดูก็ได้นะครับ
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
nott
PHP Newbie
PHP Newbie
โพสต์: 7
ลงทะเบียนเมื่อ: 01/01/1970 7:00 am

โพสต์ที่ยังไม่ได้อ่าน โดย nott »

ต้องแก้ตรงอื่นอีกหรือเปล่าครับ
# $fh=fopen($this->_tmpfilename, "w+b");
ข้างบนนี้ผมแก้ถูกหรือเปล่าครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Bing [Bot] และบุคลทั่วไป 58