Create Excel file using PHP Codeigniter

2019-03-22 05:20发布

问题:

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.

回答1:

See this tutorial:

  • http://devzone.zend.com/27/reading-and-writing-spreadsheets-with-php/

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

  • http://code.google.com/p/phpexportxlsclass/downloads/detail?name=export-xls.class-v1.01.zip

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

  • http://code.google.com/p/php-excel/downloads/detail?name=php-excel-v1.1-20090910.zip&can=2&q=

Both have small working examples.



回答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;
    }
}


回答3:

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