|
|
|
รบกวนขอคำปรึกษาหน่อยครับ ผมทำ Report จาก phpexcel ครับ จากเว็บนี้และผมทำให้มัน Report ออกมาเป็นการเซฟไฟล์ครับ แต่เซพแล้วขึ้นเป็นอักษรแปลกๆเต็มเลยครับ |
|
|
|
|
|
|
|
เขียน Code ไว้แบบไหนครับ
|
|
|
|
|
Date :
2013-02-01 14:17:36 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอคำแนะนำด้วยนะครับ
Code (PHP)
<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
header('Content-Type: text/html ; charset=UTF8');
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Bangkok');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once 'Classes/PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
//set page margins for a worksheet, use this code
$objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
$objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);
// Set Orientation, size and scaling
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
//Set BORDER
$styleArray=array(
'borders'=>array(
'allborders'=>array(
'style'=>PHPExcel_Style_Border::BORDER_THIN
)
)
);
$objPHPExcel->getActiveSheet()->getStyle('A1:A1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('B1:B1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('C1:C1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('D1:D1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('E1:E1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('F1:F1')->applyFromArray($styleArray);
$objPHPExcel->getActiveSheet()->getStyle('G1:G1')->applyFromArray($styleArray);
//Set Format Cell 1
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// Set fonts
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setBold(true);
//Set column
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(7);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ลำดับ')
->setCellValue('B1', 'SN')
->setCellValue('C1', 'ชื่อโปรแกรม')
->setCellValue('D1', 'ชื่อกิจการ')
->setCellValue('E1', 'ชื่อผู้ติดต่อ')
->setCellValue('F1', 'เบอร์โทรศัพท์')
->setCellValue('G1', 'Email');
include("connectdb.php");
$sql= "select * from member ";
$result=mysql_query($sql);
$i=2;
while($rs=mysql_fetch_array($result)){
$bill_id = $rs['id'];
$program_sn = $rs['sn'];
$program1 = $rs['program1'];
$bs_company = $rs['company'];
$bs_name = $rs['name'];
$bs_tel = $rs['tel'];
$bs_email = $rs['email'];
//Set Format Cell 2
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $bill_id)
->setCellValue('B'.$i, $program_sn)
->setCellValue('C'.$i, $program1)
->setCellValue('D'.$i, $bs_company)
->setCellValue('E'.$i, $bs_name)
->setCellValue('F'.$i, $bs_tel)
->setCellValue('G'.$i, $bs_email);
$i+=1;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('01simple.xls');
/*$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));*/
/*$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
echo 'Export Complete';
echo '<script>window.open("01simple.xls","_top")</script>';*/
?>
|
|
|
|
|
Date :
2013-02-01 14:22:14 |
By :
Lov3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าภาษาไทยให้ใช้ iconv แบบนี้ครับ
Code (PHP)
$objPHPExcel->getActiveSheet()->setCellValue('A1', ''.iconv('TIS-620','UTF-8'," รายงานข้อมูลการจัดซื้อ").'');
|
|
|
|
|
Date :
2013-02-01 14:32:04 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|