Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







Script: Convert CSV File into HTML Table

Posted by on Sunday, January 18, 2004 (PST)

Script: Converts a Comma Seperated Value (CSV) formatted file into an HTML table.

 

/**
 *    Name:       csv2html.js
 *    Author:     Daren Thiel
 *    Date:       5/30/00
 *    Web site:  
http://www.winscripter.com
 *   
 *    Comments:   Converts a Comma Separated Values(CSV)
 *                file into an HTML table.
 *  
 **/
// The names of our input and output files
var src  = "sunw.csv";
var dest = "results.html";
// 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; // not used - given for reference
// 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 );// create file if not found
// Write out header and start of table
fout.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;
  
  // Fill our array 'data' which csv data split at ','
  // If you are using a different seperator, such as a TAB
  // you will need to modify the next item
  // Some examples
  // data = line.split( "\t" ); // for tab
  // data = line.split( ":" );  // for colon
  // data = line.split( " " );  // for space
  data = line.split( "," );
  
  // Start our table ROW
  fout.WriteLine( "<tr>" );
 
  // Loop through data elements found on current line
  for( i = 0; i < data.length; i++ )
  { 
   // write TD tags to wrap data
   fout.WriteLine( "<td>" + data[i] + "</td>" );
  }
  
  // Close the ROW
  fout.WriteLine( "</tr>\r\n" );
 }
 catch( e )
 {
  WScript.Echo( "Error: " + e.description );
 }
}
// Close TABLE
fout.WriteLine( "</table>" );
// Close HTML page
fout.WriteLine( htmlFooter() );
// Close input and output files
fin.Close();
fout.Close();
 
/*******************************
 HTML Header data
********************************/
function htmlHeader()
{
 var title = "CSV2HTML";
 var head = "<html>\r\n<head>\r\n";
 
// Title
 head += "<title>" + title + "</title>\r\n";
 
// Style Sheet
 head += "<style>\r\n";
 head += " TD { \r\n";
 head += "    font-family: verdana;\r\n";
 head += "    font-size: 10pt; \r\n";
 head += "    border-bottom: thin groove lightyellow;\r\n";
 head += "    border-top: thin groove lightyellow;\r\n";
 head += "    color: blue; background: lightgrey;\r\n";
 head += " }\r\n";
 head += "</style>\r\n";
 
 head += "</head>\r\n<body>\r\n";
 return( head );
}
/********************************
 HTML Footer data
*********************************/

function htmlFooter()
{
 var foot = "\r\n</body>\r\n</html>";
 return( foot );
}

Comments:

download this script
By phirak on Wednesday, November 28, 2007 (PST)
Hello, is it possible to downloas a sample that work ? i can't re make the structure. It would be great. best regards M. KOK BearingPoint Inc

Reply to this Comment

OK
By phirak on Wednesday, November 28, 2007 (PST)
i succeded to make it work thanks to you and your usefull site M. K

Reply to this Comment

Thanks for the great code
By karl3i on Thursday, March 06, 2008 (PST)

Was very helpfull.

 

For information, here is how I modified a little bit the code, so that empty fields in the CSV are translated into non breakable spaces in the HTML and the table to look prettier for these fields.

 

 for( i = 0; i < data.length; i++ )
  {
   // write TD tags to wrap data
   if(data[i]=="")
     data[i] = "&nbsp;";
   fout.WriteLine( "<td>" + data[i] + "</td>" );
  }

 

Keep up the good work!

 

Reply to this Comment

Sort data by date ?
By zeljko on Monday, March 10, 2008 (PST)

This script is super!

I need sort data by date. ( csv to html )

How can I do this ?

Best regards

Reply to this Comment

Add Your Comment



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon