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.

3
  • Do you have a data store in the application? What are you using for it? (MYSQL, SQL Server, etc). Commented Jul 30, 2013 at 13:23
  • @evilbhonda - I am using MS SQL server 2008. Commented Jul 30, 2013 at 13:42
  • Possible duplicate of exporting html table to excel using jquery Commented Nov 23, 2015 at 8:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this is interesting but would require me to rewrite my application. it is the existing html table that I would like to export.
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!
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);
        }
    }
}

2 Comments

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.
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.