Oracle
 sql >> Database >  >> RDS >> Oracle

Esporta e scarica i risultati della query in un file Excel in PHP da Oracle

Usa questo codice per scaricare il file excel nel sistema locale Controlla anche l'autorizzazione della cartella

Per scaricare il file excel devi impostare le intestazioni. Quindi fornisci semplicemente il nome del file che sarà il nome del file excel scaricato.

// Export results
require_once('PHPExcel.php');
$query = "SELECT DISTINCT TITLE, PID, TYPE, SUM(DAYCOUNT) AS tot, ROUND(SUM(DAYCOUNT)/( SELECT SUM(DAYCOUNT) FROM REPORT_LIST_VIEW), 4) AS per FROM REPORT_LIST_VIEW WHERE DAYCOUNT > '0' GROUP BY TITLE, PID, TYPE ORDER BY tot DESC";
if ($result = db_query($query)) {
    // Create a new PHPExcel object
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('List of Cities');
    // Loop through the result set
    $rowNumber = 1;
    while ($row = $result->fetchAssoc()) {
        $objPHPExcel->getActiveSheet()->fromArray($row,NULL,'A'.$rowNumber++);
    }



// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="CityList.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: Fri, 11 Sep 2015 05:00:00 GMT'); // Date
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;
}