PHPExcel通过$ objWriter->生成文件保存(' php://输出') - 是否需要权限?

时间:2015-07-03 04:40:05

标签: php codeigniter phpexcel

我在php CodeIgniter中有一个代码,它从数据库中提取数据并通过PHPExcel生成一个xls文件。

问题是每当我将代码上传到另一台服务器时,它会生成一个带有0kb的.xls文件,而打开时的错误是:
" Excel无法打开文件" ____"因为文件格式或文件扩展名无效。验证文件是否已损坏,文件扩展名是否与文件格式相符"。

传入的数据来自同一个表,所使用的所有库也相同。

我的问题:
1.是否有服务器应用的r / w权限使得phpexcel文件为0kb?我正在使用CodeIgniter 2.有没有办法查看PHPExcel对象中写的内容?
我有什么遗漏的东西吗?

生成xls文件的代码是:

// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
    $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
    $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

// Set page orientation and size
    $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
    $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

    $this->load->library('PHPExcel/PHPExcel_IOFactory');

    $file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";
// Redirect output to a client web browser (Excel2007)
//ob_end_clean();
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename=' . $file_name);
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//  ob_end_clean();

    $objWriter->save('php://output');

提前谢谢。


我在测试服务器中显示的输出是:

enter image description here

解决方案: 事实证明,server1的php版本是5.2.9
php2的server2版本是5.1.6

server2中生成错误: 致命错误:Class' ZipArchive'在第225行的/var/www/html/APP_NAME/application/libraries/PHPExcel/Writer/Excel2007.php中找不到


ZipArchive模块需要php版本> 5.2

更新后,它可以正常工作。

谢谢你们的建议。

2 个答案:

答案 0 :(得分:3)

它输出0kb或" Excel无法打开文件" _因为执行脚本遇到异常错误。启用错误报告,将其放在脚本的顶部:

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

是的,您可能需要检查正确的r / w权限和PHPExcel.php的所有权,但输出文件不再需要了。

我已经测试了你的代码,它工作得很好..

<?php

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

/** Include PHPExcel */
require_once('PHPExcel.php');
$objPHPExcel = new PHPExcel();

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Set header and footer. When no different headers for odd/even are used, odd header is assumed.
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BInvoice&RPrinted on &D');
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

// Set page orientation and size
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

// $this->load->library('PHPExcel/PHPExcel_IOFactory');

$uri_year = "2015";
$uri_month = "now";
$file_name = $uri_year . "-" . $uri_month . "_staff_report.xlsx";

$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', "This is a test");

// Redirect output to a client web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=' . $file_name);
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//  ob_end_clean();
$objWriter->save('php://output');

&GT;

您可能需要检查您是否拥有适用于Excel2007的库,我已将其更改为Excel5,但您可以使用Excel2007,它可以正常使用。

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

答案 1 :(得分:1)

请检查以下代码测试:

 public function demo()
    {

        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);

        define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

        /** Include PHPExcel */
        $this->load->library('PHPExcel');

        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator("MokNathal")
                                     ->setLastModifiedBy("MokNathal")
                                     ->setTitle("STAFF REPORT")
                                     ->setSubject("STAFF REPORT")
                                     ->setDescription("STAFF REPORT")
                                     ->setKeywords("STAFF REPORT")
                                     ->setCategory("STAFF REPORT");



        $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A1')->setValue("Name of staff:aaaaaa Lname");
        $objPHPExcel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setWrapText(true);       

        $objPHPExcel->getActiveSheet()->mergeCells('A2:F2');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A2')->setValue("Group:Sunday Holiday Plan");
        $objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setWrapText(true);       

        $objPHPExcel->getActiveSheet()->mergeCells('A3:F3');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A3')->setValue("Login Details");
        $objPHPExcel->getActiveSheet()->getStyle('A3:F3')->getAlignment()->setWrapText(true);           

        $objPHPExcel->getActiveSheet()->mergeCells('A4:F4');
        $objPHPExcel->setActiveSheetIndex(0)
                    ->getCell('A4')->setValue("Checkin time:09:05:00am");
        $objPHPExcel->getActiveSheet()->getStyle('A4:F4')->getAlignment()->setWrapText(true);           

        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A5', 'Year')
                     ->setCellValue('B5', 'Month')
                     ->setCellValue('C5', 'Day')
                     ->setCellValue('D5', 'Login Time')
                      ->setCellValue('E5', 'Logout Time')
                       ->setCellValue('F5', 'Login Status');

        $objPHPExcel->getActiveSheet()->getStyle('A5:F5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objPHPExcel->getActiveSheet()->getStyle('A5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('B5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('D5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('E5')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('F5')->getFont()->setBold(true);

        $i=6;
        $uri_year=2071;
        $uri_month=4;
        for($j=1;$j<=30;$j++)
        {
        $objPHPExcel->setActiveSheetIndex(0)
                         ->setCellValue('A'.$i, $uri_year)
                         ->setCellValue('B'.$i, $uri_month)
                         ->setCellValue('C'.$i, $j);

                $i++;

        }
        foreach(range('A','F') as $columnID) {
            $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
                ->setAutoSize(true);
        }

        $objPHPExcel->getActiveSheet()->setTitle('Trasaction List');


        $objPHPExcel->setActiveSheetIndex(0);

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-Disposition: attachment; filename=".$uri_year."-".$uri_month."_staff_report.xlsx\"");
        header("Cache-Control: max-age=0");

        $objWriter->save("php://output");

    }