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.
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.
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.
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:
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.
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).