How to convert xls to CSV with php

Written by roy huggins
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to convert xls to CSV with php
PHP scripts can make good use of Excel data. (number background image by kuhar from

PHP and databases go together. As such, PHP and Excel files have historically encountered compatibility issues. People often keep manually edited databases in Excel spreadsheets, and then at some want to create a PHP script or a web page to interact with the data in that spreadsheet. Unfortunately, Excel is a proprietary format owned by Microsoft. So the oft-recommended method of matching open source systems to Excel files is to convert the Excel's spreadsheet data to a comma-separated value, or CSV, file. Historically, PHP scripts had to run on Windows and use COM to do this. But now there is a convenient cross-platform solution.

Skill level:

Other People Are Reading


  1. 1

    Go to the PHP Excel Reader Download and Documentation website (see Resources) and download the PHP Excel Reader package.

  2. 2

    Unzip the package and discard the example files. You don't need to use them.

  3. 3

    Install the file called "excel_reader2.php" where your PHP server can find it. For example, if you are using PHP on a remotely hosted webserver, upload excel_reader2.php to a folder in the include path on your server using FTP or your favourite file transfer program.

  1. 1

    Make your Excel file accessible to your PHP script. Make a copy of the Excel file with the name "xls_test.xls" for this tutorial. Then move "xls_test.xls" into the folder where your PHP script is located. If you are programming on a remote server, use FTP or your favourite file transfer program to transfer the Excel file to the server. If you are programming on your own computer, drag "xls_test.xls" into the folder where your PHP script is.

  2. 2

    Instantiate PHP Excel Reader in your PHP script using your Excel file. Create a variable that contains your Excel file as an object that can be manipulated, searched, etc. Use "$xls = new Spreadsheet_Excel_Reader('xls_test.xls', false);" as your code. Using "false" as our second parameter will save on memory usage since CSV files are simple and don't need such information as cell border colours.

  3. 3

    Create a variable to store the CSV data. Use "$csv = '';" as your code.

  4. 4

    Find out the number of columns and rows in the Excel file. For columns, use "$cols = $xsl->colcount();" as your code. For rows, use "$rows = $xls->rowcount();" as your code.

  5. 5

    Create your CSV data. Use a nested "for loop" to do it:

    for ($r = 1; $r <= $rows; $r++) { //go through each row of the spreadsheet

    for ($c = 1; $c <= $cols; $c++) { //go through each column

    $csv .= $xls-&gt;raw($r, $c);          //get the raw data in the current cell.
    if ($c = $cols) {
      $csv .= "\n";                     //create a new line after we've finished this row
    } else {
      $csv .= ",";                      //Put a comma between each item



  6. 6

    Save the contents of $csv to a CSV file using PHP's file-writing functions. Make sure the file ends in ".csv" for clarity.

Tips and warnings

  • CSV is a vastly more simple data format than an Excel file. This tutorial will work for XLS files with zero or one header row followed by data rows. Any other format will require more tricky programming.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.