CSV Export/Import with PHPExcel

CSV Export/Import with PHPExcel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

CSV Export and Import with PHPExcel

While PHPExcel is widely recognized for its robust handling of complex Excel files (.xlsx), developers often need to handle comma-separated values (CSV) formats due to their simplicity in data exchange or legacy system requirements. The core question is whether the library can natively support this format without writing custom logic. The answer is yes, but the implementation differs significantly from standard Excel export because PHPExcel treats CSV as a specific file type that requires explicit configuration regarding encoding and quoting rules.

To export data to a CSV file using PHPExcel, you must explicitly set the writer to output CSV rather than the default XML-based Excel format. This is achieved by instantiating the PHPExcel_Writer_CSV class instead of PHPExcel_Writer_Xlsx. The library automatically handles the conversion of cells into comma-separated strings, but it does not guess your desired delimiter or encoding strategy. By default, it uses commas for delimiters and standard ASCII encoding, which can lead to issues if your source data contains non-ASCII characters like accented letters (e.g., "é" or "ñ") or special symbols.

Here is a basic pattern for exporting an active worksheet to a CSV file:

use PHPExcel;
use PHPExcel_IOFactory;
use PHPExcel_Writer_CSV;

// Assuming $objPHPExcel contains your populated data
$writer = new PHPExcel_Writer_CSV($objPHPExcel);

// Configure the writer before saving
$writer->setDelimiter(','); // Explicitly set comma as delimiter
$writer->setEnclosure('"'); // Use double quotes for quoting fields
$writer->setSeparator("\n"); // Set line ending (platform dependent)
$writer->setSheetsInSeparateFiles(false); // Keep all sheets in one file

// Save the file
$filename = 'export.csv';
$writer->save($filename);

A critical decision point in CSV export is managing character encoding. PHPExcel defaults to UTF-8, but if your environment expects ISO-8859-1 (Latin-1) or Windows-1252, you must adjust the settings accordingly before saving. Failing to match the expected encoding often results in garbled text when opening the file in different applications like Excel on Windows versus macOS. Additionally, setting $writer->setUseBOM(false) is generally recommended unless you are targeting legacy Excel versions that struggle with UTF-8 BOM markers.

When importing CSV files into PHPExcel, the process involves creating a new PHPExcel object and loading the file using IOFactory. However, raw CSV loading can be fragile because the library attempts to infer column widths and row heights based on simple parsing logic. If your CSV contains complex formatting, merged cells, or inconsistent quoting, the imported object model might not reflect the visual layout of the original file accurately.

use PHPExcel;
use PHPExcel_IOFactory;

$objPHPExcel = new PHPExcel();
$loadedFile = 'import.csv';

// Load the file automatically detecting format if needed, 
// or explicitly set CSV if using older library versions
$objPHPExcel->load($loadedFile); 

// Access data as a sheet collection
$sheet = $objPHPExcel->getActiveSheet();
// Iterate through rows to process the imported data
foreach ($sheet->getRowIterator(1) as $row) {
    // Logic to handle imported cells
}

One significant trade-off with PHPExcel is memory consumption. The library loads the entire workbook into memory before you can manipulate it. For large CSV files, this approach becomes inefficient compared to stream-based parsers like fgetcsv(). If you are processing massive datasets, consider using a streaming approach for reading and writing instead of relying on the full workbook object model. Furthermore, always verify that your server's upload_max_filesize and memory_limit settings accommodate the file size you intend to process.

While PHPExcel provides a convenient unified API for both formats, it is essential to remember that CSV import/export is less feature-rich within this context than native Excel operations. Features like conditional formatting or complex formulas are stripped out during CSV conversion. For production systems requiring high performance on large datasets, evaluating modern alternatives that separate parsing logic from DOM manipulation might yield better results.

Our network