Question

guys. Do you happen to know any good converter of html to excel file through php? I have seen lots (PHPExcel and SimpleExcel) but nothing can function the way dompdf does. (dompdf is a html to pdf converter through php)

Is there anything like dompdf in converting html (including its attributes e.g. strong, i, u) to excel? Please post.

Was it helpful?

Solution

In the past I've just served the HTML document as-is. Excel is perfectly capable of loading an HTML document and parsing it as a spreadsheet. Excel even understands many CSS attributes. All you have to do is add an Excel mime type to the HTTP headers, for example:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:Attachment;Filename=htmltable.htm');
readfile('htmltable.html');

Unfortunately this isn't as appealing an option as it used to be. Starting with Excel 2007 the program performs some basic document type confirmation. Because HTML is not XLS (obviously) you get a warning. Excel can still open up the document and display it correctly, but there aren't really any options for disabling the warning.

You might try PHPExcel. I've never used it, but apparently you can use it to convert a document from HTML to XLS:

By combining the reader with the writer it is possible to convert files from one format to another with just 3 lines of code:

$objPHPExcel = PHPExcel_IOFactory::load("XMLTest.xml");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('covertedXml2Xlsx.xlsx');

OTHER TIPS

I spent hours setting up an automated report using PHPExcel and found it incredibly unwieldy. I found it much more user-friendly and effective to write the data to .csv (fputcsv), then use COM to run an Excel macro, which opens the .csv and implements all the formatting, etc.

You can also execute Excel macros from VBScript and call PHP's exec function to run the VBScript.

fputcsv Is what you after. Easy if you have info before it gets to html. Can also add fancy stuff like skip columns/rows and make headings, make pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top