Export to Excel Functionality

Permalink
Hi People - Hoping someone can help me figure out why my Export Button functionality is lost in concrete5 environment.

My website takes bookings. the data is stored in a table in the dashboard. I want to be able to convert this into an excel file using this method>>
http://www.mostlikers.com/2015/01/export-html-table-data-to-csv-fil...

I have tested the code and so i know it works correctly.

For some reason when i implement this on my page, the excel file does not launch, instead user is directed to exporttoexcel.php -- Page Not Found!!

I can only assume there must be a setting in some config file somehwere which i either need to add or turn on - in order to allow this functionality. If anyone has expereince of this, or can advise me on how to achieve - pls pls reply -- i look forard to hearing it.

Many Thanks, Paul

 
JohntheFish replied on at Permalink Reply
JohntheFish
At a guess, you need to set up the route to the exporter. There is stuff about setting up routes in the documentation.
PMorrison replied on at Permalink Reply
Thx -- I will take a look.
I find much of the documentation aimed at someone with a higher level of undertsanding..
which is why i asked the question on here instead...
JohntheFish replied on at Permalink Reply
JohntheFish
I agree about the quality of documentation. Whats there splits into 2 generic documentation mistakes: either example with no explanation - do this, do that, you have just created.... , but why?, or specification with no example, missing both why and how.
Comments in the core code are pretty useless, being largely limited to @perldoc - generated by IDEs for IDEs and used as an excuse for real explanatory comment.

You could also look at marketplace packages that provide routes for ajax or downloads. There could be some free ones that have better comments than the core.
PMorrison replied on at Permalink Reply
Hi

I made the mistake of putting the button code/functionality in the controller...
I have now put the button code in the single-page file instead.
My 'exporttoexcel.php' is in the same directory ie the single-pages directory.
The button still 404s when clicked.

It was suggested i may need to set up a route for this to work.
I have consulted the documentation but am not understanding.

Grateful to anyone who might be able to advise... many thanks.
Gondwana replied on at Permalink Reply
Gondwana
This almost certainly won't help you, but here's a snippet from a single page view.php of mine that includes a download button:
<form id="download_id" method="get" action="/exerbyte/data/<?=$filename?>" style="text-align: center; margin-bottom:2em;">
                <button type="submit" name="download-btn" class="btn btn-primary">Download</button>
            </form>

This presumes that a file is to be downloaded, and is stored at "/exerbyte/data/<?=$filename?>".
PMorrison replied on at Permalink Reply
Hi -- This is not the solution to my problem. But I really appreciate you trying to help.

The issue I have is that when the button is clicked it launches exporttoexcel.php as if it were a page eg. domain.com/.../.../exporttoexcel.php which needless to say 404s.

I know the code is correct however as this works on a static site.
When I try to implement into the mvc framework it doesn't work. So I am completely lost now. It has been suggested that I may need to set up a route to the exporter --- but I don't know what this means or what I would need to do.. :-(
Gondwana replied on at Permalink Reply
Gondwana
Okay, here's another non-solution... :)

You could use AJAX:
https://documentation.concrete5.org/developers/working-with-blocks/c...

I know that this applies to a block, but I've got it working for a single page (similar to a dashboard page). It puts the php response code in the controller as you want. It also avoids reloading the whole page.

I can't comment on how easy it would be to integrate this approch into the export script since I find the latter hard to read.
PMorrison replied on at Permalink Reply
Again, i appreciate your help, but these so-called solutino just seem so complex...

The BASIC issue i have is with this -- <form action="exporttoexcel.php">

The button click sends user to 'exporttoexcel.php' page which obviously 404s.
What it should do instead is open Excel.

This works on a static site.
It doesnt work in the concrete5 framework!!!
Now there's got to be a simple reason for this -- I'm just not savvy enough to realise what the solution is.
PMorrison replied on at Permalink Reply
Okay as this has seemed to baffle the entire community, I had decided to use a different method -- which seems to work!!

<a id="dlink" style="display:none;"></a>
<input type="button" onclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel">

var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name, filename) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }

document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();

}
})()
Gondwana replied on at Permalink Reply
Gondwana
The basic problem is probably that c5 is looking in a different directory for exporttoexcel.php. You could try copying that file to other directories within your site until you find which one works, or put a complete URL in the action="".

Yes, those are ugly approaches too!

Executing the form action can't open Excel directly. All it can do is to try to load that php file. It's the php file's job to do something to cause Excel to load (eg, by returning a spreadsheet). But you probably knew that.

Anyway, I think I prefer your new solution since it avoids reloading a whole page. :)