打开输出的Excel文件时出现PHPExcel错误

时间:2013-06-27 21:54:26

标签: export-to-excel phpexcel

我希望我在正确的地方。我有这个PHP将数据从SQL导出到XLSX。

<?php



 $conexion=odbc_connect("syb_master","sa","server");

 $sql = "Here's the query";

  $exec = odbc_exec($conexion,$sql);

 $resultado = odbc_result_all($exec);
 $registros = odbc_num_rows ($resultado);
 require_once '../Classes/PHPExcel.php';


 if ($registros > 0) {

   $objPHPExcel = new PHPExcel();

   //Informacion del excel
   $objPHPExcel->
    getProperties()
        ->setCreator("ingenieroweb.com.co")
        ->setLastModifiedBy("ingenieroweb.com.co")
        ->setTitle("Exportar excel desde mysql")
        ->setSubject("Ejemplo 1")
        ->setDescription("Documento generado con PHPExcel")
        ->setKeywords("ingenieroweb.com.co  con  phpexcel")
        ->setCategory("ciudades");    

   $i = 1;    
   while ($registro = odbc_fetch_object ($resultado)) {

      $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $registro->name);

      $i++;

   }
   $objPHPExcel->getActiveSheet()->setTitle('Simple');
}

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="ejemplo1.xlsx"');
header('Cache-Control: max-age=0');



$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
exit;
odbc_close($conexion);
?>

导出到XLS时有效,但是当我导出到XLS时,一切似乎都是正确的,除了完成后,excel无法打开文件。它给出错误:“Excel无法打开文件,因为文件格式或文件扩展名无效。”

我认为这是因为查询有很多数据,但我不确定。我是phpexcel的新手。我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:1)

这是我的代码。它不太好,但我没有这样的问题:

/** 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 'Classes/PHPExcel.php';


// Create new PHPExcel object
//echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(50);      
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);      
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);      
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);

$objPHPExcel->getProperties()->setCreator("Ketov Alexandr")
                             ->setLastModifiedBy("Ketov Alexandr")
                             ->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");

//$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

$query = "SELECT * FROM `lang`";
$result = mysql_query($query) or die(mysql_error());


$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Ключ')
            ->setCellValue('B1', 'На русском')
            ->setCellValue('C1', 'На казахском')
            ->setCellValue('D1', 'Портлет');

$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => '000000'),
        'size'  => 11,
        'name'  => 'Verdana'
    ));


$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('B1')->applyFromArray($styleArray)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('C1')->applyFromArray($styleArray)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('D1')->applyFromArray($styleArray)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

$n = 2;
while ($row = mysql_fetch_object($result)) 
{
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$n, $row->id)
            ->setCellValue('B'.$n, $row->ru)
            ->setCellValue('C'.$n, $row->kz)
            ->setCellValue('D'.$n, $row->module);
    $objPHPExcel->getActiveSheet()->getStyle('A'.$n)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    $objPHPExcel->getActiveSheet()->getStyle('B'.$n)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$n)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    $objPHPExcel->getActiveSheet()->getStyle('D'.$n)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    $n++;
}

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