从Oracle将查询结果导出并下载到PHP中的Excel文件中 [英] Export and download query results to Excel file in PHP from oracle

查看:232
本文介绍了从Oracle将查询结果导出并下载到PHP中的Excel文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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++);
    }
    // Save as an Excel BIFF (xls) file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('CityList.xls');
}

我在PHP页面中尝试使用此代码从Oracle中将查询结果下载为Excel文件,但是当我浏览至该页面时,它就像挂着一样都是空白.

I tried this code in a PHP page to download query results from Oracle as an Excel file, but when I browse to the page, it is all blank and white like it is hung.

有任何线索吗?

推荐答案

使用此代码将在本地系统中下载excel文件.还要检查文件夹权限

Use this code that will download the excel file in local system Check folder permission as well

要下载excel文件,您需要设置标题.因此只需提供文件名,该文件名将是下载的excel文件的名称.

// 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;
}

这篇关于从Oracle将查询结果导出并下载到PHP中的Excel文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆