Search code examples
phpexport-to-excelphpexcelhtml-table

How to go through an HTML table using PHP and get the data in each td?


I am trying to use the PHPExcel class to export reports into excel file. What I really need to do is to go thru every row in the HTML table then place it into cells in an excel file.

I think PHPExcel has enough document to help me with created the excel file. But what I am not sure on how to do is to take an html table and break it so I can take every value and put it in an excel cell.

Thanks for your help


Solution

  • As simple as:

    $objReader = PHPExcel_IOFactory::createReader('HTML');
    $objPHPExcel = $objReader->load("myHtmlFile.html");
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save("myExcelFile.xlsx");
    

    but you could use your same basic logic for building the HTML from your report data, and just modify it to set cell values in a PHPExcel object. If you have an MVC, then its only the View that you really need to change.