9

I'm trying to import a local CSV file with headings into a local HTML file, which will then display as a table in a browser.

I haven't been learning HTMLand JavaScript for long, so I don't know a lot about importing and converting. What I need is some advice or possibly a basic script describing the sort of function I need. Explanations and advice are all welcomed!

This is an example of the csv file:

    heading1,heading2,heading3,heading4,heading5
    value1_1,value1_2,value1_3,value1_4,value1_5
    value2_1,value2_2,value2_3,value2_4,value2_5
    value3_1,value3_2,value3_3,value3_4,value3_5
    value4_1,value4_2,value4_3,value4_4,value4_5
    value5_1,value5_2,value5_3,value5_4,value5_5

This is how I'd want to display it:

http://jsfiddle.net/yekdkdLm

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
Roy Davies
  • 101
  • 1
  • 1
  • 4
  • with local, you mean a CSV on the server, right? – davidkonrad Sep 05 '14 at 11:04
  • no i mean a simple csv text file in the same folder as the html file, im still kinda of learning, but all the places i've looked have not really explained what they did or how to then access that data – Roy Davies Sep 05 '14 at 11:08
  • [Here](https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications) You can find example of access to the local file from browser. You just later need to write code for split and creating table. – szpic Sep 05 '14 at 11:33
  • Mmm that page is pretty good, seems like there's plenty of ways to do this but they are all pretty longwinded – Roy Davies Sep 05 '14 at 11:49
  • @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 05 '14 at 13:04
  • There are also tons of CSV-related tools such as this one online (TextFixer.com) and many others that are dedicated to converting your CSV data to a specific format like you are trying to achieve with a table. TextFixer.com converts CSV text files to a HTML table. – fionaredmond Dec 30 '15 at 21:52
  • As far as I know there is another solution for loading local csv files (no server involved) into an html file that is kind of trivial and only needs to modify the file.csv in a convenient way to allow the javascript to be able to get the csv content. And also you don't even need the file extension to be .csv. In fact can be something else too and it still works. – willy wonka May 29 '23 at 04:26

5 Answers5

5
<div id="CSVTable"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//jquerycsvtotable.googlecode.com/files/jquery.csvToTable.js"></script>

<script>
$(function() {
  $('#CSVTable').CSVToTable('your_csv.csv');
});
</script>

you can use jquery.csvToTable.js to display csv file in html

Ankit Agrawal
  • 6,034
  • 6
  • 25
  • 49
  • 1
    The original logic still works, but for anyone else who needs it, you'll need to replace the headers with something more recent: ``` ``` – Nicholas Barrow Mar 14 '23 at 16:47
0

I don't think there is a trivial solution to this. An insistence on using client-side JavaScript makes this a more difficult task than doing the processing on the server-side and simply serving up the HTML.

You first need to use JavaScript to fetch the file from the server, the easiest way to do this is with the jQuery library. Next you need to take the data and construct the HTML in the existing document, again, jQuery will simplify this for you.

If you are still learning, I'd recommend skipping the first bit, and simply create a JavaScript variable with the data already in it. This way you can write the code to build the table, get this working, then go back to worrying about how you would actually fetch that from the server using AJAX.

Alternative, look at using a server-side language such as PHP which will incorporate the data into the page before delivering it to the browser. Without knowing more details, this would seem like the more logical solution.

Philip Wattis
  • 43
  • 1
  • 6
  • Ahh i see, no wonder i couldn't find a simple solution. Is this the case with all imported files of this type? – Roy Davies Sep 05 '14 at 11:23
  • It's not the type of file that causes the difficulty. It is because the CSV file is on the server, but the JavaScript will be running on your local machine. You require AJAX to fetch the file from the server, regardless of the format of the file. – Philip Wattis Sep 05 '14 at 14:54
0

You need to use javascript(jquery) or php This is the code to open with php and get the values in a table

<table>
<tr>
    <td>Header 1</td>
    <td>Header 2</td>
</tr>
<?php
    $fp = fopen ( "file.csv" , "r" );
    while (( $data = fgetcsv ( $fp , 1000 , "," )) !== FALSE ) {
        $i = 0;
        echo "<tr>";
        foreach($data as $row) {
           echo "<td>" . $row . "</td>";
           $i++ ;
        }
        echo "/<tr>";
    }
    fclose ( $fp );
    ?>
</table>
Javier
  • 103
  • 11
0

Fetch an external file.

You have to use xmlHttpRequest for this. Simplified using jQuery (include jQuery library) as.

You will need to run the HTML file in a local server like Apache, browsers like Chrome doesnt allow xmlHttp for file:// urls.

  $.ajax({
    type: "GET",
    url: "words.txt",
    dataType: "text",
    success: parseTxt
  });

Use a success callback to process the data

parseTxt is the function to which the content of the file is read and passed. You can then write the code in parseTxt to process the text as string.

function parseTxt(text){
  var rows=text.split('\n');

  //for each row
  //cols=rows[i].split(',');
}

This should be enough to get you started I guess.


How to read a text file from server using JavaScript?

You can even try the answer to above question by Shadow Wizard using iframes.


A RAW XMLHttpRequest can be made without jQuery as shown here

Community
  • 1
  • 1
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

I used PHP to parse the CSV file on the server side, then format that output as HTML. Along the way, it turns the unique values in the CSV columns into unique filter values to refine the result set.