How to Convert CSV to HTML

HTML is the most popular content format in the world. Most websites, if not all, feature HTML. Converting Your CSV file to HTML opens the doors to endless opportunities. There are three ways to convert CSV to HTML. You can either use Excel software, an online CSV to HTML converter, or PHP if you are a developer. In this article, I will guide you on how to convert your file using these three methods.

CSV Vs HTML

Definition: CSV stands for Comma-separated Values while HTML stands for Hypertext Markup language

Uses: HTML is used to Format content displayed on a browser, while CSV is used to store data separated by a comma

Reading and Writing: CSV requires a text editor to view, while an HTML document can be viewed using a browser and edited using a text Editor

Popularity: HTML is more popular than CSV because it's supported by all browsers

How to convert CSV TO HTML using Excel Software

1. Click on File, which is at the top left corner

2. Click Save As

3. Click on the drop-down menu, Select web-page .html or .htm, then click Save

4. Choose the whole workbook or the worksheets you wish to convert, then click Publish


5. Open the file explorer and open the file using a browser

How to convert CSV to HTML using Knight Sheets

1. Open Knightsheets.com CSV to HTML converter

2. Upload your CSV file

3. Click convert and wait a few seconds for your file to be processed and downloaded automatically.

How to convert CSV to HTML using PHP

Prerequisites

  • Composer (Install it first before moving to the first step)
  • PHPspreadsheet
  • Webserver (Lamp, Wamp, Xampp, Etc)

1. Create a Folder Inside htdocs and name it csvtohtml


2. Open CMD and navigate to the folder you have just created

3. Create a folder named files by running the command mkdir files

4. Run the composer command to install PHPSpreadsheet (composer require phpoffice/phpspreadsheet)

5. Create an Index.php file and copy-paste the following code


<html>
<body>
<?php
<form action="upload.php" enctype="multipart/form-data" method="POST">
<input id="fileToUpload" name="fileToUpload" required="" type="file" value="Select File" />
<button class="btn" name="upload">Upload</button>
</form>
<?PHP
</body>
</html>

6. Create an upload.php File and Copy paste the following code


<?php
require 'vendor/autoload.php';
if (($_FILES['fileToUpload']['name'] != "")) {
// Where the file is going to be stored
$target_dir = 'files/';
$file = $_FILES['fileToUpload']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['fileToUpload']['tmp_name'];
$path_filename_ext = $target_dir . $filename . "." . $ext;
// Check if file already exists
if (file_exists($path_filename_ext)) {
//echo "";
} else {
move_uploaded_file($temp_name, $path_filename_ext);
// echo "Congratulations! File Uploaded Successfully.";
}
}
use \PhpOffice\PhpSpreadsheet\IOFactory;
use \PhpOffice\PhpSpreadsheet\Reader\Csv;
use \PhpOffice\PhpSpreadsheet\Writer\Html;
//load the uploaded spreadsheet
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
$spreadsheet = $reader->load("$path_filename_ext");
//create a new pdf using mdf library
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
$date = date('d-m-y-' . substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$newfilename = "export_" . $date . ".html";
try {
$writer->save($newfilename);
$content = file_get_contents($newfilename);
} catch (Exception $e) {
exit($e->getMessage());
}
//make the file downloadable
header("Content-Disposition: attachment; filename=" . $newfilename);
unlink($newfilename);
exit($content);

7. Start your web server and open the folder using your browser

8. Upload the file and test whether it's working

9. Viola, You have just created a CSV to HTML converter using PHP

Final Thoughts

You can convert CSV to HTML using Excel Software or online tools. If you are developing, you can use PHP to convert CSV files to HTML and vice versa. HTML is the best format to display content on browsers.