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.

Avinash
  • 710
  • 2
  • 9
  • 24
  • xls and xlsx are both pretty involved formats, you will most likely need a heavy library – j_mcnally Feb 25 '12 at 04: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. – prodigitalson Feb 25 '12 at 04: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 '12 at 04:56
  • The most easy way is here http://stackoverflow.com/questions/11189021/reports-in-codeigniter – Muhammad Raheel Jun 25 '12 at 13:37

3 Answers3

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.

icyrock.com
  • 25,648
  • 4
  • 58
  • 78
  • I dont want PEAR libraries to installation. – Avinash Feb 25 '12 at 04:32
  • Sorry, didn't think when you said "lightweight" that you mean no PEAR. Did you see this: http://code.google.com/p/php-excel/? I don't know how lightweight you can go in Excel case... – icyrock.com Feb 25 '12 at 04:38
  • 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 '12 at 04:55
  • The zip file is 4.4 KB: http://code.google.com/p/php-excel/downloads/detail?name=php-excel-v1.1-20090910.zip&can=2&q=. Is this the one you downloaded? It generates xslx, though. – icyrock.com Feb 25 '12 at 05:12
  • 1
    Can you try this one: http://code.google.com/p/phpexportxlsclass/downloads/detail?name=export-xls.class-v1.01.zip – icyrock.com Feb 25 '12 at 05:23
  • This library solves my issue. http://code.google.com/p/phpexportxlsclass/downloads/detail?name=export-xls.class-v1.01.zip – Avinash Feb 25 '12 at 05:25
  • Thanks All specially icyrock.com – Avinash Feb 25 '12 at 05:25
  • If you strip the 3rd party tcPDF library (only used for PDF export) from PHPExcel, you reduce 19.8MB to 3MB. It should be noted that tcPDF will no longer be bundled with PHPExcel after version 1.7.8 – Mark Baker Feb 25 '12 at 08:54
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;
    }
}
Community
  • 1
  • 1
T.P.
  • 2,705
  • 1
  • 19
  • 20
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

jeremysawesome
  • 6,457
  • 3
  • 32
  • 35