program csv2html; {$APPTYPE CONSOLE} uses SysUtils, Classes; const // Carriage Return/Line Feed CRLF = #13#10; // The CSV data csvData = 'Character,Speech'+CRLF+ 'The multitude,The messiah! Show us the messiah!'+CRLF+ 'Brians mother,Now you listen here! He''s not the messiah; he''s a very naughty boy! Now go away!'+CRLF+ 'The multitude,Who are you?'+CRLF+ 'Brians mother,I''m his mother; that''s who!'+CRLF+ 'The multitude,Behold his mother! Behold his mother!'; // HTML header htmlHead = ''+CRLF+ ''+CRLF+ ''+CRLF+ ''+CRLF+ 'CSV-to-HTML Conversion'+CRLF+ ''+CRLF+ ''+CRLF+ ''+CRLF; // HTML footer htmlFoot = ''+CRLF+ ''; { Function to split a string into a list using a given delimiter } procedure SplitString(S, Delim: string; Rslt: TStrings); var i: integer; fld: string; begin fld := ''; for i := Length(S) downto 1 do begin if S[i] = Delim then begin Rslt.Insert(0,fld); fld := ''; end else fld := S[i]+fld; end; if (fld <> '') then Rslt.Insert(0,fld); end; { Simple CSV parser with option to specify that the first row is a header row } procedure ParseCSV(const csvIn: string; htmlOut: TStrings; FirstRowIsHeader: Boolean = True); const rowstart = ''; rowend = ''; cellendstart = ''; hcellendstart = ''; hrowstart = ''; hrowend = ''; var tmp,pieces: TStrings; i: Integer; begin // HTML header htmlOut.Text := htmlHead + CRLF + CRLF; // Start the HTML table htmlOut.Text := htmlOut.Text + '' + CRLF; // Create stringlist tmp := TStringList.Create; try // Assign CSV data to stringlist and fix occurences of '<' and '>' tmp.Text := StringReplace(csvIn,'<','<',[rfReplaceAll]); tmp.Text := StringReplace(tmp.Text,'>','>',[rfReplaceAll]); // Create stringlist to hold the parts of the split data pieces := TStringList.Create; try // Loop through the CSV rows for i := 0 to Pred(tmp.Count) do begin // Split the current row SplitString(tmp[i],',',pieces); // Check if first row and FirstRowIsHeader flag set if (i = 0) and FirstRowIsHeader then // Render HTML htmlOut.Text := htmlOut.Text + hrowstart + pieces[0] + hcellendstart + pieces[1] + hrowend + CRLF else htmlOut.Text := htmlOut.Text + rowstart + pieces[0] + cellendstart + pieces[1] + rowend + CRLF; end; // Finish the HTML table and end the HTML page htmlOut.Text := htmlOut.Text + '
' + CRLF + htmlFoot; finally pieces.Free; end; finally tmp.Free; end; end; var HTML: TStrings; begin // Create stringlist to hold HTML output HTML := TStringList.Create; try Writeln('Basic:'); Writeln(''); // Load and parse the CSV data ParseCSV(csvData,HTML,False); // Output the HTML to the console Writeln(HTML.Text); // Save the HTML to a file (in application's folder) HTML.SaveToFile('csv2html_basic.html'); Writeln(''); Writeln('====================================='); Writeln(''); HTML.Clear; Writeln('Extra Credit:'); Writeln(''); // Load and parse the CSV data ParseCSV(csvData,HTML,True); // Output the HTML to the console Writeln(HTML.Text); // Save the HTML to a file (in application's folder) HTML.SaveToFile('csv2html_extra.html'); Writeln(''); Writeln('====================================='); finally HTML.Free; end; // Keep console window open Readln; end.