Create an Excel document with PHP

Some weeks ago I needed the possibility to create an excel sheet with PHP. I searched for it and found several possibilities which are more or less unusable or are running to long for the timeout of the webspace where I implemented the solution.

But after a few days of searching I found a good framework to create the excel sheets in an easy way.It is a single PHP class, created by Roshan Bhattarai, which is very easy to use and it delivers absolutely the result I wanted to get. This class is called ExportExcel.

To create an excel sheet it is only necessary to create a session, write everything you need into the session array and then call the class of ExportExcel to write it to the file.

<?php
  session_start();
  ob_start();

  unset($_SESSION['report_header']);
  unset($_SESSION['report_values']);

  include_once("./ExcelReport/class.export_excel.php");
  $xls=new ExportExcel("documentName.xls");

  $_SESSION['report_header']=array("Id-Number","Lastname","Firstname","Email");

  $counter = 0;
  while ($counter <= 20) {
    $column = 0;
    $_SESSION['report_values'][$counter][$column++] = "$counter";
    $_SESSION['report_values'][$counter][$column++] = "Last Name";
    $_SESSION['report_values'][$counter][$column++] = "First Name";
    $_SESSION['report_values'][$counter][$column++] = "0123/456789012";
    $_SESSION['report_values'][$counter][$column++] = "testaccount@emailaddress.de";
    $counter++;
  }

  $xls->setHeadersAndValues($_SESSION['report_header'],$_SESSION['report_values']);
  $xls->GenerateExcelFile();
?>

Thats all. Like you can see, this kind of excel create is so easy, that I do not need much words to explain it compleately.

A lot of thanks to Roshan Bhattarai for this good peace of code which makes the world of development again a bit easier. You can visit him on http://roshanbh.com.np.

Class for download, including a test implentation

Print Friendly, PDF & Email

One thought on “Create an Excel document with PHP

Leave a Comment

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
*