使用 PHP Codeigniter 创建 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9441221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:49:52  来源:igfitidea点击:

Create Excel file using PHP Codeigniter

phpexcelcodeigniter

提问by Avinash

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

我想创建一个包含两行的简单 excel 文件。例子:

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.

是否有任何轻量级库或一些代码片段,以便我可以轻松实现这一点。

回答by icyrock.com

回答by T.P.

This has been answered at the following link:

这已在以下链接中得到解答:

How to use the CSV MIME-type?

如何使用 CSV MIME 类型?

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

尽管它输出的是 CSV MIME 类型,但 Excel 通常是 CSV 的默认应用程序。

Oops. Forgot the CodeIgniter part :)

哎呀。忘记了 CodeIgniter 部分:)

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

根据上面的链接,你可以创建一个类似于下面的控制器,假设 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;
    }
}

回答by jeremysawesome

This excel.phpfile 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.

这个excel.php文件可以作为 php-excel 的替代品对你有用。我从来没有亲自使用过它,但描述听起来对你有用。

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

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