简体   繁体   中英

Cant create excel sheet with version 2003 using PHP

I am trying for create excel sheet with the version 2003,using php But i dont get .Wth xlsx extension in my code i dont get excel sheet.I changed the version to xls in my code ,got excel sheet but cant open that file.2003 is installed in system.Any body help me please..

My code i given below.

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

As a standard install, MS Excel 2003 reads and writes BIFF-format files ( .xls extension), though there is an optional "compatibility toolkit" plug-in that also allows it to read/write OfficeOpenXML-format files ( .xlsx extension).

PHPExcel's Excel5 Writer creates BIFF-format files; the Excel2007 Writer creates OfficeOpenXML-format files.

If you're generating output for MS Excel 2003, then you should use the Excel5 Writer, unless you know that the "compatibility toolkit" plug-in has been installed for the MS Excel 2003 users who you're creating the files for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM