Advertisement
GWibisono

phpexcel.net make excel using php

May 27th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. <?php
  2. $s='Start :'.date('H:i:s')."<hr>";
  3. if($_GET['stat']=='open')
  4. {
  5. /*
  6. xls=application/vnd.ms-excel
  7. xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  8. */
  9.     $_GET['type']=='2k3'?header('Content-type:application/vnd.ms-excel'): header('Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') ;
  10.     $_GET['type']=='2k3'?$fName='tmp/002data.xls':$fName='tmp/002data.xlsx';
  11.     header('Content-Disposition: attachment; filename="002.xlsx"');
  12.     $f=file_get_contents($fName);
  13.     echo $f;
  14.     $stop=1;
  15.     die();
  16. }
  17.  
  18. if($_GET[stat]=='go')
  19. {
  20.     /* make excel */
  21.     //die(__FILE__); C:\\php\\kerjaan4\\excel2\\n<br/>asgor002-makeExcel.php
  22.     $type=$_GET['type'];
  23.     error_reporting(E_ALL);
  24.     date_default_timezone_set('Europe/London');
  25.     require_once 'Classes/PHPExcel.php';
  26.     // Create new PHPExcel object
  27.     $s.= date('H:i:s') . " Create new PHPExcel object\\n<br/>";
  28.     $objPHPExcel = new PHPExcel();
  29.  
  30.     // Set properties
  31.     $s.= date('H:i:s') . " Set properties\\n<br/>";
  32.     if($type=='2k3') $sType='2003 XLS';
  33.     if($type=='2k7') $sType='2007 XLSX';
  34.      
  35.     $objPHPExcel->getProperties()->setCreator("Nasgor")
  36.      ->setLastModifiedBy("Gunawan")
  37.      ->setTitle("Office  Test Document")
  38.      ->setSubject("Office $sType Test Document")
  39.      ->setDescription("Test document for Office $sType, generated using PHP classes.")
  40.      ->setKeywords("office $sType openxml php")
  41.      ->setCategory("Test result file");
  42.  
  43.  
  44.     // Add some data
  45.     $s.= date('H:i:s') . " Add some data\\n<br/>";
  46.     $objPHPExcel->setActiveSheetIndex(0)
  47.         ->setCellValue('A1', 'Hello')
  48.         ->setCellValue('B2', 'world!')
  49.         ->setCellValue('C1', 'Hello')
  50.         ->setCellValue('D2', 'world!');
  51.     $objPHPExcel->setActiveSheetIndex(0)
  52.         ->setCellValue('A7',$sType)
  53.         ->setCellValue('C7',date('d-m-Y'));  
  54.     //if i put in A4.. the below will erase it
  55.      
  56.     // Miscellaneous glyphs, UTF-8
  57.     $objPHPExcel->setActiveSheetIndex(0)
  58.         ->setCellValue('A4', 'Miscellaneous glyphs')
  59.         ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
  60.  
  61.     // Rename sheet
  62.     $s.= date('H:i:s') . " Rename sheet\\n<br/>";
  63.     $objPHPExcel->getActiveSheet()->setTitle('Simple');
  64.  
  65.  
  66.     // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  67.     $objPHPExcel->setActiveSheetIndex(0);
  68.  
  69.  
  70.     // Save Excel 2007 file
  71.     $s.= date('H:i:s') . " Write to $sType format\\n<br/>";
  72.  
  73.     if($type=='2k3') $writeAs='Excel2003';
  74.     if($type=='2k7') $writeAs='Excel2007';
  75.      
  76.     $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $writeAs);
  77.     if($type=='2k3') $objWriter->save('tmp/002data.xls');
  78.     if($type=='2k7') $objWriter->save('tmp/002data.xlsx');
  79.     //str_replace('.php', '.xlsx', __FILE__)
  80.  
  81.     // $s.= memory peak usage
  82.     $s.= date('H:i:s') . " Peak memory usage: " .
  83.         (memory_get_peak_usage(true) / 1024 / 1024) . " MB\\r\\n<br/>";
  84.  
  85.     // $s.= done
  86.     $s.= date('H:i:s') . " Done writing file.\\r\\n<br/>";
  87.     $s.="<a href='?stat=open&type=$type'>Open the document($sType)</a>";
  88.     $stop=1;
  89. }
  90.  
  91. if($stop==1)die($s);
  92. ?>
  93. This Script will be make an excel file (xls2k3/xls 2k7) then you can download it or open it in the browser.
  94. Chrome usualy download it not open in the browser<ol>
  95. <li><a href='?stat=go&type=2k7'>Click to continue (create xlsx)</a>
  96. <li><a href='?stat=go&type=2k3'>click to continue (create xls)</a> (not avaiable)
  97. </ol>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement