2013-10-03 118 views
1

我使用php excel將數據庫中的數據導出爲Excel格式。 我可以將所有數據從數據庫檢索到Excel ady。但是,如果我想要檢索表格的列名稱,我該怎麼做。任何人都可以幫忙檢索數據庫中的表到phpexcel

這裏是我的代碼:

<?php 

    header("Content-Type:application/vnd.ms-excel"); 
    header("Content-Disposition:attachment;filename=product.xls"); 
    header("Pragma:no-cache"); 
    header("Expires:0"); 

    header('Content-Type: text/html; charset=UTF-8'); 
     header("Content-type: application/octetstream"); 
     header('Content-Disposition: attachment; filename="export.csv"'); 
/** Error reporting */ 
    error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 
    date_default_timezone_set('Europe/London'); 

    if (PHP_SAPI == 'cli') 
die('This example should only be run from a Web Browser'); 

/** Include PHPExcel */ 
require_once '../Classes/PHPExcel.php'; 
require_once '../Classes/PHPExcel/Writer/Excel2007.php'; 
    require_once '../Classes/PHPExcel/IOFactory.php'; 

    $con = mysql_connect("localhost","root",""); 
    if (!$con) 
    { 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("litako", $con); 



    $objPHPExcel = new PHPExcel(); 

    $res= mysql_query("SELECT * FROM vendorlist "); 

    /** Error reporting */ 
    error_reporting(E_ALL); 

    date_default_timezone_set('Europe/London'); 



    $objPHPExcel = new PHPExcel(); 

if(!$res){ 
die("Error"); 
} 


$col = 0; 
$row = 3; 
while($mrow = mysql_fetch_assoc($res)) { 
$col = 0; 
foreach($mrow as $key=>$value) { 
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); 
    $col++; 
} 
$row++; 
} 
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet 
    $objPHPExcel->setActiveSheetIndex(0); 

    // Save Excel 2007 file 
$objPHPExcel->setActiveSheetIndex(0); 


header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="report.xls"'); 
    header('Cache-Control: max-age=0'); 
// If you're serving to IE 9, then the following may be needed 
header('Cache-Control: max-age=1'); 
    // If you're serving to IE over SSL, then the following may be needed 
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified 
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 
    header ('Pragma: public'); // HTTP/1.0 

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


exit; 
+0

你得到什麼錯誤? –

回答

0

你已經在你的while循環有列名,你叫mysql_fetch_assoc。 $ key值是列名稱。所以這裏是你的代碼的更新部分:

while($mrow = mysql_fetch_assoc($res)) { 
    $col = 0; 
    foreach($mrow as $key=>$value) { 
     // TODO: Do Something With the Column Name like set the row header. Note this crude code sets it every time. You really just want to do it the first time only. 
     $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 0, $key); 
     $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row + 1, $value); 
     $col++; 
    } 
    $row++; 
} 
+0

它的工作~~ Thx非常多。是否意味着$ key是列名? – jannet

+0

無論何時調用mysql_fetch_assoc,它都會返回一個數組,其中數組鍵是列名,數組值是列值。在您的foreach調用中,當您執行* $ mrow作爲$ key => $ value *時,您告訴PHP將列名稱分配給名爲$ key的變量。您可以通過執行以下操作輕鬆地將其分配給另一個變量,如$ columnName:* $ mrow as $ columnName => $ columnValue *。 – davidethell