How to embed a CSV file into HTML?
Hi Chaps
One of my clients is interested in embedding an excel spreadsheet into one of their web pages. As you might imagine I'm not filled with joy at this thought.
My question is, if they convert their spreadsheet to a CSV file - what would be the best way for me to show that on the page? Can it be embedded directly? Should I use PHP to do it?
I'm trying to make their life as easy as possible but also still have a site with valid, accessible code.
All suggestions gratefully accepted.
One of my clients is interested in embedding an excel spreadsheet into one of their web pages. As you might imagine I'm not filled with joy at this thought.
My question is, if they convert their spreadsheet to a CSV file - what would be the best way for me to show that on the page? Can it be embedded directly? Should I use PHP to do it?
I'm trying to make their life as easy as possible but also still have a site with valid, accessible code.
All suggestions gratefully accepted.
You could use <object> though I'd suggest PHP since not everyone has Excel or the same browser type.
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
convert to csv
parse csv data through php to a html page
sorted!
http://www.opsi.gov.uk/acts/acts1995/Ukpga_19950050_en_8.htm#mdiv57
parse csv data through php to a html page
sorted!
http://www.opsi.gov.uk/acts/acts1995/Ukpga_19950050_en_8.htm#mdiv57
Thanks guys - that's sort of the answer I was looking for. Now I just have to figure out exactly how to do that as I've never done it before!
Life's just one big adventure isn't it?
Life's just one big adventure isn't it?
This is a really crude demo to generate a table from a *.csv.
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
| Code: |
| <?php
$cnx = fopen("example.csv", "r"); //open file echo("<table border='1'>"); // echo table while (!feof ($cnx)) { // while not eof $buffer = fgets($cnx); // get contents if ($buffer != "" ) // When buffer is empty close table tag { echo("<tr><td>$buffer</td></tr>"); } }; echo("</table>"); fclose($cnx); ?> |
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
Thank you Robert - very kind of you!
I managed to dig up this too before I saw your post but I'm not good enough at PHP to know which is the better option?
I managed to dig up this too before I saw your post but I'm not good enough at PHP to know which is the better option?
| Code: |
| <?php
$filename = "products.csv"; //here's the filename $id = fopen($filename, "r"); //open the file while ($data = fgetcsv($id, filesize($filename))) //start a loop $table[] = $data; //put each line into its own entry in the $table array fclose($id); //close file echo "<table>\n"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) echo "<td>$data</td>"; echo "</tr>\n"; } echo "</table>\n"; ?> |
Well, the one I did above was crude this version is better as it actually generates a table and uses explode.
As to which is better it's probably down to taste.
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
| Code: |
| <?php
$cnx = fopen("example.csv", "r"); //open example.csv echo("<table border='1'>"); // echo the table while (!feof ($cnx)) { // while not end of file $buffer = fgets($cnx); // get contents of file (name) as variable $values = explode(",", $buffer); //explode "," between the values within the contents echo "<tr>"; for ( $j = 0; $j < count($values); $j++ ) { // echo("<td>$values[$j]</td>"); } echo"</tr>"; }; echo("</table>"); fclose($cnx); //close filename variable ?> |
As to which is better it's probably down to taste.
};-) http://www.xhtmlcoder.com/
WVYFC chose the Yorkshire Air Ambulance as the main charity to fund raise for in 2006
Thank you Robert - this is one of the reasons I love this job - I get to learn new, clever stuff all the time!
RFC 4180 describes a standardised form of the various proprietary Comma Seperated Values (CSV) file formats. There's also a Comma-seperated Values on Wikipedia page.
I think you might need a byte-by-byte parser to cope with values containing newlines which have been encapsulated in speechmarks. From the Wikipedia page:
Note that in the entry for 1996, the fourth column value has a newline after the first two words. If you know your input won't contain newlines then a simple parsers should be robust enough. You need to detect newlines in the format as being the starts of new rows.
Do these files always have the same headers? You really need a <thead> section with <th> elements for data tables.
I think you might need a byte-by-byte parser to cope with values containing newlines which have been encapsulated in speechmarks. From the Wikipedia page:
| Code: |
| 1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""",,4900.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00 |
Do these files always have the same headers? You really need a <thead> section with <th> elements for data tables.
I've tweaked it to give it a THEAD and the first row THs - Thanks - I'm looking at the possibilities as far as making it more idiot proof!
Perhaps not relevant here, but I have found Excel to MySQL useful.
| kiwibrit wrote: |
| Perhaps not relevant here, but I have found Excel to MySQL useful. |
/bookmarked
Thanks for that, it looks like a very useful tool.
thanks Robert Wellock
ur code helped me lot. could u please help me upgrading the code.
i would like to have the color formatting, bold, italics... in the .csv also in the webpage.
is it possible?
ur code helped me lot. could u please help me upgrading the code.
i would like to have the color formatting, bold, italics... in the .csv also in the webpage.
is it possible?
The .csv format does not support formatting. It only contains the raw data, like my sample from October 2006 showed.
Nice and great post.
Thanks & regards.
Thanks & regards.


