0

I would like to export a HTML table to Excel.

I have a looked at various client side options with jQuery plugins to export HTML tables to CSV and Excel, but these are not good as they are not supported in IE and in addition gives a warning message in all browsers. Also for larger Excel files, client side export is not optimal.

I have also looked at PHPExcel as I am using PHP, but there does not seem to be any native support for exporting a HTML table to Excel. And considering that I have custom tables which are modified based on input from the database, I cannot print the database object directly to PHPExcel.

I am using the Yii framework with a MVC approach.

So what I really want is to be able to export my HTML tables to Excel. Any ideas of how to do this?? I really need help with this one.

Qiu
  • 5,651
  • 10
  • 49
  • 56
mediasurface
  • 47
  • 1
  • 11

3 Answers3

0

You could try using SQL Reporting Services. It's a bit heavy, but you could create the HTML tables as a report. Using Reporting Services you could then render out the report as an Excel document (or PDF, or an image, etc.).

I've found it to be the easiest way to get a consistent output into documents like that.

I've not tried calling Reporting Services from PHP, but I'm sure it can be done.

There's a blog article by Brian Swan of MS that explains it -

http://blogs.msdn.com/b/brian_swan/archive/2010/09/23/rendering-sql-server-reports-as-excel-documents-with-php.aspx

I've not tried it myself though, so I can't vouch for it, but may be helpful for a starting point.

Jamie Burns
  • 1,258
  • 9
  • 21
  • this is interesting but would require me to rewrite my application. it is the existing html table that I would like to export. – mediasurface Jul 30 '13 at 14:12
  • It wouldn't require rewriting, but it would require extra code to generate the output. I'd prefer it that way, because it separates what you display for the web and what you export to Excel, which may be different, especially if you start enhancing the web version to include features you wouldn't want to export (links, buttons, etc). I agree though, it's a lot of effort if you've already got the table there! – Jamie Burns Jul 30 '13 at 14:23
0

Have you considered exporting it as CSV?

Excel can import CSV and make a table of the data (ofc without borders and other decorations).

I made a PHP class for building spreadsheets, maybe it could help you:

<?php

class Spreadsheet {

    private $grid = array();

    public function setCell(/*int*/ $x, /*int*/ $y, /*mixed*/ $value) {
        for($yy=0; $yy<=$y; $yy++) {
            if(!isset($this->grid[$yy])) {
                $this->grid[$yy] = array();
            }
        }

        for($xx=0; $xx<=$x; $xx++) {
            if(!isset($this->grid[$y][$xx])) {
                $this->grid[$y][$xx] = null;
            }
        }

        $this->grid[$y][$x] = $value;
    }

    // final command executed, including "exit".
    public function sendAsCsv(/*string*/ $filename) {
        header('Content-type: text/csv; charset=utf-8');
        header('Content-Language: en');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header("Pragma: no-cache");
        header("Expires: 0");

        ob_end_clean();

        $outputBuffer = fopen("php://output", 'w');

        $this->appendToFile($outputBuffer);

        fclose($outputBuffer);
        exit;
    }

    public function appendToFile(/*resource*/ $fileHandle) {
        foreach($this->grid as $val) {
            fputcsv($fileHandle, $val);
        }
    }
}
MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • nice class. I guess using this would require me to go back to each table and output the cells first, then sendAsCsv and then appendToFile. However, I would prefer just exporting the existing HTML table instead. – mediasurface Jul 30 '13 at 14:15
  • the appendToFile method is mostly for internal use, you only need to set the cells, and then do sendAsCsv("myfile.csv"). Using this class will certainly be much easier than trying to dig out the data from already generated html tables. I'm not even sure if jQuery can generate a CSV file for direct download - which here you get by calling the sendAsCsv method. – MightyPork Jul 30 '13 at 14:21
0

You can try this plugin, it also has IE support - tableExport.js

HTML:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="src/jquery.table2excel.js"></script>
<body>
<tr class="noExl">
  <th>#</th>
  <th>Column heading</th>
  <th>Column heading</th>
  <th>Column heading</th>
</tr>
</body>

jQuery:

$("button").click(function(){
  $("#table2excel").table2excel({
    // exclude CSS class
    exclude: ".noExl",
    name: "Excel Document Name"
  });
});

Plugin download: http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html

Leyon Gudinho
  • 91
  • 2
  • 11