HTML Table to CSV

Discussion in 'HTML & Website Design' started by Deepprogrammer, Nov 1, 2012.

  1. #1
    How to convert HTML table to CSV file?
     
    Deepprogrammer, Nov 1, 2012 IP
  2. rome2213

    rome2213 Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Impossible with HTML.
    Copy the contents of the table into an Excel spreadsheet and save as CVS file.
     
    rome2213, Nov 2, 2012 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    How are you creating the HTML? If it's in PHP, just open a text file and write the data out to the file in csv format. (A csv file is a text file.)

    If you're writing the HTML by hand, it's a one time thing, so just enter the data in an Excel worksheet and save it as csv.
     
    Rukbat, Nov 2, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    I'd see if PHP's domDocument can load the table -- the table structure would then be easy enough to parse into nested arrays, that could then simply be output using fputcsv.
     
    deathshadow, Nov 2, 2012 IP
  5. henrycarpenter03

    henrycarpenter03 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    just copy past all the data to microsoft excel and save as .csv.
     
    henrycarpenter03, Nov 5, 2012 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Which would NOT put the TD into separate cells -- which I assume is the question.
     
    deathshadow, Nov 5, 2012 IP
  7. nicksteve32

    nicksteve32 Peon

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi Deepprogrammer,

    Here are few steps to convert html to csv file please check and let us know.



    • Point your browser to the data page you want to read.
    • View the HTML SOURCE CODE of the page.
    • Highlight the source code of the table you want to copy.
      It will be between <table> and </table> tags
    • Copy the source code (control - insert in Windows)
      (You can select more than one table at a time, but you might get strange results. You can also select text outside a table, but the results will again be unpredictable.)
    • Paste the source code of the table into the left-hand window of the H2Text applet
      (Shift - insert in Windows)
    • Click on the translate button
    • The CSV (comma separated values) version of the table will show up in the right hand box.
    • Copy this data, and paste it into your spreadsheet.
    • This version should paste into your spreadsheet nicely.
     
    nicksteve32, Nov 5, 2012 IP
  8. yelbom

    yelbom Greenhorn

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    Heres a simple php script where you copy and past the table's html in text area and it converts to csv pipe delimited



    <?php
    	function make_content_file($filename,$content,$opentype="w"){
    		$fp_file = fopen($filename, $opentype);
    		fputs($fp_file, $content);
    		fclose($fp_file);
    	}
    
    	$a = trim($_POST['file']);
    	preg_match('/<table(>|[^>]*>)(.*?)<\/table(|>)/is',$a,$b);
    	$table = $b[2];
    	preg_match_all('/<tr(>|[^>]*>)(.*?)<\/tr(|>)/is',$table,$b);
    	$rows = $b[2];
    	foreach ($rows as $row){
    		preg_match_all('/<td(>|[^>]*>)(.*?)<\/td(|>)/is',$row,$b);
    		$out[] = strip_tags(implode('|',$b[2]));
    	}
    
    	
    	if($_POST){
    		$out = implode("\n", $out);
    		make_content_file('file.txt',$out);
    ?>
    
    <p><a href="file.txt">download pipe delimited file</a></p>
    
    <textarea name="file" cols="100" rows="25"><?=$out?></textarea>
    
    <?}?>
    <form method="post">
    <textarea name="file" cols="100" rows="25"><table>
    	<tr>
    		<td>corner</td>
    		<td>col 2</td>
    		<td>col 3</td>
    		<td>col 4</td>
    	</tr>
    	<tr>
    		<td>ex row 2</td>
    		<td>data 2 2</td>
    		<td>data 2 3</td>
    		<td>data 2 4</td>
    	</tr>
    	<tr>
    		<td>ex row 3</td>
    		<td>data 3 2</td>
    		<td>data 3 3</td>
    		<td>data 3 4</td>
    	</tr>
    	<tr>
    		<td>ex row 4</td>
    		<td>data 4 2</td>
    		<td>data 4 3</td>
    		<td>data 4 4</td>
    	</tr>
    </table></textarea>
    <br>
    <input type="submit" value="Save">  
    </form>
    PHP:
     
    yelbom, Dec 9, 2012 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    Assuming the only thing used in the table are TD... which done properly there probably should be more -- which is why I'd load it in using DOMDocument::loadHTML instead of regex.
     
    deathshadow, Dec 10, 2012 IP
  10. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #10
    A little trick I use. It works on some HTML tables, not all of them.

    Copy the table. Paste into a Word doc. If it doesn't appear as a table, convert text to table. You may have to do some little fixups.

    Copy the table. Paste Special into Excel. Try either HTML or text and use whichever one works (doesn't dump it all into one column).
     
    Rukbat, Dec 10, 2012 IP