簡體   English   中英

phpexcel 在 cakephp 1.3 中不起作用

[英]phpexcel not working in cakephp 1.3

我正在嘗試在我的 CakePHP 1.3 應用程序中使用 PHPExcel 創建和下載 excel 文件,但它不起作用。 我的代碼:-

App::import('Vendor', 'PHPExcel', array('file' => 'Classes/PHPExcel.php'));
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');

// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'Miscellaneous glyphs');

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// 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="01simple.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;

下載時沒有錯誤。 它只是下載一個空白文件。

它在核心 PHP 中工作(正確創建和下載),但在 CakePHP 1.3 中不起作用。

步驟 1:從 ( http://www.codeplex.com/PHPExcel ) 下載並將其放在 app\\vendors\\Classes 下。 還要確保將包含 PHPExcel 類的文件放在目錄之外。 所以你的類目錄將有 1 個文件夾和一個文件。

step2:在views/view_name/csv位置的視圖頁面中創建一個CSV文件。

第 3 步:在此文件夾中創建一個文件。

例如

<?php
echo "ID, Property ID, Date,  Status \n";
foreach($x as $y) {
    echo $y['Incident']['id'] . ',';
    echo $y['Incident']['name'] . ',';
    echo $y['Incident']['date'] .',';
    echo $y['Incident']['status'];
    echo "\n";
} 

步驟 4:在您呼叫的任何視圖中提供正確的鏈接。

使用鏈接: https : //stackoverflow.com/a/37072809/2281448

注意:以上答案適用於 CakePHP 2.0。 但唯一的區別在於下面給出的文件夾結構。 將文件夾“Classes”放入 Cakephp 文件夾“app/vendors/PHPExcel”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM