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

Discount Stainless Steel Watch, Cheap Costume Timepiece, Discount
By VenUnogeZep on Saturday, March 13, 2010 (PST)
Hello Costume Watches Manufacturers & Suppliers, Global Sources <a href="http://aerowatches.my3gb.com ">chronoswiss watches</a> <a href="http://dieselwatch.freehostking.com ">citizen watch collection</a> <a href="http://jojowatches.hostaim.com ">cole watches</a> <a href="http://watches.awardspace.biz ">concord diamond watch</a> <a href="http://watchestop.0catch.com ">costume watches</a>

Reply to this Comment

Perfect gifts for your friends, only 1 day left for SPRING SALE
By frulamruflame on Monday, March 29, 2010 (PST)
Incredible prices on designer watches including Citizen, Rotary, Seksy, Tissot, DKNY, Seiko... <a href="http://aewatches.emenace.com ">fossil watch brands</a> <a href="http://ogwatches.100webspace.net ">frogman watches</a> <a href="http://watches.1shop.de ">game time watches</a> <a href="http://watchesol.totalh.com ">geneve watches</a> <a href="http://watchesom.freehostia.com ">gents diesel watches</a>

Reply to this Comment

There was in no way a epoch when you could acquisition IWC facsimile watches cheaper than at the mom
By frovedfew on Tuesday, March 30, 2010 (PST)
UNTENANTED worldwide shipping with every order. We excite all facsimile watches brands <a href="http://avawatches.avafreehost.com ">gevril watch</a> <a href="http://rawatches.byethost9.com ">gold bulova watch</a> <a href="http://watches.net23.net ">gold tissot watches</a> <a href="http://watchesdi.100webspace.net ">hamilton aviation watches</a> <a href="http://watchesdis.freehostia.com ">howard pocket watches</a>

Reply to this Comment

Perfect gifts for your friends, only 1 day left for SPRING SALE
By frulamruflame on Saturday, April 03, 2010 (PST)
Watches by Seiko, Omega, Movado, and more brand names. <a href=" http://aewatches.emenace.com ">fossil watch brands</a> <a href=" http://gowatches.justfree.com ">frank muller watches</a> <a href=" http://watches.1shop.de ">game time watches</a> <a href=" http://watchesol.totalh.com ">geneve watches</a> <a href=" http://watchess.zxq.net ">gerald genta watches</a>

Reply to this Comment

Fake Swiss Made Rolex Replica Knockoff Watch, Fake Cartier Watches ...
By Mivedeera on Sunday, April 04, 2010 (PST)
A store selling replica Chanel, Gucci, Louis Vuitton and Tiffany once appeared on this domain <a href=" http://mwatches.hostse.com ">essential watches</a> <a href=" http://rewatches.awardspace.us ">fake cartier watch</a> <a href=" http://watchesed.10fast.net ">fake swiss watches</a> <a href=" http://watcheser.5webs.net ">faux watches</a> <a href=" http://watchesoo.0catch.com ">fireman watches</a>

Reply to this Comment

Luxury Watches at The Watchery: Tag Heuer, Cartier, Movado ...
By claicyboalo on Monday, April 05, 2010 (PST)
TAG Heuer Watches; Swiss Luxury Watches - Men and Ladies Luxury ... <a href=" http://mowatches.avafreehost.com ">designer watches</a> <a href=" http://prowatches.100webspace.net ">diesel watch strap</a> <a href=" http://toowatches.freehostia.com ">ebel watch discount</a> <a href=" http://watches.clanteam.com ">eliro movado watches</a> <a href=" http://watches.talk4fun.net ">esq quartz watch</a>

Reply to this Comment

Shop for watches such as Omega, omega watch, Gucci, Tissot, Hamilton and other
By roromaymn on Saturday, April 17, 2010 (PST)
Everything you ever wanted to know about Tissot Watches. Find watches offered by merchants. <a href=" http://kiwatches.kilu.de ">ladies luxury watches</a> <a href=" http://luwatches.kilu.de ">ladies swiss watch</a> <a href=" http://watches.66ghz.com ">long island watches</a> <a href=" http://watchesole.totalh.com ">luminox field watch</a> <a href=" http://watcheszo.zxq.net ">mechanical wrist watches</a>

Reply to this Comment

Harry Potter and the Deathly Hallows (2010) HDTV
By aquaxyvut on Monday, July 26, 2010 (PST)
Download Free Best Films HD in the World. Harry Potter and the Deathly Hallows (2010) HDTV <img>http://kino-manija.ru/images/stories/films_1/harry-potter-and-the-deathly-hallows%201.jpg</img> eva henger free divx film x adult video award free negima hentai doujinshi cooper hong inc review car sex video supra elfen lied dvd series band of brothers film soundtrack elk hunting video d2 lod singlplayer downloads http://beyonce-knowles-free-mp3-mp3.zanovo.pp.ua/ food handler permit online http://download-free-alc100p-win98-driver.zanovo.pp.ua/ engineering mathematics stroud download free http://free-myspace-outdoors-layout.zanovo.pp.ua/ anti aging seen on tv rege http://feet-licking-movies.zanovo.pp.ua/ free nudist pix http://clip-art-of-faces.zanovo.pp.ua/ copy dvd hardware

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