There are several libraries out there for reading and writing MS Excel files. Here I note down my experiences with some of them.

Here is a list of libraries for working with Excel:

 

maatwebsite/excel

Link: https://packagist.org/packages/maatwebsite/excel

Reading a worksheets first column:

Be sure to configure the Package to import the headings as numeric:

'heading' => 'numeric',

Here is my sample code:

Excel::selectSheetsByIndex(1)->load($filename, function($sheet){

  $sheet->each(function($row) {
      var_dump($row[1]);
  });

});

 

php-excel-reader/spreadsheet-reader

Link: https://packagist.org/packages/php-excel-reader/spreadsheet-reader

Reading a worksheets first column:

// open file
$data = new PHPExcelReader\SpreadsheetReader("excel.xls", false , 'UTF-8');
$rows = $data->rowcount();

// loop through all rows
for ($i=1; $i <= $rows; $i++) {
  
  // output first column
  echo $data->val($i, 1);
}

 

Export as HTML to Excel

Last but not least there is the HTML output. Here I found a nice article about: http://jwcooney.com/2011/09/08/generating-excel-documents-through-html-xml-and-css-part-2/

PHP Header

header("Content-type: application/vnd-ms-excel; charset=utf-8;");
header("Content-Disposition: attachment; filename=export_".time().".xls");
header('Pragma: no-cache');

Example HTML Table

<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
td, th {
vertical-align: top;
text-align: left;
}
</style>
</head>
<body>

<table>
  <thead>
    <?php foreach ($header as $head) : ?>
    <th><?php echo $head; ?></th>
    <?php endforeach; ?>
  </thead>
  <tbody>
    <?php foreach ($export_table as $item) : ?>
    <tr>
      <?php foreach ($keys as $key) : ?>
      <td><?php echo $item->$key; ?></td>
      <?php endforeach; ?>
    </tr>
    <?php endforeach; ?>
  </tbody>
</table>

</body>
</html>

 

Categories: PHP

1 Comment

sarawoot · 16/11/2015 at 05:22

Thank youuuu ^^

Comments are closed.