JavaScript - Grabbing contents of .csv file and displaying as a table

Asked By Jeremy Osinsi on 09-Jul-08 02:47 PM
Is there a way to grab the contents of a .csv file and show them as an HTML table? Alternatively, is there a way to convert the .csv file to an XML file? From there, I have a transform method that applies a stylesheet to display the data. Any help would be greatly appreciated. (I know there is that GridTable tool with sorting, etc., with the code floating around the Web but I would like the data just to display in table form.)

csv to xml - Partha Mandayam replied to Jeremy Osinsi on 09-Jul-08 03:06 PM

Here's  a free csv to xml converter

http://www.creativyst.com/Prod/15/

csv to xml - Jeremy Osinsi replied to Partha Mandayam on 09-Jul-08 09:46 PM

Partha,

I've actually come across the Creativst converter. I think my situation is unique, where I need that conversion to happen automatically. We have an HTML form where data collects in a CSV file. I would like this CSV data displayed dynamically in an HTML table or in an XML file. (Our form servlet allows for a CSV reset when a button only viewable by authorized users is pushed.) While the site is built in HTML, I do have access to ASP and PHP servers.

Convert CSV File into HTML Table - alice johnson replied to Jeremy Osinsi on 09-Jul-08 10:04 PM

var src  = "sunw.csv";
var dest = "results.html";// The names of our input and output files
// Create some variables
var fso, fin, fout;
var data = new Array();
// Define constants for file access
var forReading   = 1;
var forWriting   = 2;
var forAppending = 8;
// Create File System Object and open input and output files
fso  = new ActiveXObject( "Scripting.FileSystemObject" );
fin  = fso.OpenTextFile( src, forReading );
fout = fso.OpenTextFile( dest, forWriting, true );
// Write out header and start of tablef
out.WriteLine( htmlHeader() );
fout.WriteLine( "<table border='0' cellpadding='1' cellspacing='0' width='100%'>" );
// Loop through entire file
while( !fin.AtEndOfStream )
{
try
 { 
// Read the next line
 var line = fin.ReadLine(); 
  // If line if blank - skip it 
if( line == "" )  
 continue;
data = line.split( "," );
fout.WriteLine( "<tr>" );
for( i = 0; i < data.length; i++ )
  {
fout.WriteLine( "<td>" + data[i] + "</td>" );
  }
fout.WriteLine( "</tr>\r\n" );
 }
catch( e )

WScript.Echo( "Error: " + e.description );
}
}
fout.WriteLine( "</table>" );
// Close HTML
fout.WriteLine( htmlFooter() );
// Close input and output file
fin.Close();
fout.Close();

Hello go through this link:

http://www.winscripter.com/WSH/FileIO/76.aspx

It will surely help you.

CSV to HTML - mv ark replied to Jeremy Osinsi on 10-Jul-08 03:00 AM
Generate an XML file in the background through ASP or PHP using a command line tool like one of these -
CSV2XML - http://www.a7soft.com/csv2xml.html
csv2xml - http://csv2xml.sourceforge.net/index.html

To convert the generated XML file to a Html Table, you can use the Generic XSL stylesheet mentioned here -
http://www.eggheadcafe.com/tutorials/aspnet/58b72f32-8da6-44e3-8c9a-61aebdf6be6b/xsl-transformation-to-htm.aspx