How to Convert Excel to CSV Format

CSV file format comes with many immeasurable benefits. For this reason, you might need to convert your excel files to CSV format. You can use either an Excel application, an online converter, or Google sheets. You can convert Excel to CSV using PHP if you are a developer. This article will cover various easy ways to convert Excel Files to CSV.

What is a CSV file?

CSV files simply text files with one column per line. Each column is separated by a comma. The first column is always the file's name, followed by the list of values. For example, a CSV file would contain the names of the colors in a rainbow: red, orange, yellow, green, blue, indigo, and violet. Each row in a CSV file is separated by a newline which shows why it's referred to as Comma Separated Values.

Reasons to Use CSV File Format

  • CSV is a text format that is easy to read and write

You can open a CSV file in any text editor, such as Microsoft Word, and start editing the data.

  • You can use a CSV file to store data in a tabular format

Each row in the table corresponds to a line in the CSV file. Each column in the table corresponds to a column in the CSV file. This format is easy to read and use, making it a popular data storage choice.

  • CSV files are easy to use in data analysis and reporting.

CSV files are versatile and a great choice for data files you need to use frequently. This makes it easy to sort, search, and compare data.

  • CSV files are easy to export and import.

CSV files are commonly used in spreadsheet applications, and exporting a CSV file is easy: just select the file and choose File > Export. You can also import a CSV file by selecting it and choosing File > Import.

  • CSV files are easy to share.

Although CSV is not a memory-saving format, it's much smaller than an excel spreadsheet.

  • CSV file format is easy to update

Knowing that each column is separated by a comma makes it easy to update the CSV file.

How to convert Excel to CSV file Using Excel Software

1. Click on the file

2. Click on Save as and choose your desired folder

3. Click on the dropdown arrow and Select *CSV (Comma Delimited)

4. Click save

How to Convert Excel to CSV Online

1. Open Knightsheets.com Excel to CSV converter

2. Select or drag and drop your File

3. Click Convert

4. Wait a few seconds for your file to be processed, and the download will start automatically.

How to convert Excel to CSV using Google Sheets

1. Open Google sheets

2. Click the file picker

3. Navigate to the upload tab, then Drag and drop or browse the excel file on your device.

4. Click on the file

5. Click Download and select Comma Separated Values (.CSV)

How to Convert Excel to CSV Using PHP

  • PHPspreadsheet Library
  • Server (Xamp, Wamp, Lamp, Mamp, etc)
  • Composer (You will use it to install PHPspreadsheet)- Install composer before starting the process

1. Create a folder inside htdocs and label it exceltocsv


2. Launch CMD and change the directory to the folder you have just created

3. Run the command to Install PHPspreadheet using Composer (composer require phpoffice/phpspreadsheet)

4. Create a folder and name it files inside the exceltocv folder (This is where the file will be stored temporarily before being converted)

5. Create an Index.php file. Paste the following code to create an upload form.


<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>
?>
</body>
</html>

6. Create an upload.php file and paste the following code


[php]
<?php
require 'vendor/autoload.php';
/* code by https://knightsheets.com/ */
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 "Sorry, file already exists.";
} else {
move_uploaded_file($temp_name, $path_filename_ext);
// echo "Congratulations! File Uploaded Successfully.";
}
}
use \PhpOffice\PhpSpreadsheet\IOFactory;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
//load the uploaded spreadsheet
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("$path_filename_ext");
//create a new csv file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$date = date('d-m-y-' . substr((string)microtime(), 1, 8));
$date = str_replace(".", "", $date);
$newfilename = "export_" . $date . ".csv";
try {
$writer-&gt;save($newfilename);
$content = file_get_contents($newfilename);
} catch (Exception $e) {
exit($e-&gt;getMessage());
}
//make the file downloadable
header("Content-Disposition: attachment; filename=" . $newfilename);
unlink($newfilename);
exit($content);
[/php]

Final Thoughts

Converting Excel to CSV is easy with the above methods. Using Excel and Google sheets involves several steps, while online tools provide one-click solutions to save your time. Alternatively, if you don't have excel software or access to Google sheets, you should definitely try the free online tools.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.