使用 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
Create Excel file using PHP Codeigniter
提问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
See this tutorial:
请参阅本教程:
Here's a small (2.7 KB zipped) library to export XLS:
这是一个用于导出 XLS 的小型(2.7 KB 压缩)库:
Here's a similar one to export XLSX (4.4 KB zipped):
这是一个类似的导出 XLSX(4.4 KB 压缩文件):
Both have small working examples.
两者都有小的工作示例。
回答by T.P.
This has been answered at the following link:
这已在以下链接中得到解答:
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.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.
这个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