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

html to csv?
By watsthen on Friday, April 24, 2009 (PST)
Hi, the script is gr8 but i need a script to do the exact reverse.I have a main page with an html table which has links to subpages.I need to crate a csv file that has all th data of the table plus for each row of the table,it has the links also of the images on the subpages.so ultimately not only do i hav to convert html table to csv but also read the links from the subpages and add them to the csv for each row..plz help!!

Reply to this Comment

Multiple Convert CSV Files into multiple HTML
By ramps7 on Tuesday, November 03, 2009 (PST)
Hi Daren, I have a directory full CSV files which record the time each person logs in and out of our domain. Each file is %loginname%.csv I.E. the login JBloggs creates a file called JBloggs.csv. There are over 200 different individually named files all stored in folder called 'Loggs' on my server and I would like to convert all of these to HTML so managers can quickly access them through a web browser. Is there a way with your script the var src and var destination fields I can point to the directory and look at every file in there that is a csv. Like wild card the name of the source and destination... so *.csv outputs to the same name *.html :- // The names of our input and output files var src = "d:\loggs\*.csv"; var dest = "*d:\loggs\*.html"; So if where I have JBloggs.csv I get JBloggs.html and ASmith.csv goes to ASmith.html... and so fourth. If you could help I would be most grateful. Regards Danny

Reply to this Comment

weetw
By CagentencyTen on Monday, December 28, 2009 (PST)
miley cyrus nude miley cyrus nude miley cyrus nude

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