Home arrow PHP arrow Page 4 - PHP Excel Implementation in XAMPP Localhost

Testing Scripts to Create MS Excel Files - PHP

If you are planning to use PHP to create and interact with a Microsoft Excel spreadsheet application, then you can use the PHP Excel class. It can be a little tricky to use, however, especially for beginners. This tutorial will walk you through the basics and show you how to customize it for your own Excel implementation.

TABLE OF CONTENTS:
  1. PHP Excel Implementation in XAMPP Localhost
  2. Downloading and Installing PHP Excel Class in Windows XAMPP
  3. Configuration of PHP Excel Class
  4. Testing Scripts to Create MS Excel Files
By: Codex-M
Rating: starstarstarstarstar / 6
July 27, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If your target application is MS Excel 2007, phpexcel's default PHP scripts will create MS Excel 2007 documents by default. To do this:

Step 1. In the browser, execute this URL: http://localhost/phpexcel/Tests/01simple.php

Step 2. The created files are placed in the Tests folder /phpexcel/Tests  

It is named 01simple.xlsx 

Step 3. Open the files using MS Excel 2007.

However, problems will arise if you have MS Excel 2002 (XP)/2003 installed and not Excel 2007. You cannot view the created MS Excel 2007 files with an MS Excel 2002/XP installation, even if you have compatibility programs and patches installed.

The error generated is "The Converter Failed to Open the File."

Therefore, this phpexcel class will work best if you have an MS Excel installed that matches the version of your phpexcel-created files.

If your target application is MS Excel 2002(XP) and the later version, like MS Excel 2003 (but not MS Excel 2007), then you need to tweak the script as follows:

Step 1. Go to C:xampphtdocsphpexcelTests1simple.php and open the PHP script with your favorite editor.

Step 2. You need to edit the following lines so that it will output for MS Excel 2002/2003 and not Excel 2007.

Look for these lines:

// Save Excel 2007 file

echo date('H:i:s') . " Write to Excel2007 formatn";

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

 

And change them to:

 

// Save Excel 2002 file

echo date('H:i:s') . " Write to Excel2002 formatn";

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save(str_replace('.php', '.xls', __FILE__));

You can also change the document properties defined by $objPHPExcel->getProperties() to relevant information (creator, subject, etc.).

The options to make changes so that the created spreadsheet will be compatible with older versions of Excel are the createwriter($objPHPExcel)...

Other supported file formats of the writer can be found here: C:xampphtdocsphpexcelClassesPHPExcelWriter

Based on that information, it is possible for you to create HTML, CSV, Serialized and PDF output, etc. using PHPExcel.  

Step 3. Save the test script 01simple.php

Step 4. Execute it in the browser:  http://localhost/phpexcel/Tests/01simple.php 

Step 5. Open the newly created spreadsheet file, named 01simple.xls.

And then you should see output similar to what is shown below (using the default contents):

To add data to the spreadsheet, you can modify the setCellValue parameter such as what is shown below:

$objPHPExcel->setActiveSheetIndex(0)

            ->setCellValue('A1', 'Hello')

            ->setCellValue('B2', 'world!')

            ->setCellValue('C1', 'Hello')

            ->setCellValue('D2', 'world!');

 

For example, if you would like to write the text (coming from a web form or another application, for example) "The quick brown fox jumps over the lazy dog" in cell D5, then the syntax would be:

// Add some data

echo date('H:i:s') . " Add some datan";

$objPHPExcel->setActiveSheetIndex(0)

            ->setCellValue('D5', 'The quick brown fox jumps over the lazy dog.');

 

You can comment "Miscellaneous glyphs" as shown below, for example, so that the output text contains only the intended data:

 

// Miscellaneous glyphs, UTF-8

//$objPHPExcel->setActiveSheetIndex(0)

//->setCellValue('A4', 'Miscellaneous glyphs')

//->setCellValue('A5', 'é èùâê®ôûëïüÿäöüç');

 

In the next tutorials on PHP Excel, you will learn details of how to work with Excel formulas in PHP. Formulas are fairly common applications in Excel.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: