![](/img/trans.png)
[英]Irish accent get changed to wiered charecter while processing the csv file in php (yii1.1 framework)
[英]How to download a filtered CSV file with PHP using Yii 1.1 framework
在您進一步閱讀之前,如果我在這里復制,我想道歉。 我已經環顧了很長一段時間,但對如何處理以下情況沒有任何運氣。 由於我是一個新手程序員,整個事情變得更糟。
情況
我在工作時從我的高級程序員那里得到了一項任務,即創建一個可以生成 CSV 文件的函數。 我遇到了一些麻煩,但終於解決了。 函數(位於模型中)看起來像這樣。
// The function below will return a CSV file.
public function convert2CSV(){
$data = $this->search()->rawData;
$fileOpener = fopen('php://output', 'w');
fputcsv($fileOpener, array(
'some code...'
), ';');
foreach ($data as $rows) {
fputcsv($fileOpener, $rows ';');
}
fclose($fileOpener);
}
到現在為止還挺好。 現在我的任務是創建一個也適用於 CSV 文件的過濾器。 我這是什么意思? 好吧,我在網站中使用過濾器,然后導出 CSV 文件,它應該只下載過濾器的結果。 到目前為止,無論我做什么,它都只是下載了它所能達到的最大數量的所有內容。
我試圖通過使用 $_GET 以某種方式告訴上面的 CSV 函數只下載過濾的結果來完成它。 我一直試圖在控制器中這樣做,在調用模型的 CSV 函數的操作中。
public function actionExport2CSV() {
$a = new A();
$b = new B();
// This part will output the headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=TestFile.csv');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
$a->convert2CSV(); {
if (isset($_GET['B'])) {
$b->attributes = $_GET['B'];
}
}
}
}
我感謝您能給我的任何幫助和/或解釋。 我當然更願意了解我做錯了什么,以便學習更多並在未來成為更好的程序員。
非常感謝你們的幫助。
親切的問候。
我用的是EExcelview擴展,非常有用。
/protected/extensions/
的代碼/protected/config/main.php
文件中,如下所示 'import' => array(
'app.models.*',
'app.portlets.*',
'app.components.*',
'app.extensions.*',
...
'ext.phpexcel.*', # here we are importing the extension
),
$this->widget('EExcelView', array(
'dataProvider' => $model->search(),
'creator' => Yii::app()->name,
'grid_mode' => 'grid',
'title' => 'CSV File title',
'subject' => 'CSV File Subject',
'description' => 'CSV File Description',
'exportText' => 'Download file ',
'filename' => urlencode('My file to download'),
'exportType' => 'CSV', # Available Options => Excel5, Excel2007, PDF, HTML, CSV
'template' => "{exportbuttons}",
'columns' => array(
'colA',
'colB'
...
),
));
不介意大家。
我編輯了搜索功能,以便在創建 CSV 文件之前對其進行過濾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.