Dynamically display a CSV file as an HTML table on a web page

how to read a csv file in javascript with example
make a table in html to hold the following csv data
upload csv file javascript
javascript read and write csv file
javascript search csv file
javascript read local csv file
jquery read csv
import csv file using ajax

I'd like to take a CSV file living server-side and display it dynamically as an html table. E.g., this:

Name, Age, Sex
"Cantor, Georg", 163, M

should become this:

<html><body><table>
<tr> <td>Name</td> <td>Age</td> <td>Sex</td> </tr>
<tr> <td>Cantor, Georg</td> <td>163</td> <td>M</td> </td>
</table></body></html>

Solutions in any language are welcome.

The previously linked solution is a horrible piece of code; nearly every line contains a bug. Use fgetcsv instead:

<?php
echo "<html><body><table>\n\n";
$f = fopen("so-csv.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";

From CSV to HTML table (Simple), This code generates an HTML table based from a CSV file. This is from the tutorial by Christophe Viau. In trying his code example, I discovered  After installation copy the CSV file you want to display to the data/ folder and add the following JavaScript code to where you want to display the table. The table-container div will hold the displayed CSV table. In this example we will use the ‘countries’ CSV dataset from worlddata. A sample CSV data is shown below:

Here is a simple function to convert csv to html table using php:

function jj_readcsv($filename, $header=false) {
$handle = fopen($filename, "r");
echo '<table>';
//display header row if true
if ($header) {
    $csvcontents = fgetcsv($handle);
    echo '<tr>';
    foreach ($csvcontents as $headercolumn) {
        echo "<th>$headercolumn</th>";
    }
    echo '</tr>';
}
// displaying contents
while ($csvcontents = fgetcsv($handle)) {
    echo '<tr>';
    foreach ($csvcontents as $column) {
        echo "<td>$column</td>";
    }
    echo '</tr>';
}
echo '</table>';
fclose($handle);
}

One can call this function like jj_readcsv('image_links.csv',true);

if second parameter is true then the first row of csv will be taken as header/title.

Hope this helps somebody. Please comment for any flaws in this code.

Reading csv file using JavaScript and HTML5, You can also use jQuery to read csv data into HTML table. Reading a CSV File Using jQuery and Display into HTML Table · Exporting JSON to CSV); Auto- detect delimiter; Worker threads to keep your web page see, I am iterating csv file data and dynamically generating html table row using csv data. I’d like to take a CSV file living server-side and display it dynamically as an html table. E.g., this: Name, Age, Sex "Cantor, Georg", 163, M

phihag's answer puts each row in a single cell, while you are asking for each value to be in a separate cell. This seems to do it:

<?php
// Create a table from a csv file 
echo "<html><body><table>\n\n";
$f = fopen("so-csv.csv", "r");
while (($line = fgetcsv($f)) !== false) {
        $row = $line[0];    // We need to get the actual row (it is the first element in a 1-element array)
        $cells = explode(";",$row);
        echo "<tr>";
        foreach ($cells as $cell) {
            echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}
fclose($f);
echo "\n</table></body></html>";
?>

Quick way to display CSV files as HTML tables, data to HTML. One of my current favorite is the csv-to-html-table library. This allows you to easily integrate CSV files into your web pages. In simple words we will parse CSV file data and display into HTML table format on web page by using Ajax JQuery method. Here We will not use any JQuery plugin or library for read CSV file and display CSV file data in HTML table.

Just improved phihag's code because it runs into a infinite loop if file not exists.

<?php

$filename = "so-csv.csv";

echo "<html><body><table>\n\n";

if (file_exists($filename)) {
$f = fopen($filename, "r");
while (($line = fgetcsv($f)) !== false) {
        echo "<tr>";
        foreach ($line as $cell) {
                echo "<td>" . htmlspecialchars($cell) . "</td>";
        }
        echo "</tr>\n";
}
fclose($f);
}

else{ echo "<tr><td>No file exists ! </td></tr>" ;}
echo "\n</table></body></html>";
?>

How to Load CSV File data into HTML Table Using AJAX jQuery , arrow_down_small: Display any CSV (comma separated values) file as a searchable, filterable, pretty HTML table Display any CSV file as a searchable, filterable, pretty HTML table. Want to embed your nifty table on your website? Inside the function, first a check is performed to verify whether the file is a valid CSV or a text file. Then a check is performed to make sure whether the browser supports HTML5 File API. Once the above checks are cleared then the contents of the CSV file are read as text string and then the string is split into parts using comma and new line characters and finally is displayed as an HTML Table.

Does it work if you escape the quoted commas with \ ?

Name, Age, Sex

"Cantor\, Georg", 163,M

Most delimited formats require that their delimiter be escaped in order to properly parse.


A rough Java example:

import java.util.Iterator;

public class CsvTest {

    public static void main(String[] args) {
        String[] lines = { "Name, Age, Sex", "\"Cantor, Georg\", 163, M" };

        StringBuilder result = new StringBuilder();

        for (String head : iterator(lines[0])) {
            result.append(String.format("<tr>%s</tr>\n", head));
        }

        for (int i=1; i < lines.length; i++) {
            for (String row : iterator(lines[i])) {
                result.append(String.format("<td>%s</td>\n", row));
            }
        }

        System.out.println(String.format("<table>\n%s</table>", result.toString()));
    }

    public static Iterable<String> iterator(final String line) {
        return new Iterable<String>() {
            public Iterator<String> iterator() {
                return new Iterator<String>() {
                    private int position = 0;

                    public boolean hasNext() {
                        return position < line.length();
                    }

                    public String next() {
                        boolean inquote = false;
                        StringBuilder buffer = new StringBuilder();
                        for (; position < line.length(); position++) {
                            char c = line.charAt(position);
                            if (c == '"') {
                                inquote = !inquote;
                            }
                            if (c == ',' && !inquote) {
                                position++;
                                break;
                            } else {
                                buffer.append(c);
                            }
                        }
                        return buffer.toString().trim();
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}

derekeder/csv-to-html-table: Display any CSV (comma , How can I display a specific data from this data set in a web site like: script uses a jQuery component to create a table on your HTML page. @davidkonrad is correct. With working on a local file, your machine is "the server", even though it's not deployed anywhere. Whether it's on a server, or just a raw HTML file in a folder on your machine, with the CSV sitting right next to it are inconsequential (other than the URL to access said CSV). – krillgar Sep 5 '14 at 13:04

Displaying specific data from a CSV file - JavaScript, In this program, the contents of a CSV file were read and it was displayed in an HTML table. It's not terribly different than reading any other text file, but there are   My HTML page should continuously look into this CSV and automatically display new data if that CSV file is updated User should be able to see new Data without any refreshing. I have come across various web-advises however all of them suggest to update Web page based on JS code, however, data are created randomly by those JS code only - i.e. no

How to Read CSV Data in PHP for HTML5 and CSS3 Programming , how to display only last few lines of csv? Like · Reply · 2y. On clicking the button, exportTableToCSV() method is called to export table data to CSV file. Also, the desired filename for download CSV file is passed to this function. < button onclick = "exportTableToCSV('members.csv')" >Export HTML Table To CSV File</ button > Conclusion. Hope using our minimal JavaScript code you can easily export table

CSV File to HTML Table Using AJAX jQuery, Description CSV to html makes it easy to fetch content from csv-file (s) and put content from that file/those files and display the html (table) on a page with a single shortcode. If you created files to use with the Visualizer Plugin, those are formatted in a specific way and if you have saved the csv-file (s) from excel, the csv looks

Comments
  • Brilliant! Thank you so much. I think I'll go ahead and remove that link from the question then.
  • Just want to say, this is best code I've found so far - well done sir!
  • Just a copy paste away! Thanks! :)
  • @phihag. Could you please provide an example to use your code? Thanks in advance
  • @ddpd You can create a new php file, insert this code, and run it. It's already an example ;)
  • I don't think the sample solution handles that. But I also don't think that escaping the commas should be necessary, since they're in quotes. (Escaping quotes, on the other hand, yes.) For example, mysql exports to CSV without escaped commas.
  • Php that generates it server-side per web request was good enough for me. If you have an ajaxy solution it would be great to include it here for others searching for this later.
  • He said "dynamically", your solution doesn't allow for that