Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > เกิดปัญหาการเขียน PHP Export Database to Excel คือว่าดิฉันได้เอาตัวอย่างหัวข้อ PHP Export Database to Excel (Excel.Application)



 

เกิดปัญหาการเขียน PHP Export Database to Excel คือว่าดิฉันได้เอาตัวอย่างหัวข้อ PHP Export Database to Excel (Excel.Application)

 



Topic : 043620



โพสกระทู้ ( 43 )
บทความ ( 0 )



สถานะออฟไลน์




คือว่าดิฉันได้เอาตัวอย่างหัวข้อ PHP Export Database to Excel (Excel.Application) ตาม link นี้ https://www.thaicreate.com/php/php-export-database-to-excel.html ไปรันดูสามารถรันได้
แต่ปัญหาของดิฉันคือว่าโปรเจคที่ทำนั้นต้องมีการค้นหาข้อมูลจาก database ตามเงื่อนไขก่อนจากนั้นค่อย Export เป็น Excel ตามเงื่อนไขนั้น แต่ดิฉันติดปัญหาตรงที่ว่าได้ลองเขียนโดยเอา code Export Database To Excel นี้ไปใส่ในหน้าเดียวกับหน้าค้นหา ทำให้เวลาค้นหาจะเจอค่าแรกสุดเพียงค่าเดียว แต่เวลา Export เป็น Excel แล้วจะขึ้นค่านั้นซ้ำตามจำนวนจริงที่ค้นหาได้สมมติว่าในหน้าค้นหา ค้นหาได้100 เรคคอร์ด แต่จะแสดงจริงๆแค่เรคคอร์ทแรกเรคคอร์ทเดียวแต่พอExport เป็น Excel จะแสดงผลเรคคอร์ดแรกซ้ำ100 บรรทัด ต่อมาดิฉันจึงคิดวิธีการใหม่โดยการทำแยกหน้ากับหน้าค้นหาโดยส่งเงื่อนไขการค้นหาเป็นตัวแปรsessionไปหน้า phpExportDatabaseToExcel.php แต่ว่าติดปัญหาตรงที่บรรทัดExcel Created <a href="<?=$strFileName?>">Click here</a> to Download. ของหน้า phpExportDatabaseToExcel.php จะทำอย่างไรให้สามารถExport ไปโดยทีเดียวโดยคลิ๊ก Export ที่หน้าค้นหาแล้วไปทำbackend ที่ phpExportDatabaseToExcel.php โดยที่เงื่อนไขที่ส่งจากหน้าค้นหาก็สามารถไปกำหนดข้อมูลใน phpExportDatabaseToExcel.php เพื่อ Export เป็น Excel ได้ค่ะ ดิฉันทำมา 2 วันแล้วยังไม่ได้เลยค่ะ รบกวนท่านผู้รู้ช่วยทีนะคะ ขอบคุณค่ะ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-06-04 09:03:46 By : tiggerOne View : 3219 Reply : 8
 

 

No. 1



โพสกระทู้ ( 9 )
บทความ ( 0 )



สถานะออฟไลน์


ผมลองใช้สคริปต์จาก thaicreate.com ก็ใช้ได้แต่ต้องระบุชื่อฟิลด์ในตาราง สคริปต์นี้สั้นกว่าตรงที่ไม่ต้องระบชื่อฟิลด์....ดึงเอาข้อมูลออกมาจากตารางตามเงื่อนไข แล้วส่งออกเป็นไฟล์ Excel ก็โอเคน่ะครับ ลองแก้ไขใน 3 ส่วน ที่ต้องการให้ตรงกับงานของคุณ

<?
/*
Conditioned search and export to Excel file
source : http://snipplr.com/view.php?codeview&id=16324
*/

//----------- แก้ไข ส่วนที่ 1-------------------------
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxxx";
$dbname = "xxxxx";
$dbtable = "xxxxxx";
//-----------------------------------------------------

function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname );

// ------------------ แก้ไข ส่วนที่ 2-------------------------------------------------
$q = "SELECT * FROM ".$dbtable." WHERE INSTOCK < 6";
// -------------------------------------------------------------------------------------

mysql_query("SET CHARACTER SET tis620");
$qr = mysql_query( $q ) or die( mysql_error() );

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=instock_lower5_".$dbtable.".xls "); // <==แก้ไข ส่วนที่ 3
header("Content-Transfer-Encoding: binary ");
xlsBOF();
$col = 0;
$row = 0;
$first = true;

while( $qrow = mysql_fetch_assoc( $qr ) )
{
if( $first )
{
foreach( $qrow as $k => $v )
{
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
$col = 0;
$row++;
$first = false;
}

foreach( $qrow as $k => $v )
{
xlsWriteLabel( $row, $col, $v );
$col++;
}
$col = 0;
$row++;
}

xlsEOF();
exit();
?>

ได้ผลเป็น

เปิด Excel






Date : 2010-06-04 15:11:28 By : aacha46
 


 

No. 2



โพสกระทู้ ( 43 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณคุณaacha มากค่ะตอนนี้เอา code ไปลองในdatabase ทดสอบดูเกิดปัญหาที่บรรทัด

Code
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=instock_lower5_".$dbtable.".xls "); // <==แก้ไข ส่วนที่ 3
header("Content-Transfer-Encoding: binary ");


จะฟ้อง error ตามข้างล่างค่ะ

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 53

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 55

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 56

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 57

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 58

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 59

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 60
Date : 2010-06-04 15:49:08 By : tiggerOne
 

 

No. 3



โพสกระทู้ ( 9 )
บทความ ( 0 )



สถานะออฟไลน์


เป็น code ที่ผมใช้ทำงานบน server อยู่ทุกวัน ไม่มี error เช่นนั้น ลองดูต้นฉบับที่ http://snipplr.com/view.php?codeview&id=16324
Date : 2010-06-07 15:15:26 By : aacha46
 


 

No. 4



โพสกระทู้ ( 9 )
บทความ ( 0 )



สถานะออฟไลน์


ใส่ ob_start(); ไว้บรรทัดแรก ลองรันดูได้ผลไม่แตกต่างจาก No.1 ทั้งบน localhost/Server หาก error อีกเห็นท่าต้องพึ่ง MR.WIN ครับ
Date : 2010-06-07 15:31:55 By : aacha46
 


 

No. 5



โพสกระทู้ ( 43 )
บทความ ( 0 )



สถานะออฟไลน์


หลังจากที่ติดปัญหา Error ตามโค๊ดของคุณ aarcha ดิฉันก็เลยกลับไปลองแก้โปรเจคของดิฉันตามตัวอย่าง" PHP Export Database to Excel (Excel.Application)" ตาม link นี้ https://www.thaicreate.com/php/php-export-database-to-excel.html ตอนนี้ดิฉันสามารถ Export ไฟล์ได้แล้วค่ะ แต่ตอนนี้ติดปัญหาอยู่อย่างนึงคือว่า เวลาที่ Shut Down เครื่อง เครื่องจะฟ้องว่า คุณจะ Save ไฟล์ Book1หรือเปล่า ถ้าหากว่าในวันนี้ดิฉัน Export ไฟล์ Excelจำนวน 70 ไฟล์ เวลาที่ Shut Down ดิฉันต้องกด Cancle 70ครั้ง สำหรับ Book1-Book70 ไม่ทราบว่าเป็นเพราะอะไร ทำอย่างไรไม่ให้ขึ้นฟ้องแบบนี้ เพราะว่าต้องเวลากด Cancle ตั้งหลายครั้งกว่าจะ Shut down เครื่องได้ รบกวนท่านผู้รู้ทั้งหลายแนะนำทีคะ
Date : 2010-06-10 08:53:54 By : tiggerOne
 


 

No. 6

Guest


Code

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=instock_lower5_".$dbtable.".xls "); // <==แก้ไข ส่วนที่ 3
header("Content-Transfer-Encoding: binary ");


จะฟ้อง error ตามข้างล่างค่ะ

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 53

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 54

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 55

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 56

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 57

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 58

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 59

Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\export\export.php:12) in D:\AppServ\www\export\export.php on line 60





header("Content-Disposition: attachment;filename=instock_lower5_".$dbtable.".xls "); // <==แก้ไข ส่วนที่ 3
ส่วนนี้ครับลองใส่ชื่อ "ใส่ชื่อfile.xls ลองดูครับ
Date : 2010-11-01 10:43:37 By : twinmafia
 


 

No. 7

Guest


<?php
ob_start();
?>

บนสุด
<html>
.
.
.
.
.
</html>
ล่างสุด

<?php
ob_end_flush();
?>

ช่วยได้ครับ
Date : 2011-02-26 19:54:20 By : ^__^
 


 

No. 8



โพสกระทู้ ( 1 )
บทความ ( 0 )



สถานะออฟไลน์


ลองใช้แล้วเป็นสคริปส์ ที่เจ๋งมากคับ...

สั้น ๆ แต่ชัดเจน
Date : 2011-06-25 18:26:42 By : dynaz
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เกิดปัญหาการเขียน PHP Export Database to Excel คือว่าดิฉันได้เอาตัวอย่างหัวข้อ PHP Export Database to Excel (Excel.Application)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่