สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี

สอบถามส่งออกเป็น Excel ในปีงบ เลือกช่วง วัน เดือน ปี

ผลรวมออก แต่ในช่องแต่ละเดือนไม่ออกมีผิดพลาด ตรงไหนคับ




<?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');
$file->save('php://output');
mysqli_close($connect);
} // 14
?>


Batyoo 113.53.0.xxx 03-12-2018 23:38:47

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

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


ninenik 223.24.61.xxx 03-12-2018
 ความคิดเห็นที่ 2

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



batyoo 113.53.0.xxx 04-12-2018 00:15






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ