无法使用PHP创建2003版本的excel表

时间:2014-09-19 08:56:25

标签: php phpexcel

我正在尝试使用版本2003创建excel表格,使用php但是我没有。我的代码中的.xth xlsx扩展我没有得到excel表。我在我的代码中将版本更改为xls,获得excel表但无法打开system.2003安装在system.Any身体帮助我..

我的代码我在下面给出。

 <?php
 error_reporting(E_ALL ^ E_NOTICE);
 session_start();

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




 require_once("../../../codelibrary/inc/variables.php");
  require_once("../../../codelibrary/inc/functions.php");

 /** Include PHPExcel */
 require_once '../../Classes/PHPExcel.php';

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

   $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A2:E2');
 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2','Sales Register');
  $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->getAlignment()->
  setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  $styleArray = array('font' => array('bold' => true));
  $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->
  applyFromArray($styleArray);
  $objPHPExcel->getActiveSheet()->getStyle("A2:D2")->getFont()->setSize(16);

   $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A4', 'Sl No')
        ->setCellValue('B4', 'Date')
        ->setCellValue('C4', 'Ledger Account')
        ->setCellValue('D4', 'Debit Amount')
        ->setCellValue('E4', 'Credit Amount');

   $styleArray = array('font' => array('bold' => true));
                          $objPHPExcel->setActiveSheetIndex(0)->getStyle('A4')->
   applyFromArray($styleArray);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('B4')->
   applyFromArray($styleArray);
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('C4')->
   applyFromArray($styleArray);
   $objPHPExcel->setActiveSheetIndex(0)->getStyle('D4')->
    applyFromArray($styleArray);
   $objPHPExcel->setActiveSheetIndex(0)->getStyle('E4')->
    applyFromArray($styleArray);

   //ALIGN HEADING TO THE CENTER        
    $objPHPExcel->setActiveSheetIndex(0)->getStyle('A4')->
   getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  $objPHPExcel->setActiveSheetIndex(0)->getStyle('B4')->
  getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  $objPHPExcel->setActiveSheetIndex(0)->getStyle('C4')->
  getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  $objPHPExcel->setActiveSheetIndex(0)->getStyle('D4')->g
  etAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
   $objPHPExcel->setActiveSheetIndex(0)->getStyle('E4')->
   getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

  //FOR SETTING WIDTH OF EACH COLUMN
   $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
   $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
   $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
   $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
   $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);

   $sl_no=1;
   $r=5;
  $sql = "SELECT * FROM customer";
  $qry = mysql_query($sql);
 if(mysql_num_rows($qry)>0)
  {
    while($res=mysql_fetch_assoc($qry))
    { 
       $cell1 = 'A'.$r; 
       $cell2 = 'B'.$r; 
       $cell3 = 'C'.$r; 
        $cell4 = 'D'.$r; 
       $cell5 = 'E'.$r; 

        $objPHPExcel->getActiveSheet()->setCellValue($cell1,$sl_no); 
         $objPHPExcel->getActiveSheet()->setCellValue($cell2,$res['name']); 
        $objPHPExcel->getActiveSheet()->setCellValue($cell3,$res['address']); 
        $objPHPExcel->getActiveSheet()->setCellValue($cell4,$res['email']); 
        $objPHPExcel->getActiveSheet()->setCellValue($cell5,$res['supp_id']); 

        $sl_no++;
        $r++;
     }
   }

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

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

1 个答案:

答案 0 :(得分:1)

作为标准安装,MS Excel 2003读取和写入BIFF-format文件(.xls扩展名),尽管有一个可选的&#34;兼容性工具包&#34;插件,也允许它读/写OfficeOpenXML-format个文件(.xlsx扩展名)。

PHPExcel的Excel5 Writer创建BIFF-format个文件; Excel2007 Writer会创建OfficeOpenXML-format个文件。

如果您正在为MS Excel 2003生成输出,那么您应该使用Excel5 Writer,除非您知道&#34;兼容性工具包&#34;已为您正在为

创建文件的MS Excel 2003用户安装了插件