Make tabular Data downloadable as a CSV file - mysql

I have a website which offers data in standard HTML table form and is displayed across 7 columns. At the backend, data is stored in MySQL and displayed through PHP on the webpage.
Site visitors have been demanding that data be made available for download in a CSV file, and I am OK to offer that feature.
What is the best way to offer a limited number of columns (say 3 out of 7 columns) with a one-click download into CSV file?
Note: this question is NOT about site visitors scraping data using python or other scripts, but it is about a webmaster willingly offering the option to download data in a CSV file.
Searched for and checked this thread XML or CSV for "Tabular Data", but it does not have a precise answer, hence the question.

Create a new PHP file (page) that instead of rendering the content as HTML will render it as a CSV. Then manipulate response headers to make clear to the browser that it is a file meant to be downloaded. This is how your .php file should look like:
<?php
header('Content-Description: File Transfer');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="file.csv"');
// ... QUERY FOR DATA
$outstream = fopen("php://output", 'w');
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals, ',', '"');
}
// CSV header
__outputCSV(['header1', 'header2', ...]);
// CSV body
array_walk($data, '__outputCSV', $outstream);
fclose($outstream);
?>

Related

Is there a good alternative for embedding a PDF with HTML next to using a local file path, online file path or data source as base64-string?

I am building a web app and I would like to show PDF files to my users. My files are mainly stored as byte arrays in the database as they are generated in the backend. I am using the embed element and have found three ways to display a PDF:
Local file path in src attribute: Works, but I need to generate a file from the database byte array, which is not desirable as I have to manage routines to delete them once they are not needed anymore.
Online file path in src attribute: Not possible since my files may not be hosted anywhere but on the server. Also has the same issues as the previous method anyway.
Data as base64 string in src attribute: Current method, but I ran into a problem for larger files (>2MB). Edge and Chrome will not display a PDF when I covert a PDF of this size to a base64 string (no error but the docs reveal that there is a limit for the data in the src attribute). It works on Firefox but I cannot have my users be restricted to Firefox.
Is there any other way to transmit valid PDF data from a byte array out of the database without generating a file locally?
You have made the common mistake of thinking of URLs and file paths as the same thing; but a URL is just a string that's sent to the server, and some content is sent back. Just as you wouldn't save an HTML file to disk for every dynamic page on the site, you don't have to write to the file system to display a dynamic PDF.
So the solution to this is to have a script on your server that takes the identifier of a PDF in your system, maybe does some access checking, and outputs it to the browser.
For example, if you were using PHP, you might write the HTML with <embed src="/loadpdf.php?id=42"> and then in loadpdf.php would write something like this:
$pdfContent = load_pdf_from_database((int)$_GET['id']);
header('Content-Type: application/pdf');
echo $pdfContent;
Loading /loadpdf.php?id=42 directly in the browser would then render the PDF just the same as if it was a "real" file, and embedding it should work the same way too.

Value of input is from a file

<input id="LoadText" value= 'data.txt' />
I am trying to give the default value of the LoadText input to the contents of the 'data.txt' file.
Note: This is a solution, if the file data.txt is stored on the local file system of the client, if it is stored on the server, please have a look at Daniel's solution...
To read a local file into your site there a mainly two possible solutions:
Uploading the file to the server and download it again with AJAX
Use the File API. The current support for it can be seen on this site: http://caniuse.com/fileapi
If you store your HTML file also on your local file system (and you never want it to be stored on a server), you can also directly use AJAX. Here is a code using jQuery:
$.ajax({
url: "data.txt",
dataType: "text",
success: function (data) {
// do something with the text of data.txt (which is stored in the value data)
}
});
The files data.txt and your HTML file must be stored in the same directory for this.
You need to use a dynamic programming langauge to achieve this behaviour. You could use a serverside technology like PHP to read the content of data.txt and insert its content into the html before sending the file to the client. Or you can use clientside technology like Javascript to load the data.txt file speratly and insert it into the input form afterwards.
Update PHP example:
If your webspace/server/etc supports PHP you could simply write something like this:
<input id="LoadText" value= '<?php echo file_get_contents('data.txt'); ?>' />
But you need to take care of the content of data.txt . The file could contain anything and therefore break your design. Just imagine the following content:
' /><any tag and code you do not want in your design

Streaming CSV to browser

Busy building a website for a client using classic ASP (It will reside on an old server) which is going to be used internally only.
The admin is able to view a paginated table of data and export this to CSV. This works fine when I save the CSV data to a CSV file but I have now been asked to try avoid the creation of the file if possibly and create the CSV in memory without the need for a file.
I have my doubts that this is possible but might be completely wrong. Is there anyway to send the CSV data to the browser such that it will open in Excel rather than having to create a CSV file and link to it as I am currently doing ?
TIA
John
Response.ContentType = "text/csv" will help you here. In the past I've paired that with a rewrite rule so that the URL is something like foo.com/example.csv but there are plenty of other ideas to be found in the following question: Response Content type as CSV

Javascript Writing to PDF or Excel

Hey Related to your answer on Can I write files with HTML5/JS?
How would I modify this for say Excel or PDF. I tried but was unable to get it to work. I get a corrupt file downloaded when I change MIME to application/pdf
I am trying to link it to data stored in localstorage which I have all in a variable.
My current code is:
function setSaveFile(contents, file_name, mime_type) {
var a = document.getElementById('save');
mime_type = mime_type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // text/html, image/png, et c
if (file_name) a.setAttribute('Log.xls', file_name);
a.href = 'data:'+ mime_type +';base64,'+ btoa(contents || 'Description' + ' ' + 'Notes' + ' ' + 'Date\n\n' + pdftimeLog);
}
I want to be able to export it as a CSV with each of those headings: DATE, description, notes in different cells. I want it mobile friendly.Thanks
Excellent Options for CSV Everyone:
http://snapshotmedia.co.uk/blog/jspdf
Is it possible to use any HTML5 fanciness to export local storage to Excel?
http://css-tricks.com/localstorage-examples/
Local Storage manipulation and possible sending
Thanks!
For a file to be a valid pdf or excelfile their contents need to conform to the standard that defines the respective format, just changing mimetypes won't do much good.
For PDF there's a library jspdf that might work for you.
I haven't seen a solution for Excel yet, it will be a whole lot harder as the (xls) file format is quite complicated, for the time being it's probably preferable to generate the file on the server.
As an alternative to CSV, it's also possible to write an Excel file by writing HTML to a file with a .xls extension. Excel opens it up based on the extension. Haven't yet explored how deeply it can paginate, format.

Best way to store data retrieved from JSON file?

I'm hoping to store data I get from a server which sends data via JSON. I don't want anything fancy - just would like to save the data so I can play with it in excel.
Here is the JSON URL: http://realm3.castle.wonderhill.com/api/map.json
I'm extremely surprised there are no solutions out there on this yet.
What would you guys used to achieve this?
Convert the json into a text file formatted as CSV - Excel can read that. I'll come up with some sample code in PHP after dinner.
EDIT: Bah, dinner can wait.
<?php
// Download data to a string
$mapData = file_get_contents('http://realm3.castle.wonderhill.com/api/map.json');
// Convert JSON into an Array
$mapData = json_decode($mapData);
var_dump($mapData);
echo "\n";
Saved the above into test.php and then ran it like this:
php test.php | less
and the output is a huge data structure. You'll need to extract what you want and then use fputcsv() to write the content to a file that you'll then read into Excel. The output doesn't seem to have any special characters, but if you do have a problem make sure to encode the data as CP1252 so Excel for Windows can read it.