| Convert CSV to XLS – Complete Guide for All Methods (2024) |
Introduction
CSV files are great for data portability, but when you need Excel features like formulas, multiple sheets, or formatting, you need XLS or XLSX format. In this guide, we cover every method to convert CSV to XLS — from Excel to Python to PHP.
Why Convert CSV to XLS?
There are several compelling reasons to convert your CSV data into Excel format:
- Add formulas and calculations to your data
- Apply professional formatting and color coding
- Use Excel pivot tables and charts
- Share with non-technical team members who prefer Excel
- Combine multiple CSV files into one multi-sheet workbook
Method 1: Convert CSV to XLS Using Microsoft Excel
- Open Microsoft Excel on your computer.
- Go to File > Open and browse to your CSV file.
- Excel will open the Text Import Wizard. Select Delimited and click Next.
- Check Comma as the delimiter and click Next, then Finish.
- Your CSV data is now in Excel. Go to File > Save As.
- In the ‘Save as type’ dropdown, select Excel Workbook (*.xlsx) or Excel 97-2003 Workbook (*.xls).
- Click Save. Your file is now converted.
Method 2: Convert CSV to XLS Using Python
Python is the most powerful and flexible method for converting CSV to XLS, especially for automation and large files.
Simple Conversion with pandas
import pandas as pd
# Read CSV file
df = pd.read_csv(‘input_file.csv’)
# Save as XLSX
df.to_excel(‘output_file.xlsx’, index=False, engine=’openpyxl’)
print(‘CSV to XLS conversion complete!’)
Convert CSV to XLS with Custom Formatting
import pandas as pd
from openpyxl.styles import Font, PatternFill
df = pd.read_csv(‘data.csv’)
with pd.ExcelWriter(‘output.xlsx’, engine=’openpyxl’) as writer:
df.to_excel(writer, sheet_name=’Sheet1′, index=False)
ws = writer.sheets[‘Sheet1’]
# Style header row
for cell in ws[1]:
cell.font = Font(bold=True, color=’FFFFFF’)
cell.fill = PatternFill(‘solid’, fgColor=’2E75B6′)
Method 3: Convert CSV to XLS Using PHP
For web developers building server-side conversion features, PHP with PhpSpreadsheet is the industry standard.
Install PhpSpreadsheet
composer require phpoffice/phpspreadsheet
PHP Conversion Script
<?php
require ‘vendor/autoload.php’;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
$reader = new Csv();
$spreadsheet = $reader->load(‘input.csv’);
$writer = new Xlsx($spreadsheet);
$writer->save(‘output.xlsx’);
echo ‘Conversion successful!’;
?>
Method 4: Convert CSV to XLS Online
Online tools offer the fastest solution with zero setup. Simply upload your CSV file to a converter like CloudConvert or Convertio, select XLS or XLSX as output format, and download.
For instant CSV to XLS conversion, try our dedicated online tool which handles files up to 50MB for free.
Common Issues When Converting CSV to XLS
Issue: Numbers Are Stored as Text
Solution: After importing in Excel, select the column, click the warning icon, and choose ‘Convert to Number’. In Python, use: df[‘column’] = pd.to_numeric(df[‘column’], errors=’coerce’)
Issue: Date Format Is Wrong
Solution: In Excel, select the date column and format as Date. In Python: df[‘date’] = pd.to_datetime(df[‘date’]).dt.strftime(‘%d/%m/%Y’)
Frequently Asked Questions (FAQs)
Is XLS or XLSX better for my converted file?
XLSX is the modern format and recommended in almost all cases. XLS is the legacy Excel 97-2003 format. Use XLSX unless you specifically need compatibility with very old Excel versions.
Can I convert multiple CSV files to one XLS with multiple sheets?
Yes! In Python, use pandas ExcelWriter and loop through your CSV files, writing each to a different sheet_name. This creates a single multi-sheet workbook from multiple CSVs.
How do I convert CSV to XLS without losing the leading zeros in numbers?
Format those columns as Text before or during import. In Python, use: df = pd.read_csv(‘file.csv’, dtype={‘zip_code’: str}) to preserve leading zeros.
Is the CSV to XLS conversion reversible?
Yes. You can always go back to CSV from Excel by using File > Save As and selecting CSV format. However, any Excel-specific formatting or formulas will be lost in that reverse conversion.