สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี
สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี
ผลรวมออก แต่ในช่องแต่ละเดือนไม่ออกมีผิดพลาด ตรงไหนคับ
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 | <?php include 'conf2.inc.php' ; require_once 'Classes/PHPExcel.php' ; require_once 'Classes/PHPExcel/IOFactory.php' ; if ( empty ( $_GET [ "repno_y" ])|| empty ( $_GET [ "repno_m" ])) { echo "ไม่มีหน้าที่ต้องการ" ; echo "<meta http-equiv='refresh' content='3; url=index.php'>" ; } else { $excel = new PHPExcel(); $excel ->setActiveSheetIndex(0); $dmy1 = $_GET [ "repno_y" ]; $dmy2 = $_GET [ "repno_m" ]; $sql = "SELECT a.rm_group_id as a,a.rm_group_name as b, (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=10 and YEAR( date )= $dmy1 ) AS 'c' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=11 and YEAR( date )= $dmy1 ) AS 'd' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=12 and YEAR( date )= $dmy1 ) AS 'e' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=1 and YEAR( date )= $dmy2 ) AS 'f' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=2 and YEAR( date )= $dmy2 ) AS 'g' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=3 and YEAR( date )= $dmy2 ) AS 'h' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=4 and YEAR( date )= $dmy2 ) AS 'i' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=5 and YEAR( date )= $dmy2 ) AS 'j' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=6 and YEAR( date )= $dmy2 ) AS 'k' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=7 and YEAR( date )= $dmy2 ) AS 'l' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=8 and YEAR( date )= $dmy2 ) AS 'm' , (select count (*) from rm_report where rm_group=a.rm_group_id and MONTH( date )=9 and YEAR( date )= $dmy2 ) AS 'n' , (select count (*) from rm_report where rm_group=a.rm_group_id and date BETWEEN '$dmy1' AND '$dmy2' ) AS 'o' from rm_group a"; mysqli_set_charset( $connect , "utf8" ); $query = mysqli_query( $connect , $sql ); $row = 4; while ( $data = mysqli_fetch_object( $query )) { $excel ->getActiveSheet() ->setCellValue( 'A' . $row , $data ->a) ->setCellValue( 'B' . $row , $data ->b) ->setCellValue( 'C' . $row , $data ->c) ->setCellValue( 'D' . $row , $data ->d) ->setCellValue( 'E' . $row , $data ->e) ->setCellValue( 'F' . $row , $data ->f) ->setCellValue( 'G' . $row , $data ->g) ->setCellValue( 'H' . $row , $data ->h) ->setCellValue( 'I' . $row , $data ->i) ->setCellValue( 'J' . $row , $data ->j) ->setCellValue( 'K' . $row , $data ->k) ->setCellValue( 'L' . $row , $data ->l) ->setCellValue( 'M' . $row , $data ->m) ->setCellValue( 'N' . $row , $data ->n) ->setCellValue( 'O' . $row , $data ->o); $row ++; } // 60 // กำหนดขนาดของ column $excel ->getActiveSheet()->getColumnDimension( 'A' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'B' )->setWidth(60); $excel ->getActiveSheet()->getColumnDimension( 'C' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'D' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'E' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'F' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'G' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'H' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'I' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'J' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'K' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'L' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'M' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'N' )->setWidth(5); $excel ->getActiveSheet()->getColumnDimension( 'O' )->setWidth(10); $excel ->getActiveSheet() ->setCellValue( 'A1' , 'รายงานโปรแกรมความเสี่ยง ตั้งแต่วันที่' .( $dmy1 ). ' ถึง ' .( $dmy2 )) ->setCellValue( 'A3' , 'รหัส' ) ->setCellValue( 'B3' , 'โปรแกรมความเสี่ยงด้าน' ) ->setCellValue( 'C3' , 'ต.ค.' ) ->setCellValue( 'D3' , 'พ.ย.' ) ->setCellValue( 'E3' , 'ธ.ค.' ) ->setCellValue( 'F3' , 'ม.ค.' ) ->setCellValue( 'G3' , 'ก.พ.' ) ->setCellValue( 'H3' , 'มี.ค.' ) ->setCellValue( 'I3' , 'เม.ย.' ) ->setCellValue( 'J3' , 'พ.ค.' ) ->setCellValue( 'K3' , 'มิ.ย.' ) ->setCellValue( 'L3' , 'ก.ค.' ) ->setCellValue( 'M3' , 'ส.ค.' ) ->setCellValue( 'N3' , 'ก.ย.' ) ->setCellValue( 'O3' , 'รวม' ); // จัดกลาง $excel ->getActiveSheet()->getStyle( 'A3:O3' )->getAlignment()->setHorizontal( 'center' ); // ตีเส้นตาราง $excel ->getActiveSheet()->getStyle( 'A3:O3' .( $row -1))->applyFromArray( array ( 'borders' => array ( 'allborders' => array ( 'style' => PHPExcel_Style_Border::BORDER_THIN ) ) ) ); // footer // $excel->getActiveSheet()->getStyle('G'.($row).':K'.($row))->applyFromArray( // array( // 'borders' => array( // 'allborders' => array( // 'style' => PHPExcel_Style_Border::BORDER_THIN // ) // ) // ) // ); header( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ); header( 'Content-Disposition: attachment; filename="รายงานโปรแกรมความเสี่ยงแยกรายเดือน.xlsx"' ); // header( 'Cache-Control: max-age=0' ); $file = PHPExcel_IOFactory::createWriter( $excel , 'Excel2007' ); mysqli_close( $connect ); } // 14 ?> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
แนวทาง ลองตรวจสอบส่วนการทำงานของตัวสร้าง excel โดยลองฟิกค่าตัวเลขไป แทนการดึงค่าจากฐานข้อมูล
ถ้าสร้าง excel ถูกต้อง แสดงว่าเป็นที่คำสั่งการ คิวรี่ ให้ลองนำคำสั่งคิวรี่ไปรันผ่าน phpmyadmin ดูผลลัพธ์ว่า
มีค่าถูกต้องตามต้องการหรือไม่
ถ้าสร้าง excel ถูกต้อง แสดงว่าเป็นที่คำสั่งการ คิวรี่ ให้ลองนำคำสั่งคิวรี่ไปรันผ่าน phpmyadmin ดูผลลัพธ์ว่า
มีค่าถูกต้องตามต้องการหรือไม่

ความคิดเห็นที่
2
ลองทำตาม @บอกออกนะ ครับ
แต่ที่นี้แต่ละปีจะต้องไปแก้ตัวเลขในโคดทุกปีละสิ ?? พอมีแนวทางรับไหมคับ @

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