2013-10-03 2 views
1

나는 PHP를 사용하여 Excel 형식으로 데이터베이스의 데이터를 내보낼 수 있습니다. 데이터베이스에서 Excel ady로 모든 데이터를 검색 할 수 있습니다. 하지만 테이블의 열 이름을 가져 오려면 어떻게해야할까요? 누구든지 도울 수 있니? 당신이 mysql_fetch_assoc를 호출 할 경우 당신은 이미 while 루프에서 열 이름이데이터베이스에있는 테이블을 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

:

여기 내 코드입니다. $ 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 *로 할 때 $ key라는 변수에 열 이름을 할당하도록 PHP에 지시합니다. 다음과 같이하면 $ columnName과 같은 다른 변수에 쉽게 할당 할 수 있습니다. * $ mrow as $ columnName => $ columnValue *. – davidethell

관련 문제