PHP – Convert HTML to Xlsx

July 14th, 2021 / by api2pdf /

Intro

If you are writing php code, you might be tasked with the strange requirement of converting HTML to a Microsoft Excel (xlsx) file. After some research, you discover that this is not as trivial as you hoped it would be. You might have ended up on stackoverflow seeing old, outdated libraries or confusing answers that ask you to use interop or LibreOffice. Let’s make your pain go away right now.

Our API will take your .html file and convert it to xlsx. Just make sure your HTML is saved as a .html file and accessible at a URL that our service can ingest. For example, see this: https://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html — Ideally a file storage provider like S3 or Azure Blob Storage. See the code sample below.

Convert HTML to Xlsx with PHP

Step 1) Install the PHP client library from github: https://github.com/Api2Pdf/api2pdf.php

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

require_once 'your-own-directory/Api2Pdf.php';
$apiClient = new Api2Pdf('YOUR-API-KEY'); 

$result = $apiClient->libreOfficeHtmlToXlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html');
echo $result->getFile();

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes converting HTML to Xlsx files easy and painless for those writing php code.

See full github library

We have a whole php based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.php

Tags: , , , , ,

Comments are closed.