 |
|
|
 |
 |
|
ผมลองใช้สคริปต์จาก 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();
?>


|
 |
 |
 |
 |
Date :
2010-06-04 15:11:28 |
By :
aacha46 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็น code ที่ผมใช้ทำงานบน server อยู่ทุกวัน ไม่มี error เช่นนั้น ลองดูต้นฉบับที่ http://snipplr.com/view.php?codeview&id=16324
|
 |
 |
 |
 |
Date :
2010-06-07 15:15:26 |
By :
aacha46 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่ ob_start(); ไว้บรรทัดแรก ลองรันดูได้ผลไม่แตกต่างจาก No.1 ทั้งบน localhost/Server หาก error อีกเห็นท่าต้องพึ่ง MR.WIN ครับ
|
 |
 |
 |
 |
Date :
2010-06-07 15:31:55 |
By :
aacha46 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หลังจากที่ติดปัญหา 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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
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 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
ob_start();
?>
บนสุด
<html>
.
.
.
.
.
</html>
ล่างสุด
<?php
ob_end_flush();
?>
ช่วยได้ครับ
|
 |
 |
 |
 |
Date :
2011-02-26 19:54:20 |
By :
^__^ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองใช้แล้วเป็นสคริปส์ ที่เจ๋งมากคับ...
สั้น ๆ แต่ชัดเจน
|
 |
 |
 |
 |
Date :
2011-06-25 18:26:42 |
By :
dynaz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|