PHPExcel - .xlsx文件下载不可读的内容

时间:2015-06-13 14:17:08

标签: php phpexcel

我正在尝试将我的数据导出到电子表格,并且它在我的localhost中工作正常,但是当我将其上传到服务器时,它总是下载具有不可读内容的文件。在这里编写代码。

我的服务器中已经安装了php_xml,php_zip和gd。下载的文件是只读的。

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');

    if (PHP_SAPI == 'cli')
        die('This example should only be run from a Web Browser');

    /** Include PHPExcel */
    require_once dirname(__FILE__) . '/lib/PHPExcel.php';


    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();

    // Set document properties
    $objPHPExcel->getProperties()->setCreator("test")
                         ->setLastModifiedBy("test")
                         ->setTitle("test Report")
                         ->setSubject("test Report")
                         ->setDescription("test Report")
                         ->setKeywords("test Report")
                         ->setCategory("test Report");

    //Title
    $objPHPExcel->getActiveSheet()->mergeCells('A1:J1');
    $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objPHPExcel->getActiveSheet()->getStyle("A1:J1")->getFont()->setSize(20);
    $styleArray = array(
        'font' => array(
            'bold' => true
        )
    );
    $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->applyFromArray($styleArray);
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'test Report');
    $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(30);
    // //Set Column headlines
    $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A2', 'First Name')
        ->setCellValue('B2', 'Last name')
        ->setCellValue('C2', 'State')
        ->setCellValue('D2', 'Email')
        ->setCellValue('E2', 'Mob')
        ->setCellValue('F2', 'address')
        ->setCellValue('G2', 'PIN')
        ->setCellValue('H2', 'status')
        ->setCellValue('I2', 'condition')
        ->setCellValue('J2', 'log');

  $objPHPExcel->getActiveSheet()->getStyle("A2:J2")->getFont()->setSize(10);
  $objPHPExcel->getActiveSheet()->getStyle('A2:J2')->applyFromArray($styleArray);
  $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
  $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
  $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
  $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
  $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(5);
  $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(7);
  $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
  $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
  $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
  $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);

$colArray=array("A","B","C","D","E","F","G","H","I","J");
 $rowNumber = 3; 
    while ($row = mysqli_fetch_row($result)) { 
    $col = 0; 
    foreach($row as $cell) { 
        $objPHPExcel->getActiveSheet()->setCellValue($colArray[$col].$rowNumber,$cell); 
        $objPHPExcel->getActiveSheet()->getStyle($colArray[$col].$rowNumber)->getFont()->setSize(10);
        $col++; 
    } 
    $rowNumber++; 

    } 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('test Report');

// 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 (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="testreport.xlsx"');
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, 'Excel2007');
$objWriter->save('php://output');
exit;

2 个答案:

答案 0 :(得分:0)

我真的没有看到你的核心逻辑有问题。我能够通过轻微的修改从我的linux盒子中生成有效的xlsx文件

  1. 更新了require_once行,如下所示(我觉得保持PHPExcel默认文件夹结构完好无损)
  2. require_once dirname( FILE )。 '/../类/ PHPExcel / IOFactory.php';

    1. 由于我没有与示例对应的Mysqli,我将其修改为..

      $ row = array(1,2,3,4,5,6,7,8,9);
      // while($ row = mysqli_fetch_row($ result)){

    2. 总的来说,我想,您需要查看mysqli数据是否有效并正确启动PHPExcel。除此之外,我在这里看不到任何问题。

答案 1 :(得分:-1)

我在开始文档生成之前使用 ob_start(),然后以 ob_end_clean()结束,这解决了我的问题。