用PHPExcel外掛匯出Excel表格

PHPExcel外掛下載:http://download.csdn.net/detail/haibo0668/9844591

config.php程式碼:

<?php
session_start();
header("Content-type: text/html;charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "book";

// 建立連線
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢測連線
if ($conn->connect_error) {
    die("連線失敗: " . $conn->connect_error);
} 
    /* Close the connection 關閉連線*/     
   // mysqli_close($conn);     
date_default_timezone_set('prc');//時區問題,php環境預設時差與北京時間相差8小時,我們要想獲取正確的時間就必須設定在PHP檔案開始處 加上date_default_timezone_set('prc');
 
//代替_get('str')=$_GET['xxx']
function _get($str){
    $val = !empty($_GET[$str]) ? $_GET[$str] : null;
    return $val;
}
function _REQUEST($str){
    $val = !empty($_REQUEST[$str]) ? $_REQUEST[$str] : null;
    return $val;
}
function _POST($str){
    $val = !empty($_POST[$str]) ? $_POST[$str] : null;
    return $val;
}
ini_set("error_reporting","E_ALL & ~E_NOTICE");//遮蔽錯誤資訊

?>


<?php  
include("config.php");
header("Content-type: text/html;charset=utf-8");
//檢查許可權 04=匯出

	require_once("Classes/PHPExcel.php");
	include("Classes/PHPExcel/IOFactory.php"); 
	
	
	//$id=$_GET["id"];
//	$id="SA00000008";
//	$sql_saleinfo="select * from OrderInfoTable where SalesID='$id' ";
//	$rs_saleinfo=mysql_query($sql_saleinfo);
	
$sql_saleinfo = "SELECT * FROM data_guestbook";
$rs_saleinfo = mysqli_query($conn, $sql_saleinfo);
	
	//建立一個excel物件
	$objPHPExcel = new PHPExcel();

	// Set properties  設定檔案屬性
	$objPHPExcel->getProperties()->setCreator("ctos")
        ->setLastModifiedBy("ctos")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");

	//set width  設定表格寬度
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(60);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(60);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(30);
	
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(30);

	//設定水平居中  
	$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('K')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	
	$objPHPExcel->getActiveSheet()->getStyle('L')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('M')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('N')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('O')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
	// set table header content  設定表頭名稱 
	$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '專案')
        ->setCellValue('B1', '推廣渠道')
        ->setCellValue('C1', '電話')
        ->setCellValue('D1', '標題')
        ->setCellValue('E1', '備註')
        ->setCellValue('F1', '學員名稱')
        ->setCellValue('G1', 'QQ')
        ->setCellValue('H1', '微訊號')
        ->setCellValue('I1', 'Email')
        ->setCellValue('J1', '網址')
        ->setCellValue('K1', '負責人')
		->setCellValue('L1', '分類1')
		->setCellValue('M1', '分類2')
		->setCellValue('N1', '分類3')
		->setCellValue('O1', '留言時間');
		
	$rownum=1;	
  	//while ($rows_saleinfo=mysql_fetch_assoc($rs_saleinfo))
	while($rs = mysqli_fetch_assoc($rs_saleinfo)) 
	{ 
		$rownum++;		
		
			
		$objPHPExcel->getActiveSheet()->setCellValue('A' . $rownum, $rs['project']);  
		$objPHPExcel->getActiveSheet()->setCellValue('B' . $rownum, $rs['ad']);
		$objPHPExcel->getActiveSheet()->setCellValue('C' . $rownum, $rs['phone']);	
		$objPHPExcel->getActiveSheet()->setCellValue('D' . $rownum, $rs['title']);	
		$objPHPExcel->getActiveSheet()->setCellValue('E' . $rownum, $rs['content']);	
		$objPHPExcel->getActiveSheet()->setCellValue('F' . $rownum, $rs['username']);	
		$objPHPExcel->getActiveSheet()->setCellValue('G' . $rownum, $rs['qq']);	
		$objPHPExcel->getActiveSheet()->setCellValue('H' . $rownum, $rs['wx']);
		$objPHPExcel->getActiveSheet()->setCellValue('I' . $rownum, $rs['email']);
		$objPHPExcel->getActiveSheet()->setCellValue('J' . $rownum, $rs['url']);
		$objPHPExcel->getActiveSheet()->setCellValue('K' . $rownum, $rs['admin_name']);
		$objPHPExcel->getActiveSheet()->setCellValue('L' . $rownum, $rs['type1']);
		$objPHPExcel->getActiveSheet()->setCellValue('M' . $rownum, $rs['type2']);
		$objPHPExcel->getActiveSheet()->setCellValue('N' . $rownum, $rs['type3']);
		$objPHPExcel->getActiveSheet()->setCellValue('O' . $rownum, $rs['time']);

	}
		
	mysqli_close($conn);

	$objPHPExcel->getActiveSheet()->setTitle('Simple');


	// Set active sheet index to the first sheet, so Excel opens this as the first sheet
	$objPHPExcel->setActiveSheetIndex(0);

	//	$filename="銷售訂單".date('Y-m-d');
	// Redirect output to a client’s web browser (Excel5)
//	ob_end_clean();//清除緩衝區,避免亂碼
	header('Content-Type: application/vnd.ms-excel');
	//	header('Content-Disposition: attachment;filename='.$filename);
	header('Content-Disposition: attachment;filename="01simple.xls"');

	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
	exit;  

?>