vijayjoshi.org - php | javascript | ajax | and all things web

How to create excel files in PHP?

Some time ago I found a class on phpclasses.org by Harish Chauhan which makes it incredibly easy to generate simple excel spreadsheets from PHP. I found it useful so I want to share its usgae with readers too. I also made some changes to the class for styling the rows.


Download Source

Here is the process:

1- Include the ExcelWriter class file in your code

include("excelwriter.inc.php");

2- Initialize the ExcelWriter class

$fileName = "myNewExcel.xls";
	$excel = new ExcelWriter($fileName);

Constructor of ExcelWriter class accepts a file name as parameter. File will be saved by this name.

3- Write data to the file

There are 2 ways to write data to the file. One row at a time or column by column.


-Writing one complete row:

$myArr=array("First Name",
				"Last Name",
				"Address",
				"Age"
				);

	$excel->writeLine($myArr, array('text-align'=>'center', 'color'=> 'red'));

First create an array of all values and use the writeLine method to write it to file. WriteLine accepts 2 parameters. First is an array of values that you wish to write in a row. Second parameter, which is optional, is the array of style properties that you may wish to apply to whole row. All css properties will work as long as you pass them in array format.

-Writing column by column data:
You can write data on column basis too. To do this create a row first and then write columns as required.

// this will create an empty row
	$excel->writeRow();
	// Now write columns
	$excel->writeCol("Alok");
	// inline styling can be done too
	$excel->writeCol("Sah");
	// or you can pass array of css properties
	$excel->writeCol("Pandav Nagar", array('text-align'=>'center', 'color'=> 'green','font-size'=> '22px'));
	$excel->writeCol(30,array('text-align'=>'right', 'color'=> 'brown'));

WriteCol writes data to a single cell. Just like WriteLine, WriteCol also accepts 2 parameters. First is column value and second(optional) is css properties.

4- Close the file and you are done

$excel->close();

After you are done with writing all data, don’t forget to close the file. Your excel file is ready now.

One more thing. You can style the column data in 2 ways. Either pass an array of css properties to WriteLine() method or wrap the column data in a html element.

Also make sure that you have write permission set for your excel file.
Complete source code for example:

include("excelwriter.inc.php");
	$fileName = "mynewxls.xls";
	$excel = new ExcelWriter($fileName);

	if($excel==false)
	{
		echo $excel->error;
		die;
	}
	$myArr=array(
				"First Name",
				"Last Name",
				"Address",
				"Age"
				);

	$excel->writeLine($myArr, array('text-align'=>'center', 'color'=> 'red'));

	$myArr=array("Vijay","Joshi","Vikaspuri, Delhi",27);
	$excel->writeLine($myArr);

	$excel->writeRow();
	$excel->writeCol("Alok");
	$excel->writeCol("Sah");
	$excel->writeCol("Pandav Nagar", array('text-align'=>'center', 'color'=> 'green','font-size'=> '22px'));
	$excel->writeCol(30,array('text-align'=>'right', 'color'=> 'brown'));

	$excel->close();
	echo "Data written to file $fileName Successfully.";


Download Source

Give this article a Thumbs-Up on StumbleUpon if you like this.

Related Posts

Posted in : FAQ, PHP
Tags:

12 Comments to “How to create excel files in PHP?”

Add Comments (+)

  1. Jean says:

    Hello Vijay!
    I’m trying to use your code but every time i’m trying to generate a xls file exactly your code i’m getting this text in the excel sheet as result: ‘Resource id #5′ i have a office 2003 .

  2. sree says:

    Hi,can u please tell how can we change the background color of a cell using this class.Thank You in advance

  3. divya says:

    this is not working in linux. No error message also. new xls file is not created.

  4. MSN says:

    Really nice.
    I was trying for so many time
    finally this code worked!!!!!!!!

  5. hernan says:

    I can’t set the background-color !!!

    $excel->writeCol(“value”); not works

    please help me

    • hernan says:

      I could set this property:

      $excel->writeCol(“value”, array(‘background-color’=> ‘#CCFF99′));

  6. hernan says:

    How can I create an ‘xlsx” file?
    thanks

  7. sandm says:

    This is really a important code for a website designer. I tried this code and after certain errors it worked. Thanks for the source code friend!

  8. saissy says:

    hi

    how about if i want to save the xls file in other location how can i do that?

  9. Banzay says:

    Hi, Thank you for this usefull code,

    Every thing is working fine but I am getting this error in the page:

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 239

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 239

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 239

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 239

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 289

    Notice: Undefined variable: cssStr in C:\Program Files\EasyPHP-5.3.8.0\www\Test\excelwriter.inc.php on line 289

    How can i sort this out?

    Banzay

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

55,788 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>