<?php
  /* Include PEAR package for Excel export */

  if ($hostname != 'Carsten-Germers-iMac.local') {
      ini_set("register_globals",1);
    require_once 'Spreadsheet/Excel/Writer.php';
      ini_set("register_globals",0);
  }

  class Excelexport {
    /* Header: Array of field descriptions */
    var $fields = Array();
    /* Data rows (array of arrays) */
    var $data = Array();

    var $workbook;
    var $worksheet;

    function __construct($filename, $name, $fieldnames, $data) {
      $this->name = $name;
      $this->filename = $filename;
      $this->addfields($fieldnames);
      $this->adddata($data);
      $this->workbook = new Spreadsheet_Excel_Writer($filename);
      $this->worksheet = $this->workbook->addWorksheet($this->name);
      // $this->worksheet->setInputEncoding('ISO-8859-1');
      $this->worksheet->setInputEncoding('WINDOWS-1252');
    }

    function addfields($row) {
      $this->fields = $row;
    }

    function adddata($rows) {
      $this->data = $rows;
    }

    function create() {
        // Write header
        $format =& $this->workbook->addFormat();
        $format->setBold();
        for ($i=0; $i<sizeof($this->fields); $i++) {
            $this->worksheet->writeString(0, $i, utf8_decode($this->fields[$i]), $format);
        }
        // Write data
        $format =& $this->workbook->addFormat();
        $format->setNumFormat('@');
        for ($j=0; $j<sizeof($this->data); $j++) {
            for ($i=0; $i< sizeof($this->data[$j]); $i++) {
                $this->worksheet->writeString($j+1, $i ,utf8_decode($this->data[$j][$i]),$format);
            }
        }
        $this->workbook->close();
    }

  }
