Export To Excel With Yii

I have a number of custom tables which are printed out as html. Which is the best way to just send the same html code for these tables and have them exported to Excel?

Searching a bit on the Yii pages and found a number of different extensions, but I am not really sure which one fit my need the best:


Any ideas what I should go for?


I used eexcelview (http://www.yiiframework.com/extension/eexcelview/) and tlbexcelview (http://www.yiiframework.com/extension/tlbexcelview/). Both are good but tlbexcelview is slight easy to configure and customize.

YiiReport (http://www.yiiframework.com/extension/yiireport/) is quit impressive but it request to install extention yiiexcel(http://www.yiiframework.com/extension/yiiexcel/), which is not applicable for already created applications (Please read documentation and comments of the addon).

I looked closer at all these Yii Excel export extensions, but I don’t believe that any of them is what I am looking for. These all seem to be passing dataProvider or the model directly.

In my application I already have a number of tables, e.g.

Can I somehow add an export button next to a table like above which will render the Excel file based on the table?

I have spent the evening reading the PHPExcel documentation and I must say that this plugin seems OK, but really unnecessarily complex in my opinion. There is simply no native support for exporting a HTML table to Excel, and considering that the way I print out my tables can sometimes differ from the way they are retrieved from the DB, I really want my HTML table exported.

I found an alternative solution where you can export to Excel through jQuery with a few lines of code, but with the big disadvantage that it does not work with IE at all and that you get a warning message as of Excel 2010. I guess that client side export to Excel is not optimal either.


Does anyone know of a solution where you can export a HTML table to Excel? I can’t believe that nobody has run into this issue before.

Sorry for bumping this again. But there must be a way to solve this…

Have you not gone through with that wikki article ??

Since, i have the same situation that’s why i have draft all that work in this wikki article as i did in my project.

Thank you for sharing this wiki. Can you please clarify a bit closer related to step 2, I have extracted PHPExcel to the extensions folder so that it looks like this:

However, when I try to import it, it does not find the file:

PS. If I can get the PHPExcel working I seem to have found a way to export a HTML table directly to Excel with this plugin, and will share it once done :)

Just given me few minute here…i sharing few more code for it.And for sure…your problem will be sort out…:)

In Extension directory you have to manage this plugin something like

protected/extension/phpexcel/<here you need to put phpexcel class>

then you can import it like


else you need to change this code


according to your directory structure.Since the error message which is coming here is due to incorrect path for phpexcel directory.

So, please try to do that changes in your import statement and also in your extension folder.

Sorry now i can not access my codes that’s why i am unable to share few more codes.But i am sure if you will rearrange your extension directory as i suggest or change import statement then for sure it will work.

Also please make sure you have read all steps carefully.Since i have provide enough information to implement that phpexcel.:) else keep posted here about your progress.

And also i would love to see an extension for it.Since due to lake of time i could write extension for that.so,i would be great if you can provide us that extension :)

How about the dataTables plugin?

I am using this in one of my apps.

Hey yii_king,

Did you solve your issue or still having problem ??

Thank you codesutra, I was able to import your extension according to your instructions. Based on your solution I have created an ExportController with a method ExportToExcel($htmltable). When you pass the html to the method it takes care of exporting to Excel. However, I still have a problem in that Excel 2007 generates this error message:

It currently has two known issues:

When I click Yes, then it shows the content in Excel.

Here is the ExportController (the method is based on a forum post I found when looking a bit:

Any ideas of how to get these two issues working?

I think you have to check phpexcel library options thoroughly to create excel file with html table.As i know they have a demo example for it also.

So,i would recommend you to check phpexcel library again.

Since, this error is showing that at the time of creating xml from html there is some syntax error. Might be it is related to html markup or some special character if you are using in it.

So, i hope your Yii part of phpexcel is done only thing is that you need to spend some time with phpexcel library.so, that you can handle these issues.

The issues I had simply had to do with the fact that my table was not properly generated. When passing a HTML table which looks like this, then it actually works both in IE, FF etc.


you can first create the herf and call the onlclick function

 <a onclick="exportcsv(<?php echo $vid; ?>)" href="javascript:void(0)" id="add" class="blue">Export CSV</a>

 function exportcsv(vid)


        var value = $("#search_value_liquor").val();

        var search_type=$("#search_type").val();

        window.location = "<?php echo CController::createUrl("ManageTicket/index",array('vid'=>$vid,'flag'=>'export')); ?>&value="+value+"&type="+search_type;



and in actionIndex you want to just simple PHP code

 $fileName = 'manage_ticket_type.csv';

                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

                header('Content-Description: File Transfer');

                header("Content-type: text/csv");

                header("Content-Disposition: attachment; filename={$fileName}");

                header("Expires: 0");

                header("Pragma: public");

                //$fp = @fopen( 'file.csv', 'w' );

                $fh = @fopen('php://output', 'w');

                $headerDisplayed = false;

                foreach ($data_arr as $fields) {

                    //fputcsv($fp, $fields);

                    if (!$headerDisplayed) {

                        // Use the keys from $data as the titles

                        fputcsv($fh, array_keys($fields), ",", '"');

                        $headerDisplayed = true;


                    // Put the data into the stream

                    fputcsv($fh, $fields, ",", '"');




$data_arr in your adminGrid array…

i hope it may be helpful…

Ahh ok,

So, it means now everything working right ?? :)

@Ankit In this thread we are discussing about Excel file export issue.so there is nothing related to CSV.Anyways thanks for your input ;)

So I have been testing this a bit with different tables and the class is actually working quite well.

The disadvantage so far is that if I have nested tables, then the script will consider each table as a separate table and will create a separate worksheet for these as well.

Well, Spend some more time with it.i hope you will also succeed to make it happen.you know why i am saying this because i have created 8 tables in a one sheet ;)