2

I want to create a simple excel file with two rows. example:

order id | Name | Address | Quantity | Price | Total
    1    | XXXX | YYYYYYY |     10   |  700  |  7000

Is there any lightweight library or some code snippet so that I can easily achieve this.

4
  • xls and xlsx are both pretty involved formats, you will most likely need a heavy library
    – j_mcnally
    Feb 25, 2012 at 4:33
  • If you want an actual XLS or XLSX then phpexcel is your best bet regardless of the size of the library. If you can deal with just handing them a CSV then or a HTML table then do that and force a download or what have you. Feb 25, 2012 at 4:35
  • @prodigitalson: I want excel file with .xls extention. I just downloaded PHPExcel and I found that It contains 19.9 MB classes folder. I dont think so to generate such a small content it requires that much big library.
    – Avinash
    Feb 25, 2012 at 4:56
  • The most easy way is here stackoverflow.com/questions/11189021/reports-in-codeigniter Jun 25, 2012 at 13:37

3 Answers 3

5

See this tutorial:

Here's a small (2.7 KB zipped) library to export XLS:

Here's a similar one to export XLSX (4.4 KB zipped):

Both have small working examples.

8
2

This has been answered at the following link:

How to use the CSV MIME-type?

Although it outputs a CSV MIME type, Excel is usually the default application for CSV.

Oops. Forgot the CodeIgniter part :)

Based on the above link, you can create a controller similar to the following, assuming CI 2.x

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Csv extends CI_Controller {

    public function index() {
        header('Content-type: text/csv');
        header('Content-disposition: attachment;filename=fromci.csv');
        echo "order,id,Name,Address,Quantity,Price,Total".PHP_EOL;
        echo "1,1,XXXX,YYYYYYY,10,700,7000".PHP_EOL;
    }
}
5
  • 1
    I want excel file with .xls extention.
    – Avinash
    Feb 25, 2012 at 4:58
  • You're going to need a library since .xls is a binary format. CSV is a simple way to get Excel to open and the user can save as .xls if desired.
    – T.P.
    Feb 25, 2012 at 5:02
  • Sorry, but its my need. Dont ask why.
    – Avinash
    Feb 25, 2012 at 5:06
  • Requirements are requirements :)
    – T.P.
    Feb 25, 2012 at 5:13
  • Yup. we can't say anything beyond that :)
    – Avinash
    Feb 25, 2012 at 5:18
0

This excel.php file could prove useful to you as an alternative to php-excel. I've never personally used it, but the description sounds like it could work for you.

http://www.phpclasses.org/package/1919-PHP-Stream-wrapper-to-read-and-write-MS-Excel-files.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.