How to Use PHP to Make an Excel File

Written by steve mcdonnell Google
  • Share
  • Tweet
  • Share
  • Email

Although Microsoft Excel uses a proprietary format to store the data, formatting and properties of an Excel spreadsheet, you can create an Excel file with the PHP scripting language. If the server that runs PHP has Excel installed, you can create an Excel spreadsheet by creating an Excel COM object, setting the COM object properties and using the COM object methods to create and save an Excel spreadsheet. If the server does not have Excel, you can use the PHPExcel library from Microsoft's open-source community, CodePlex.

Skill level:


  1. 1

    Initialise a variable to be a new Excel application COM object. For example, type:


    $excel = new COM("excel.application");

  2. 2

    Hide the Excel window you just created until you have populated it with formatting and data. For example, type:

    $excel->Visible = 0;

  3. 3

    Create a new workbook inside the Excel object. For example, type:

    $w = $excel->Workbooks->Add();

  4. 4

    Create a new worksheet in the Excel object and make that worksheet active. For example, type:

    $w->Worksheets(1)->Name = "Main Worksheet";


  5. 5

    Add data and formatting to the worksheet. For example, type:

    $w->ActiveSheet->Cells(1,1)->Value = "Name";

    $w->ActiveSheet->Cells(1,2)->Value = "Address";

    $w->ActiveSheet->Cells(1,1)->Font->Bold = true;

    $w->ActiveSheet->Cells(1,2)->Font->Bold = true;

    $w->ActiveSheet->Range("A1:A1")->ColumnWidth = 10;

    $w->ActiveSheet->Range("A1:A1")->ColumnWidth = 20;

  6. 6

    Save the Excel document you created. For example, type:

    $file = tempnam(sys_get_temp_dir(), "excel");


  7. 7

    Close the file and quit the application. For example, type:






  1. 1

    Download the PHPExcel library and extract it to a directory on the webserver. Create a new PHP file in an editor and specify where the PHPExcel library can be found. For example, type:


    $library_location = "/usr/local/mylibraries/phpexcel/";

    include $library_location."PHPExcel.php";

    include $library_location."PHPExcel/Writer/Excel2007.php";

  2. 2

    Create a new PHPExcel object. For example, type:

    $excel = new PHPExcel();

  3. 3

    Set the properties of the spreadsheet by using the object methods in "getProperties". For example, type:

    $excel->get Properties()

    ->set Creator("John Doe")

    ->setLastModifiedBy("John Doe")

    ->set Title("Excel Spreadsheet created using PHP")

    ->set Subject("Excel Spreadsheet created using PHP");

        -&gt;set Description("This Excel spreadsheet was created using PHP and PHPExcel from Microsoft");
  4. 4

    Add data to the spreadsheet by using the object methods in "getActiveSheet." For example, type:


    $excel->getActiveSheet()->SetCellValue("A1", "Name");

    $excel->getActiveSheet()->SetCellValue("B2", "Address");

  5. 5

    Add formatting and cell properties to the spreadsheet using the object methods in "getActiveSheet." For example, type:


    $excel->getActiveSheet()->getColumnDimension("B")->set Width(20);

    $excel->getActiveSheet()->get Style("A1:B1")->get Font()->set Bold(true);

  6. 6

    Save the Excel file and exit the program. For example, type:

    $excel_writer = PHPExcel_IOFactory::create Writer($excel, "Excel2007");


    $excel->disconnect Worksheets();




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.