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 > เขียนถูกไหมครับ Export เป็น Excel ช่วยแนะนำการเขียนที่ถูกหน่อยครับ



 

เขียนถูกไหมครับ Export เป็น Excel ช่วยแนะนำการเขียนที่ถูกหน่อยครับ

 



Topic : 125450



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

สมาชิกที่ใส่เสื้อไทยครีเอท

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



จะส่งออกเป็น Excel ไม่รู้ว่าเขียนถูกไหม ครับ

export_excel

Code (PHP)
<?php
 
set_time_limit(0);
header('Content-Type: text/html; charset=utf-8');
/** PHPExcel */
require_once ("PHPExcel/Classes/PHPExcel.php");
// สร้าง object ของ Class  PHPExcel  ขึ้นมาใหม่
$objPHPExcel = new PHPExcel();
 
// กำหนดค่าต่างๆ
$objPHPExcel->getProperties()->setCreator("Company Co., Ltd.");
$objPHPExcel->getProperties()->setLastModifiedBy("Company Co., Ltd.");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Data Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Data Document");
$objPHPExcel->getProperties()->setDescription("Data from Company Co., Ltd.");
 
$sheet = $objPHPExcel->getActiveSheet();
$pageMargins = $sheet->getPageMargins();
 
// margin is set in inches (0.5cm)
$margin = 0.5 / 2.54;
//Set Margin
$pageMargins->setTop($margin);
$pageMargins->setBottom($margin);
$pageMargins->setLeft($margin);
$pageMargins->setRight(0);
 
//กำหนดความกว้างในแต่ละ Colum
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(10);
	$objPHPExcel->getActiveSheet()->getColumnDimension('0')->setWidth(10);
//  
 
//สร้าง Style เพื่อกำหนดฟอร์แมตของหัว Colum
$styleHeader = array(
        'fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID,'color' => array('rgb' => 'ffff00')),
        'borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN)),
        'font'  => array(
        'bold'  => true,
        'size'  => 9,
        'name'  => 'Arial'
    ));
 
//กำหนด Style และข้อมูล ให้กับ Row ที่ 1 หรือหัวของ Colum
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'รหัส')
                ->setCellValue('B1', 'โปรแกรมความเสี่ยง')
                ->setCellValue('C1', 'ต.ค.')
                ->setCellValue('D1', 'พ.ย.')
                ->setCellValue('E1', 'ธ.ค.')
				->setCellValue('F1', 'ม.ค.')
				->setCellValue('G1', 'ก.พ.')
				->setCellValue('H1', 'มี.ค.')
				->setCellValue('I1', 'เม.ย.')
				->setCellValue('J1', 'พ.ค.')
				->setCellValue('K1', 'มิ.ย.')
				->setCellValue('L1', 'ก.ค.')
				->setCellValue('M1', 'ส.ค.')
				->setCellValue('N1', 'ก.ย.')
				->setCellValue('O1', 'รวม');
                $objPHPExcel->getActiveSheet()->getStyle('A1:O1')->applyFromArray($styleHeader);
//
 
//กำหนดแถวเริ่มต้นในการเริ่มใส่ข้อมูล
$rowCell=2;
 
//ทำการดึงข้อมูลจาก Database
//Connect DB
$mysql = new mysql('localhost','root','11428','dbrm');
if ($mysql->connect_errno) {
    die( "Failed to connect to MySQL : (" . $mysql->connect_errno . ") " . $mysql->connect_error);
}
$mysql->set_charset("utf8"); 
$query = " select a.rm_group_id,a.rm_group_name as program,
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=10 and YEAR(date)=$ya) AS 'oct',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=11 and YEAR(date)=$ya) AS 'nov',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=12 and YEAR(date)=$ya) AS 'dec',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=1 and YEAR(date)=$yb) AS 'jan',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=2 and YEAR(date)=$yb) AS 'feb',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=3 and YEAR(date)=$yb) AS 'mar',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=4 and YEAR(date)=$yb) AS 'apr',								
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=5 and YEAR(date)=$yb) AS 'may',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=6 and YEAR(date)=$yb) AS 'jun',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=7 and YEAR(date)=$yb) AS 'jul',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=8 and YEAR(date)=$yb) AS 'aug',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and MONTH(date)=9 and YEAR(date)=$yb) AS 'sep',
		  (select count(*) from rm_report where rm_group=a.rm_group_id and date BETWEEN '$ya-10-01' and '$yb-09-30') AS 'total'	from rm_group a ";
$res = $mysql->query($query);
while($row = $res->fetch_array()){
    //วนลูปเพื่อใส่ข้อมูล
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$rowCell, $row['a.rm_group_id'])
                ->setCellValue('B'.$rowCell, $row['a.rm_group_name'])
                ->setCellValue('C'.$rowCell, $row['oct'])
                ->setCellValue('D'.$rowCell, $row['nov'])
                ->setCellValue('E'.$rowCell, $row['dec'])
		        ->setCellValue('F'.$rowCell, $row['jan'])
		        ->setCellValue('G'.$rowCell, $row['feb'])
		        ->setCellValue('H'.$rowCell, $row['mar'])
		        ->setCellValue('I'.$rowCell, $row['apr'])
		        ->setCellValue('J'.$rowCell, $row['may'])
		        ->setCellValue('K'.$rowCell, $row['jun'])
		        ->setCellValue('L'.$rowCell, $row['jul'])
		        ->setCellValue('M'.$rowCell, $row['aug'])
		        ->setCellValue('N'.$rowCell, $row['sep'])
		        ->setCellValue('O'.$rowCell, $row['total']);
   // $rowCell++;
}
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Sheet1');
 
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment;filename="rm_group.xlsx"'); //กำหนดชื่อไฟล์
$objWriter->save('php://output');
 
exit();
 
?>




Tag : PHP, MySQL







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-11-21 09:58:30 By : you_jack View : 1221 Reply : 4
 

 

No. 1



โพสกระทู้ ( 5,105 )
บทความ ( 4 )

สมาชิกที่ใส่เสื้อไทยครีเอท Hall of Fame 2012

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


ลองรัน แล้วได้ผลอย่างไรครับ

ถ้าไม่ผ่าน มี Error ก็แก้ตาม Error
ถ้าผ่านก็เขียนถูกครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-21 10:49:17 By : apisitp
 


 

No. 2



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

สมาชิกที่ใส่เสื้อไทยครีเอท

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

ตอบความคิดเห็นที่ : 1 เขียนโดย : apisitp เมื่อวันที่ 2016-11-21 10:49:17
รายละเอียดของการตอบ ::
...Fatal error: Call to a member function fetch_array() on a non-object in C:\AppServ\www\bt-rm\ExportExcel_rm_group.php on line 101

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-21 14:49:26 By : you_jack
 

 

No. 3

Guest


เหมือน query จะเขียนผิด
ลองเอาไปรันใน sql ดูก่อนดีกว่า
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-11-22 15:34:53 By : limberger
 


 

No. 4



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



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


while($row = $res->fetch_array(ตรงนี้หรือเปล่า)){
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-01 09:51:23 By : moonnui
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : เขียนถูกไหมครับ Export เป็น Excel ช่วยแนะนำการเขียนที่ถูกหน่อยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 00
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 อัตราราคา คลิกที่นี่