如何在 PHP 中使用自动过滤器生成 Excel 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7344707/
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
How to Generate Excel File With Autofilters in PHP?
提问by JaidynReiman
Okay, here's my dilemma.
好的,这是我的困境。
I've been working on a Wordpress plugin for Medical Marcomto automatically update his List of US Twitter Doctors. Basically, it provides the ability to create a form where users can request to be added to the list, requests can be confirmed in the admin panel, when added they're available in the Excel file and the initial data is filled out, and finally, certain fields are automatically updated throughout the week.
我一直在为Medical Marcom 开发Wordpress 插件,以自动更新他的美国 Twitter 医生名单。基本上,它提供了创建表单的能力,用户可以在其中请求添加到列表中,可以在管理面板中确认请求,添加后它们在 Excel 文件中可用并填写初始数据,最后,某些字段会在一周内自动更新。
Here's the problem.
这就是问题所在。
My code is generating an Excel file with PHP using PHPExcel. However, I need to have a simple autofilter applied to the sheet at startup (honestly, I don't know what the big deal is... anyone can easily apply an autofilter in Excel, but he wants it available from the start). So, I tried applying the code I found:
我的代码正在使用 PHPExcel 生成一个带有 PHP 的 Excel 文件。但是,我需要在启动时对工作表应用一个简单的自动过滤器(老实说,我不知道有什么大不了的......任何人都可以轻松地在 Excel 中应用自动过滤器,但他希望它从一开始就可用)。所以,我尝试应用我发现的代码:
$excel->getActiveSheet()->setAutoFilter('A1:J' . $row);
$excel is my PHPExcel instance. $row is the last row being outputted from the database. The file is generated immediately when the url is clicked and PHP's headers are set to translate the output as an Excel file, like so:
$excel 是我的 PHPExcel 实例。$row 是从数据库输出的最后一行。单击 url 时会立即生成该文件,并将 PHP 的标题设置为将输出转换为 Excel 文件,如下所示:
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=" . $file);
But when I open the file, no autofilters are set... I tried posting a question over at PHPExcel's website, but I didn't get any replies, so I decided to ask here.
但是当我打开文件时,没有设置自动过滤器...我尝试在 PHPExcel 的网站上发布一个问题,但没有得到任何回复,所以我决定在这里提问。
Does anyone know what I may be doing wrong? For now he's going with the original file (updated a bit, though) until this issue is resolved.
有谁知道我可能做错了什么?现在,他将使用原始文件(虽然更新了一点),直到这个问题得到解决。
回答by Maxime Rainville
In case anyone comes across this question. The feature has been implemented both for XLSX and XLS.
如果有人遇到这个问题。该功能已为 XLSX 和 XLS 实现。
You just need to specify the range of your header row for it to work:
您只需要指定标题行的范围即可使其工作:
$excel->getActiveSheet()->setAutoFilter('A1:J1');
回答by JMax
I understand from this linkthat it has not been implemented yet.
我从这个链接了解到它尚未实施。
It seems like it is still a work item(with a low priority sorry).
看起来它仍然是一个工作项目(抱歉,优先级低)。
[EDIT] seems like it may work with Excel 2007 (see this work item). Which version of Excel do your client use?
[编辑] 似乎它可以与 Excel 2007 一起使用(请参阅此工作项)。您的客户使用哪个版本的 Excel?
回答by Ng Sek Long
PHPSpreadsheet have a much nicer implementationif you want your whole sheetauto-filtered:
如果您希望自动过滤整个工作表,PHPSpreadsheet 有一个更好的实现:
$sheet->setAutoFilter(
$sheet->calculateWorksheetDimension()
);