tags:

views:

124

answers:

4

I'm displaying a table of data:

[Name] [Tel] [Email]    [checkbox]
Name 1  000  [email protected]
Name 2  000  [email protected] (checked)
Name 3  000  [email protected] (checked)

(BUTTON TO DOWNLOAD CHECKED IN XLS)

What I'm trying to achieve here is to create this button which will allow the user to download all checked entries in an XLS file?

+2  A: 

If a CSV file will do (can be opened/edited/saved in Excel) you can use do something like this:

foreach ($thing as $val) {
    if($val['checked']) {
        $csv_output .= "\"" . $val['name'] . "\",\"" . $val['tel'] . "\",\"" . $val['email'] . "\"\n";
    }
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=" . $filename . "-" . date("Y-m-d") . ".csv");
print $csv_output;die;

Where $csv_output is a string formatted in CSV format.

If you want to save the file you can use the PHP function fputcsv

mrinject
A: 

Only one part of the solution of course, but the PHP module Spreadsheet Excel Writer will allow you to output a Excel spreadsheet.

therefromhere
A: 

Outputting a regular HTML page worked best for me. Excel interprets the formatting including CSS. And the code stays readable.

<?php

$countries = Array(
    Array('name' => 'France', 'capital' => 'Paris'),
    Array('name' => 'Germany', 'capital' => 'Berlin'),
    Array('name' => 'Switzerland', 'capital' => 'Berne'),
);

// make the browser open it in Excel
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: filename=countries.xls");

?>
<html>
    <head>
        <style type="text/css">
td {
    border: 1px solid blue
}
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th>Country</th>
                <th>Capital</th>
            </tr>
            <? foreach ($countries as $country): ?>
                <tr>
                    <td><?=$country['name'] ?></td>
                    <td><?=$country['capital'] ?></td>
                </tr>
            <? endforeach; ?>
        </table>
    </body>
</html>
Silvan Mühlemann
A: 

There is PHPExcel, a good library for writing Excel files in PHP.

Flo