简体   繁体   English

在zend框架中导出csv

[英]exporting csv in zend framework

I know how to export a csv file in php. 我知道如何在php中导出csv文件。 Now I want to do this task in zendframe work. 现在我想在zendframe工作中完成这个任务。

1 I want to know is there any built in functionality that zend provides for this purposes or we have to write the same code that we use in simple php? 1我想知道zend是否有为此目的提供的内置功能,或者我们必须编写我们在简单的php中使用的相同代码?

2 If I have to use same code then where should I put that whether in model or controller? 2如果我必须使用相同的代码,那么我应该把它放在模型或控制器中?

3 I have a view where user can select start date, last date and user andthen press enter. 3我有一个视图,用户可以选择开始日期,最后日期和用户,然后按Enter键。 On the basis of that selection I want to exprot csv file. 在那个选择的基础上我想exprot csv文件。

Any one guide me how to accomplish this task 任何人都指导我如何完成这项任务

To address your points in order 按顺序解决您的问题

  1. There is not a component in the Zend Framework that will generate and dispose of CSV files. Zend Framework中没有可生成和处理CSV文件的组件。

  2. Your controller should just collect request parameters (ie date ranges from point 3) and initiate a response. 您的控制器应该只收集请求参数(即从第3点开始的日期范围)并启动响应。 I would create another class which can generate a CSV and then use this Controller Action Helper, SendFile to actually send the CSV file as a download. 我将创建另一个可以生成CSV的类,然后使用此Controller Action Helper,SendFile实际发送CSV文件作为下载。

  3. Have your controller receive the request parameters using a form. 让控制器使用表单接收请求参数。 Generate a CSV using your class (point 2) and then use the helper to send the response. 使用您的类生成CSV(第2点),然后使用帮助程序发送响应。

For generating CSV files it's recommended to use fputcsv rather than building strings yourself. 为了生成CSV文件,建议使用fputcsv而不是自己构建字符串。

Here is a Zend Framework Action Helper - may be helpful. 这是一个Zend框架行动助手 - 可能会有所帮助。 Note the site and code comments are in German. 请注意,网站和代码注释均为德语。 http://blog.abmeier.de/zend-csv-action-helper http://blog.abmeier.de/zend-csv-action-helper

Building on regilero's answer for this question , I did come up with a Zend solution. 基于regilero对这个问题的回答,我确实想出了一个Zend解决方案。 Just use a complete query on the database itself: 只需对数据库本身使用完整的查询:

$result = $this->_db->query(
     'SELECT * FROM `table_xy`'
    .'INTO OUTFILE \'tmp/outputfile.csv\''
    .'FIELDS TERMINATED BY \';\''
    .'ENCLOSED BY \'"\''
    .'LINES TERMINATED BY \'\\n\''
);

Just don't forget to set up the necessary privileges for that user. 只是不要忘记为该用户设置必要的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM