ต้องแก้ไข Code อย่างไร จึงจะรองรับการ Export Table เป็น Excel แบบเลือกช่วงวันที่ได้ครับ
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ต้องแก้ไข Code อย่างไร จึงจะรองรับการ Export Table เป็น Excel แบบเลือกช่วงวันที่ได้ครับ
ต้องแก้ไข Code อย่างไร จึงจะรองรับการ Export Table เป็น Excel แบบเลือกช่วงวันที่ได้ครับ
ต้องแก้ไข Code อย่างไร จึงจะรองรับการ Export Table เป็น Excel แบบเลือกช่วงวันที่ได้ เพราะลองเขียน Code ตามตัวอย่างด้านล่างแล้ว Export Table เป็น Excel มาแต่แบบทั้งหมด ไม่มาแบบเลือกช่วงวันที่ครับ
ตัวอย่าง Code มีที่มาจากเว็บด้านล่างครับ
ชุดไฟล์ + Code ทั้งหมดครับ
1. testdata.sql (ชื่อ database คือ testdata ครับ)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | CREATE TABLE `testdata` ( `IDrun` int (100) NOT NULL , `IDrun2` varchar (100) COLLATE utf8_unicode_ci DEFAULT NULL , `IDnumber` varchar (100) COLLATE utf8_unicode_ci DEFAULT NULL , ` Name ` longtext COLLATE utf8_unicode_ci, `Work1` longtext COLLATE utf8_unicode_ci, `Work2` longtext COLLATE utf8_unicode_ci, `MyTime` longtext COLLATE utf8_unicode_ci, `logtime` longtext COLLATE utf8_unicode_ci, `logdate` longtext COLLATE utf8_unicode_ci, `sortdate` longtext COLLATE utf8_unicode_ci, `sortdate2` date NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE =utf8_unicode_ci; INSERT INTO `testdata` (`IDrun`, `IDrun2`, `IDnumber`, ` Name `, `Work1`, `Work2`, `MyTime`, `logtime`, `logdate`, `sortdate`, `sortdate2`) VALUES (39, 'a' , '1' , 'a' , '' , '' , '' , '' , '02-04-2563' , '2563-04-02' , '2020-04-02' ), (36, 'b' , '2' , 'b' , '' , '' , '' , '' , '31-03-2563' , '2563-03-31' , '2020-03-31' ), (44, 'c' , '3' , 'c' , '' , '' , '' , '' , '03-04-2563' , '2563-04-03' , '2020-04-03' ), (58, 'd' , '4' , 'd' , '' , '' , '' , '' , '07-04-2563' , '2563-04-07' , '2020-04-07' ), (57, 'e' , '5' , 'e' , '' , '' , '' , '' , '06-04-2563' , '2563-04-06' , '2020-04-06' ), (66, 'f' , '6' , 'f' , '' , '' , '' , '' , '09-04-2563' , '2563-04-09' , '2020-04-09' ); ALTER TABLE `testdata` ADD PRIMARY KEY (`IDrun`); ALTER TABLE `testdata` MODIFY `IDrun` int (100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67; |
2. index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | <?php $conn = mysqli_connect( "localhost" , "root" , "" , "testdata" ); mysqli_set_charset( $conn , "utf8" ); $post_at = "" ; $post_at_to_date = "" ; $queryCondition = "" ; if (! empty ( $_POST [ "search" ][ "post_at" ])) { $post_at = $_POST [ "search" ][ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_POST [ "search" ][ "post_at_to_date" ])) { $post_at_to_date = $_POST [ "search" ][ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_POST [ "search" ][ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } $sql = "SELECT * FROM testdata " . $queryCondition . " ORDER BY sortdate ASC, logtime ASC" ; $result = mysqli_query( $conn , $sql ); ?> <html> <head> <meta charset= "UTF-8" > <style> * { font-size: 24px; } .table-content{border-top:#CCCCCC 4px solid; width:80%; overflow-x:auto;} .table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;} .table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;} </style> </head> <body><center> <div class = "demo-content" > <form name= "frmSearch" method= "post" action= "" > <p class = "search_input" > <input type= "text" placeholder= "From Date" id= "post_at" name= "search[post_at]" value= "<?php echo $post_at; ?>" class = "input-control" /> <input type= "text" placeholder= "To Date" id= "post_at_to_date" name= "search[post_at_to_date]" style= "margin-left:10px" value= "<?php echo $post_at_to_date; ?>" class = "input-control" /><br> <input type= "submit" name= "go" value= "Search" > <a href = "excel2.php" >Export to Excel</a> </p> <?php if (! empty ( $result )) { ?> <table class = "table-content" > <thead> <tr> <th><div align= "center" >aa </div></th> <th style= "width:22%" ><div align= "center" >bb </div></th> <th><div align= "center" >cc </div></th> <th><div align= "center" >ee </div></th> <th style= "width:15%" ><div align= "center" > date </div></th> <th><div align= "center" >ff </div></th> <th><div align= "center" >gg </div></th> </tr> </thead> <tbody> <?php while ( $row = mysqli_fetch_array( $result )) { ?> <tr> <td><div align= "center" ><?php echo $row [ "IDnumber" ];?></div></td> <td><div align= "left" ><?php echo $row [ "Name" ];?></div></td> <td><div align= "center" ><?php echo $row [ "Work1" ];?></div></td> <td><div align= "center" ><?php echo $row [ "Work2" ];?></div></td> <td><div align= "center" ><?php echo $row [ "logdate" ];?></div></td> <td><div align= "center" ><?php echo $row [ "MyTime" ];?></div></td> <td><div align= "center" ><?php echo $row [ "logtime" ];?></div></td> </tr> <?php } ?> <tbody> </table> <?php } ?> </form> </div> <script> $.datepicker.setDefaults({ dateFormat: 'dd-mm-yy' , showOn: 'button' , buttonImage: "datepicker.png" , buttonImageOnly: false, dayNamesMin: [ 'อา' , 'จ' , 'อ' , 'พ' , 'พฤ' , 'ศ' , 'ส' ], monthNamesShort: [ 'มกราคม' , 'กุมภาพันธ์' , 'มีนาคม' , 'เมษายน' , 'พฤษภาคม' , 'มิถุนายน' , 'กรกฎาคม' , 'สิงหาคม' , 'กันยายน' , 'ตุลาคม' , 'พฤศจิกายน' , 'ธันวาคม' ], changeMonth: true, changeYear: true, beforeShow: function (){ if ($(this).val()!= "" ){ var arrayDate=$(this).val().split( "-" ); arrayDate[2]=parseInt(arrayDate[2])-543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } setTimeout( function (){ $.each($( ".ui-datepicker-year option" ), function (j,k){ var textYear=parseInt($( ".ui-datepicker-year option" ).eq(j).val())+543; $( ".ui-datepicker-year option" ).eq(j).text(textYear); }); },50); }, onChangeMonthYear: function (){ setTimeout( function (){ $.each($( ".ui-datepicker-year option" ), function (j,k){ var textYear=parseInt($( ".ui-datepicker-year option" ).eq(j).val())+543; $( ".ui-datepicker-year option" ).eq(j).text(textYear); }); },50); }, onClose: function (){ if ($(this).val()!= "" && $(this).val()==dateBefore){ var arrayDate=dateBefore.split( "-" ); arrayDate[2]=parseInt(arrayDate[2])+543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } }, onSelect: function (dateText, inst){ dateBefore=$(this).val(); var arrayDate=dateText.split( "-" ); arrayDate[2]=parseInt(arrayDate[2])+543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } }); $( function () { $( "#post_at" ).datepicker(); $( "#post_at_to_date" ).datepicker(); }); </script> </center> </body></html> |
3. excel2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | <?php //คำสั่ง connect db เขียนเพิ่มเองนะ $serverName = "localhost" ; $userName = "root" ; $userPassword = "" ; $dbName = "testdata" ; $conn = mysqli_connect( $serverName , $userName , $userPassword , $dbName ); mysqli_set_charset( $conn , "utf8" ); $strExcelFileName = "testdata.xls" ; header( "Content-Type: application/x-msexcel; name=" $strExcelFileName "" ); header( "Content-Disposition: inline; filename=" $strExcelFileName "" ); header( "Pragma:no-cache" ); $sql = "SELECT * FROM testdata ORDER BY sortdate ASC, logtime ASC" ; $query = mysqli_query( $conn , $sql ); ?> <?php $conn = mysqli_connect( "localhost" , "root" , "" , "testdata" ); mysqli_set_charset( $conn , "utf8" ); $post_at = "" ; $post_at_to_date = "" ; $queryCondition = "" ; if (! empty ( $_POST [ "search" ][ "post_at" ])) { $post_at = $_POST [ "search" ][ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_POST [ "search" ][ "post_at_to_date" ])) { $post_at_to_date = $_POST [ "search" ][ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_POST [ "search" ][ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } $sql = "SELECT * FROM testdata " . $queryCondition . " ORDER BY sortdate ASC, logtime ASC" ; $result = mysqli_query( $conn , $sql ); ?> <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" > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> </head> <body> <strong>testdata</strong><br> <br> <div id= "SiXhEaD_Excel" align=center x:publishsource= "Excel" > <table x:str border=1 cellpadding=0 cellspacing=1 width=50% style= "border-collapse:collapse" > <tr> <th><div align= "center" >aa </div></th> <th><div align= "center" >bb </div></th> <th><div align= "center" >cc </div></th> <th><div align= "center" >ee </div></th> <th><div align= "center" > date </div></th> <th><div align= "center" >ff </div></th> <th><div align= "center" >gg </div></th> </tr> <?php while ( $result =mysqli_fetch_array( $query ,MYSQLI_ASSOC)) { ?> <tr> <td><div align= "center" ><?php echo $result [ "IDnumber" ];?></div></td> <td><div align= "left" ><?php echo " " . $result [ "Name" ];?></div></td> <td><div align= "center" ><?php echo $result [ "Work1" ];?></div></td> <td><div align= "center" ><?php echo $result [ "Work2" ];?></div></td> <td><div align= "center" ><?php echo $result [ "logdate" ];?></div></td> <td><div align= "center" ><?php echo $result [ "MyTime" ];?></div></td> <td><div align= "center" ><?php echo $result [ "logtime" ];?></div></td> </tr> <?php } ?> </table> </div> <script> window.onbeforeunload = function (){ return false;}; setTimeout( function (){window.close();}, 10000); </script> </body> </html> |
4. datepicker.png โหลดได้ที่ Link ด้านล่างครับ
โครงสร้าง File ทังหมด มีดังนี้ครับ

Screenshot 1 ที่อธิบายว่า เลือกช่วงวันที่แล้ว ก่อนที่จะ Export เป็น Excel ครับ

Screenshot 2 ที่อธิบายว่า เมื่อ Export เป็น Excel แล้ว ตารางใน Excel มาแต่แบบทั้งหมด ไม่มาแบบเลือกช่วงวันที่ครับ



คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
แนะนำลองศึกษา library ใหม่ แทนรูปแบบเดิม จะทำให้การออกไฟล์ Excel มีรูปแบบ
ที่ถูกต้องและทันสมัยขึ้น เช่น PHPExcel หรือ PhpSpreadsheet
ส่วนโค้ดตัวอย่าง น่าจะเป็นรูปแบบเก่าแล้ว ยังไงก่อนใช้งาน ควรดูวันที่ของเนื้อหา
ของตัวอย่างประกอบ ก่อนนำมาใช้
โค้ดตัวอย่าง หน้า index.php สำหรับการกรองข้อมูลวันที่ เราสามารถแสดงข้อมูลได้ถูกต้อง
เพราะมีการเลือกวันที่เริ่มต้น และวันที่สิ้นสุด พร้อมกำหนดเงื่อนไขในคำสั่ง sql ของ
ช่วงวันที่เข้าไป ทำให้เราได้ข้อมูลที่ต้องการ
เราส่งข้อมูลเงื่อนไข โดยใช้ method "POST" สำหรับ ฟอร์มตามโค้ดในส่วน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $queryCondition = "" ; if (! empty ( $_POST [ "search" ][ "post_at" ])) { $post_at = $_POST [ "search" ][ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_POST [ "search" ][ "post_at_to_date" ])) { $post_at_to_date = $_POST [ "search" ][ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_POST [ "search" ][ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } |
** การกำหนดชื่อ ให้กับ form element ข้างต้น ไม่จำเป็นต้องกำหนดแบบ 2 มีติ หรือมี key
เพราะจะทำให้เวลาไปเรียกใช้งานเกิดความยุ่งยาก เช่นควรเป็น
name="post_at" แทน name="search[post_at]"
name="post_at_to_date" แทน name="search[post_at_to_date]"
ไฟล์ excel2.php เราเปิดไฟล์นี้ โดยไม่มีการส่งค่าใดๆ ไป
1 | <a href = "excel2.php" >Export to Excel</a> |
ดังนั้นเงื่อนไข ที่เรากำหนดในไฟล์ export2.php จึงไม่มีการส่งค่าจากลิ้งค์
แต่ว่า เรากลับเรียกใช้เงื่อนไข เช่นเดียวกับหน้า index.php คือใช้ค่าจาก method post
ให้เปลี่ยนเงื่อนไขการรับค่าในไฟล์ excel2.php เป็น GET แทน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $queryCondition = "" ; if (! empty ( $_GET [ "post_at" ])) { $post_at = $_GET [ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_GET [ "post_at_to_date" ])) { $post_at_to_date = $_GET [ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_GET [ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } |
และในตอนกำหนดลิ้งค์ก็ต้องส่งตัวแปร และค่าไปด้วย เป็น
1 2 | <a href = "excel2.php?post_at=ค่าวันที่เริ่ม&post_at_to_date=ค่าวันที่สิ้นสุด" > Export to Excel</a> |
ถ้ารับค่าจากฟอร์ม index.php ก็จะเป็น
1 2 3 4 5 6 7 8 | <?php // สร้างตัวแปร รับค่าจาก method POST แล้วนำไปต่อใน url เพื่อส่งค่า // ไปยังอีกไฟล์ excel2.php ด้วย method GET $_post_at = $_POST [ 'search' ][ 'post_at' ]; $_post_at_to_date = $_POST [ 'search' ][ 'post_at_to_date' ]; ?> <a href = "excel2.php?post_at=<?=$_post_at?>&post_at_to_date=<?=$_post_at_to_data?>" > Export to Excel</a> |
หรือจะใช้วิธีการสร้าง query string จากตัวแปร ด้วยคำสั่ง http_build_query ก็ได้ เช่น
1 2 3 4 5 | <?php $query_str = http_build_query( $_POST [ 'search' ]); ?> <a href = "excel2.php?<?=$query_str?>" > Export to Excel</a> |
บทความแนะนำที่เกี่ยวข้อง | |
---|---|
ออกรายงานเป็น excel ไฟล์ด้วย PHPExcel ตอนเริ่มใช้ | อ่าน 19,688 |
แนวทางดึงข้อมูลจาก database ออกรายงาน ด้วย PHPExcel | อ่าน 14,285 |
ออกรายงานเป็น Excel ไฟล์ ด้วย PhpSpreadsheet เบื้องต้น ตอนที่ 1 | อ่าน 17,644 |

ความคิดเห็นที่
2
ขอบคุณครับ สำหรับคำตอบ Code ใช้งานได้แล้ว ซึ่งปรับปรุงเป็นฉบับของตัวเอง ได้ดังนี้ครับ
1. testdata.sql (ชื่อ database คือ testdata ครับ)
2. index.php
3. excel2.php
1. testdata.sql (ชื่อ database คือ testdata ครับ)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | CREATE TABLE `testdata` ( `IDrun` int (100) NOT NULL , `IDrun2` varchar (100) COLLATE utf8_unicode_ci DEFAULT NULL , `IDnumber` varchar (100) COLLATE utf8_unicode_ci DEFAULT NULL , ` Name ` longtext COLLATE utf8_unicode_ci, `Work1` longtext COLLATE utf8_unicode_ci, `Work2` longtext COLLATE utf8_unicode_ci, `MyTime` longtext COLLATE utf8_unicode_ci, `logtime` longtext COLLATE utf8_unicode_ci, `logdate` longtext COLLATE utf8_unicode_ci, `sortdate` longtext COLLATE utf8_unicode_ci, `sortdate2` date NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE =utf8_unicode_ci; INSERT INTO `testdata` (`IDrun`, `IDrun2`, `IDnumber`, ` Name `, `Work1`, `Work2`, `MyTime`, `logtime`, `logdate`, `sortdate`, `sortdate2`) VALUES (39, 'a' , '1' , 'a' , '' , '' , '' , '' , '02-04-2563' , '2563-04-02' , '2020-04-02' ), (36, 'b' , '2' , 'b' , '' , '' , '' , '' , '31-03-2563' , '2563-03-31' , '2020-03-31' ), (44, 'c' , '3' , 'c' , '' , '' , '' , '' , '03-04-2563' , '2563-04-03' , '2020-04-03' ), (58, 'd' , '4' , 'd' , '' , '' , '' , '' , '07-04-2563' , '2563-04-07' , '2020-04-07' ), (57, 'e' , '5' , 'e' , '' , '' , '' , '' , '06-04-2563' , '2563-04-06' , '2020-04-06' ), (66, 'f' , '6' , 'f' , '' , '' , '' , '' , '09-04-2563' , '2563-04-09' , '2020-04-09' ); ALTER TABLE `testdata` ADD PRIMARY KEY (`IDrun`); ALTER TABLE `testdata` MODIFY `IDrun` int (100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=67; |
2. index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | <?php $conn = mysqli_connect( "localhost" , "root" , "" , "testdata" ); mysqli_set_charset( $conn , "utf8" ); $post_at = "" ; $post_at_to_date = "" ; $_post_at = (isset( $_POST [ 'search' ][ 'post_at' ])) ? $_POST [ 'search' ][ 'post_at' ] : '' ; $_post_at_to_date = (isset( $_POST [ 'search' ][ 'post_at_to_date' ])) ? $_POST [ 'search' ][ 'post_at_to_date' ] : '' ; $queryCondition = "" ; if (! empty ( $_POST [ "search" ][ "post_at" ])) { $post_at = $_POST [ "search" ][ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_POST [ "search" ][ "post_at_to_date" ])) { $post_at_to_date = $_POST [ "search" ][ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_POST [ "search" ][ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } $sql = "SELECT * FROM testdata " . $queryCondition . " ORDER BY sortdate ASC, logtime ASC" ; $result = mysqli_query( $conn , $sql ); ?> <html> <head> <meta charset= "UTF-8" > <style> * { font-size: 24px; } .table-content{border-top:#CCCCCC 4px solid; width:80%; overflow-x:auto;} .table-content th {padding:5px 20px; background: #F0F0F0;vertical-align:top;} .table-content td {padding:5px 20px; border-bottom: #F0F0F0 1px solid;vertical-align:top;} </style> </head> <body><center> <div class = "demo-content" > <form name= "frmSearch" method= "POST" action= "" > <p class = "search_input" > <input type= "text" placeholder= "From Date" id= "post_at" name= "search[post_at]" value= "<?php echo $post_at; ?>" class = "input-control" /> <input type= "text" placeholder= "To Date" id= "post_at_to_date" name= "search[post_at_to_date]" style= "margin-left:10px" value= "<?php echo $post_at_to_date; ?>" class = "input-control" /><br> <input type= "submit" name= "go" value= "Search" > <a href = "excel2.php?search[post_at]=<?=$_post_at?>&search[post_at_to_date]=<?=$_post_at_to_date?>&go=Search" >Export to Excel</a> </p> <?php if (! empty ( $result )) { ?> <table class = "table-content" > <thead> <tr> <th><div align= "center" >aa </div></th> <th style= "width:22%" ><div align= "center" >bb </div></th> <th><div align= "center" >cc </div></th> <th><div align= "center" >ee </div></th> <th style= "width:15%" ><div align= "center" > date </div></th> <th><div align= "center" >ff </div></th> <th><div align= "center" >gg </div></th> </tr> </thead> <tbody> <?php while ( $row = mysqli_fetch_array( $result )) { ?> <tr> <td><div align= "center" ><?php echo $row [ "IDnumber" ];?></div></td> <td><div align= "left" ><?php echo $row [ "Name" ];?></div></td> <td><div align= "center" ><?php echo $row [ "Work1" ];?></div></td> <td><div align= "center" ><?php echo $row [ "Work2" ];?></div></td> <td><div align= "center" ><?php echo $row [ "logdate" ];?></div></td> <td><div align= "center" ><?php echo $row [ "MyTime" ];?></div></td> <td><div align= "center" ><?php echo $row [ "logtime" ];?></div></td> </tr> <?php } ?> <tbody> </table> <?php } ?> </form> </div> <script> $.datepicker.setDefaults({ dateFormat: 'dd-mm-yy' , showOn: 'button' , buttonImage: "datepicker.png" , buttonImageOnly: false, dayNamesMin: [ 'อา' , 'จ' , 'อ' , 'พ' , 'พฤ' , 'ศ' , 'ส' ], monthNamesShort: [ 'มกราคม' , 'กุมภาพันธ์' , 'มีนาคม' , 'เมษายน' , 'พฤษภาคม' , 'มิถุนายน' , 'กรกฎาคม' , 'สิงหาคม' , 'กันยายน' , 'ตุลาคม' , 'พฤศจิกายน' , 'ธันวาคม' ], changeMonth: true, changeYear: true, beforeShow: function (){ if ($(this).val()!= "" ){ var arrayDate=$(this).val().split( "-" ); arrayDate[2]=parseInt(arrayDate[2])-543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } setTimeout( function (){ $.each($( ".ui-datepicker-year option" ), function (j,k){ var textYear=parseInt($( ".ui-datepicker-year option" ).eq(j).val())+543; $( ".ui-datepicker-year option" ).eq(j).text(textYear); }); },50); }, onChangeMonthYear: function (){ setTimeout( function (){ $.each($( ".ui-datepicker-year option" ), function (j,k){ var textYear=parseInt($( ".ui-datepicker-year option" ).eq(j).val())+543; $( ".ui-datepicker-year option" ).eq(j).text(textYear); }); },50); }, onClose: function (){ if ($(this).val()!= "" && $(this).val()==dateBefore){ var arrayDate=dateBefore.split( "-" ); arrayDate[2]=parseInt(arrayDate[2])+543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } }, onSelect: function (dateText, inst){ dateBefore=$(this).val(); var arrayDate=dateText.split( "-" ); arrayDate[2]=parseInt(arrayDate[2])+543; $(this).val(arrayDate[0]+ "-" +arrayDate[1]+ "-" +arrayDate[2]); } }); $( function () { $( "#post_at" ).datepicker(); $( "#post_at_to_date" ).datepicker(); }); </script> </center> </body></html> |
3. excel2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <?php //คำสั่ง connect db เขียนเพิ่มเองนะ $serverName = "localhost" ; $userName = "root" ; $userPassword = "" ; $dbName = "testdata" ; $conn = mysqli_connect( $serverName , $userName , $userPassword , $dbName ); mysqli_set_charset( $conn , "utf8" ); $strExcelFileName = "testdata.xls" ; header( "Content-Type: application/x-msexcel; name=" $strExcelFileName "" ); header( "Content-Disposition: inline; filename=" $strExcelFileName "" ); header( "Pragma:no-cache" ); $post_at = "" ; $post_at_to_date = "" ; $queryCondition = "" ; if (! empty ( $_GET [ "search" ][ "post_at" ])) { $post_at = $_GET [ "search" ][ "post_at" ]; list( $fid , $fim , $fiy ) = explode ( "-" , $post_at ); $post_at_todate = date ( 'Y-m-d' ); if (! empty ( $_GET [ "search" ][ "post_at_to_date" ])) { $post_at_to_date = $_GET [ "search" ][ "post_at_to_date" ]; list( $tid , $tim , $tiy ) = explode ( "-" , $_GET [ "search" ][ "post_at_to_date" ]); $post_at_todate = "$tiy-$tim-$tid" ; } $queryCondition .= "WHERE sortdate BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'" ; } $sql = "SELECT * FROM testdata " . $queryCondition . " ORDER BY sortdate ASC, logtime ASC" ; $query = mysqli_query( $conn , $sql ); ?> <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" > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> </head> <body> <strong>testdata</strong><br> <br> <div id= "SiXhEaD_Excel" align=center x:publishsource= "Excel" > <table x:str border=1 cellpadding=0 cellspacing=1 width=50% style= "border-collapse:collapse" > <tr> <th><div align= "center" >aa </div></th> <th><div align= "center" >bb </div></th> <th><div align= "center" >cc </div></th> <th><div align= "center" >ee </div></th> <th><div align= "center" > date </div></th> <th><div align= "center" >ff </div></th> <th><div align= "center" >gg </div></th> </tr> <?php while ( $result =mysqli_fetch_array( $query ,MYSQLI_ASSOC)) { ?> <tr> <td><div align= "center" ><?php echo $result [ "IDnumber" ];?></div></td> <td><div align= "left" ><?php echo " " . $result [ "Name" ];?></div></td> <td><div align= "center" ><?php echo $result [ "Work1" ];?></div></td> <td><div align= "center" ><?php echo $result [ "Work2" ];?></div></td> <td><div align= "center" ><?php echo $result [ "logdate" ];?></div></td> <td><div align= "center" ><?php echo $result [ "MyTime" ];?></div></td> <td><div align= "center" ><?php echo $result [ "logtime" ];?></div></td> </tr> <?php } ?> </table> </div> <script> window.onbeforeunload = function (){ return false;}; setTimeout( function (){window.close();}, 10000); </script> </body> </html> |
4. datepicker.png โหลดได้ที่ Link ด้านล่างครับ
โครงสร้าง File ทังหมด มีดังนี้ครับ

Screenshot 1 ที่อธิบายว่า เลือกช่วงวันที่แล้ว ก่อนที่จะ Export เป็น Excel ครับ

Screenshot 2 ที่อธิบายว่า เมื่อ Export เป็น Excel แล้ว ตารางใน Excel มาแบบเลือกช่วงวันที่แล้วครับ



ความคิดเห็นที่
3
เพิ่มเติมข้อมูล ในความเห็นที่ 2 ครับ
Code บรรทัดที่ 14-16 ตรงชื่อไฟล์ excel2.php นั้น พิมพ์ Code แล้ว ไม่มี "ชื่อตัวแปร" ที่ตัวแปร $strExcelFileName จึงเพิ่มเติมข้อมูล ดังนี้ครับ
Code บรรทัดที่ 14-16 ตรงชื่อไฟล์ excel2.php นั้น พิมพ์ Code แล้ว ไม่มี "ชื่อตัวแปร" ที่ตัวแปร $strExcelFileName จึงเพิ่มเติมข้อมูล ดังนี้ครับ
header("Content-Type: application/x-msexcel; name="$strExcelFileName"");
header("Content-Disposition: inline; filename="$strExcelFileName"");
header("Pragma:no-cache");

ความคิดเห็นที่
4
เพิ่มเติมข้อมูล ในความเห็นที่ 3 ครับ
เนื่องจากลองพิมพ์ Backslash ตรง Code บรรทัดที่ 14-16 แล้ว ไม่ขึ้นที่ตัวแปร $strExcelFileName จึงเพิ่มเติมข้อมูลเป็นรูปภาพแทน ดังนี้ครับ
เนื่องจากลองพิมพ์ Backslash ตรง Code บรรทัดที่ 14-16 แล้ว ไม่ขึ้นที่ตัวแปร $strExcelFileName จึงเพิ่มเติมข้อมูลเป็นรูปภาพแทน ดังนี้ครับ


ขอบคุณทุกการสนับสนุน
![]()