2015-09-11 47 views
0
`$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"; 
     //print $query; exit; 
     $res = db_query($query); 
      while ($dataFetched = $res->fetchAssoc()) { 
$objPHPExcel->setActiveSheetIndex(0) 
      ->setCellValue('A1', $dataFetched['title']) 
      ->setCellValue('B1', $dataFetched['tot']) 
      ->setCellValue('C1', $dataFetched['per']); 

}` 

我試圖將我的oracle查詢結果導出到PHPExcel.php庫的xls文件中。 Xls文件正在生成並下載到網絡文件夾(下載腳本文件被保存)。但是xls文件只包含最後的查詢結果。將數據導出爲循環中的excel

問題

  1. 如何運行一個循環,所有結果保存到xls文件的每一行。
  2. 如何將xls文件下載到我的本地系統而不是Web託管文件夾。正常下載,並提示彈出詢問保存。
  3. 我怎樣才能定義一個自定義的文件名如report.xls下載文件。

工作代碼(XLS文件下載到網站根目錄)

`/** Error reporting */ 
error_reporting(E_ALL); 
ini_set('display_errors', TRUE); 
ini_set('display_startup_errors', TRUE); 
date_default_timezone_set('Europe/London'); 
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); 
/** Include PHPExcel */ 
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; 
// Create new PHPExcel object 
echo date('H:i:s') , " Create new PHPExcel object" , EOL; 
$objPHPExcel = new PHPExcel(); 

// Set document properties 
echo date('H:i:s') , " Set document properties" , EOL; 
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw") 
          ->setLastModifiedBy("Maarten Balliauw") 
          ->setTitle("PHPExcel Test Document") 
          ->setSubject("PHPExcel Test Document") 
          ->setDescription("Test document for PHPExcel, generated using PHP classes.") 
          ->setKeywords("office PHPExcel php") 
          ->setCategory("Test result file"); 
echo date('H:i:s') , " Add some data" , EOL; 
$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"; 
//print $query; exit; 
$res = db_query($query); 
$rowNumber = 1; 
while ($dataFetched = $res->fetchAssoc()) { 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$rowNumber, $dataFetched['title']) 
     ->setCellValue('B'.$rowNumber, $dataFetched['tot']) 
     ->setCellValue('C'.$rowNumber, $dataFetched['per']); 
    $rowNumber++; 
} 
$objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1); 
$objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true); 
// Rename worksheet 
echo date('H:i:s') , " Rename worksheet" , EOL; 
$objPHPExcel->getActiveSheet()->setTitle('Simple'); 
// Set active sheet index to the first sheet, so Excel opens this as the first sheet 
$objPHPExcel->setActiveSheetIndex(0); 
// Save Excel 2007 file 
echo date('H:i:s') , " Write to Excel2007 format" , EOL; 
$callStartTime = microtime(true); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); 
$callEndTime = microtime(true); 
$callTime = $callEndTime - $callStartTime; 
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; 
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL; 
// Echo memory usage 
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true)/1024/1024) , " MB" , EOL; 
// Save Excel 95 file 
echo date('H:i:s') , " Write to Excel5 format" , EOL; 
$callStartTime = microtime(true); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save(str_replace('.php', '.xls', __FILE__)); 
$callEndTime = microtime(true); 
$callTime = $callEndTime - $callStartTime; 

echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; 
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL; 
// Echo memory usage 
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true)/1024/1024) , " MB" , EOL; 
// Echo memory peak usage 
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true)/1024/1024) , " MB" , EOL; 

// Echo done 
echo date('H:i:s') , " Done writing files" , EOL; 
echo 'Files have been created in ' , getcwd() , EOL;` 

更新後的代碼(添加到下載XLS文件下載到本地)

// Add some data $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"; //print $query; exit; $res = db_query($query); $rowNumber = 1; while ($dataFetched = $res->fetchAssoc()) { $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$rowNumber, $dataFetched['title']) ->setCellValue('B'.$rowNumber, $dataFetched['tot']) ->setCellValue('C'.$rowNumber, $dataFetched['per']); $rowNumber++; } // Miscellaneous glyphs, UTF-8 $objPHPExcel->getActiveSheet()->getRowDimension(8)->setRowHeight(-1); $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setWrapText(true); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Page & Files Reports '); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="page_file_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

我從頁面中刪除了所有打印的錯誤報告。仍然添加html內容而不是結果。 – drup

回答

0

問題#1:

保持一排櫃檯,併爲每個循環的新行數修改單元格地址:

$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"; 
//print $query; exit; 
$res = db_query($query); 
$rowNumber = 1; 
while ($dataFetched = $res->fetchAssoc()) { 
    $objPHPExcel->setActiveSheetIndex(0) 
     ->setCellValue('A'.$rowNumber, $dataFetched['title']) 
     ->setCellValue('B'.$rowNumber, $dataFetched['tot']) 
     ->setCellValue('C'.$rowNumber, $dataFetched['per']); 
    $rowNumber++; 
} 

問題2:

看看下載的例子像01simple-download-xls.phpExamples PHPExcel文件夾.....閱讀文檔並查看示例應回答99%的可能問題

問題3:

你不能絕對,這取決於下載文件的人;您可以做的最好的方法是在響應頭中使用Content-Disposition(請參閱問題#2的答案)以「建議」下載的文件名

+0

謝謝馬克。您的代碼按預期工作。我嘗試了'01simple-download-xls.php'中的代碼來下載文件。但下載的文件包含文件的HTML而不是結果。我在問題中發佈我的更新代碼。 – drup

+0

然後,如果請求是用於導出,則不會生成任何html ....不要向瀏覽器回顯/打印任何內容以導出 –

+0

如果您沒有向瀏覽器發送任何內容,您確定代碼被調用?並檢查您的日誌 –