2013-03-11 1 views
-1

이 코드 조각을 인터넷에서 가져 와서 mySQL 데이터베이스에서 Excel 파일로 데이터를 내보내도록되어 있습니다. 그게 내가 어떻게 이걸 촉발해야한다는거야? 그래픽 측면이 없으며 스크립트를 트리거하는 버튼이 없습니다. 어떻게 작동시킬 수 있습니까?PHP 스크립트에 그래픽 측면이 없음

<?PHP 
    // Original PHP code by Chirp Internet: www.chirp.com.au 
    // Please acknowledge use of this code by including this header. 

    include('config.php'); 
    function cleanData(&$str) 
    { 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
    } 

    // filename for download 
    $filename = "website_data_" . date('Ymd') . ".xls"; 

    header("Content-Disposition: attachment; filename=\"$filename\""); 
    header("Content-Type: application/vnd.ms-excel"); 

    $flag = false; 
    $result = pg_query("SELECT * FROM norse5_proov ORDER BY id") or die('Query failed!'); 
    while(false !== ($row = pg_fetch_assoc($result))) { 
    if(!$flag) { 
     // display field/column names as first row 
     echo implode("\t", array_keys($row)) . "\r\n"; 
     $flag = true; 
    } 
    array_walk($row, 'cleanData'); 
    echo implode("\t", array_values($row)) . "\r\n"; 
    } 
    exit; 
?> 
+3

대신 복사/붙여 넣기의 PHP를 학습하려고 텍스트를 가져옵니다. 일부 html도 배울 수 있습니다. –

+0

코드를 잘 이해할 수 있습니다. 나는 HTML과 CSS에서 고급이지만 PHP는 프로그래밍 언어입니다 ... –

답변

1

여기 상업용 앱에서 저에게 적합한 코드가 있습니다.

if($_REQUEST["EXCEL"]){ 
    header("Content-Type: application/excel"); 
    header("Content-Disposition: attachment; FileName=assets.xls"); 
    header("Cache-Control: private"); 
    header("Content-Length: ".strLen($data)); 
    echo $data; 
    exit; 
} 

은 $ 데이터는 탭으로 구분 된 텍스트이지만, 엑셀 헤더는 Excel을 열고

+0

파일 확장명이 실제 파일 형식과 일치하지 않을 때 MS Excel의 최신 버전에서 제공하는 경고에주의하십시오 –