XLS Download from PHP Class!

January 21, 2009 | PHP, Time Savers | 5 comments

Following on from my last post on creating a proper XLS spreadsheet from PHP I have just written a small class which will do all the hard work for you if necessary.

It will handle rows and columns automatically aswell as being able to be passed an array and then generating a spreadsheet download for you.

Download: [download#7#size#nohits]

Sample usage is as follows:

	//Require the file
	require_once (SHARED_CLASSES_DIR . 'xls.class.php');

	//Instantiate the class.
	$xls = new xls(); 

	//Just build an example array out of test data
	$array = array(
		array(1,2,3,4,'five')
		, array('five','test' ,4,3, 0)
	);

	//Triggers the download using the passed array
	$xls->download_from_array($array);

Note that when instantiating the class you can pass a string as an argument and it will set the name of the XLS download. No need to add .xls to the end because it will do that for you.

Download: [download#7#size#nohits]

A Donate Button!

5 Comments

  1. jbogdani

    Hello,
    thank you very much for your class witch I found very useful and fast.
    But I have an encoding problem. I use your class to export data from a MySQL DB via PHP. Both MySQL and PHP work with UTF-8 char encoding, but when I open the xls file all accentuated characters looks strange: “ën” stands for “ë”, etc. Maybe I have to specify character encoding the xls header? Could you help me in any way?
    Thank you very much

    Reply
    • Sean

      Hi there. Glad you are finding it useful. I used to work a lot with UTF8. We used to have to put utf8_decode() on a load of the outputs for various download and output systems. Maybe that will work?

      thanks
      Sean

      Reply
  2. jbogdani

    Hi.
    Thank you for your help. I just changed the encoding of my data before packing them and now it works fine!
    Here it is the changed function (if anybody needs it):

    function cell($value) {
    $value=utf8_decode($value);
    if (is_numeric($value)) {
    … etc …
    }

    $this->xls_string .= $value;

    $this->col++;
    }

    Reply
  3. stefi

    Hi Sean,

    thank you very much you for your class!
    It is very useful and nice.

    But I have still problem with encoding in UTF8. String ěščřžýáíé looks ?????ýáíé

    Do you some idea please?

    Thank you stefi.

    Reply
    • Sean

      Hi Stefi, is the file stored itself in UTF8? Also have you run utf8_encode() over the data before it creates the cells?

      thanks
      Sean

      Reply

Submit a Comment

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

CommentLuv badge

Stay in touch!

Page Builder Cloud

Page Builder Cloud

A truly universal template library for WordPress Page Builders.

Page Builder Recommendation

Elementor Banner

We are BIG fans of the Elementor page builder. Give it a look!

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist and Co-Founder of Page Builder Cloud.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!