php 下载导出csv格式
1 //下载合同信息 2 public function actionDownload() 3 { 4 set_time_limit(0); 5 ini_set('memory_limit', '512M'); 6 $criteria = new CDbCriteria(); 7 8 $rent_ids = trim(Yii::app()->request->getParam("rent_ids")); //下载的合同列表 9 //字符串转换为数组 10 if (!empty($rent_ids)) 11 { 12 $criteria->addCondition("f_id in ({$rent_ids})"); 13 } 14 15 $count = T_rent_contract::model()->count(); 16 17 //导出csv格式 18 header("Content-Type:text/csv"); 19 header("Content-Disposition:attachment;filename=" . "_shift_agreement_detail-" . date("Y-m-d") . ".csv"); 20 ob_end_clean(); 21 $delimiter = ','; 22 $table_headers = array( 23 '收款人帐号', 24 '收款人名称', 25 '用途', 26 '金额', 27 ); 28 $header = ''; 29 foreach($table_headers as $table_header) 30 { 31 $header .= $table_header . $delimiter; 32 } 33 $header = substr($header, 0, -1); 34 $header .= "\n"; 35 36 // 输出表头 37 echo iconv('utf-8', 'GBK//IGNORE', $header); 38 39 //每次取数据的步进 40 $step = 1000; 41 $round = ceil($count/$step); 42 for($i = 1 ; $i <= $round ; $i++) 43 { 44 $criteria->limit = $step; //取1条数据,如果小于0,则不作处理 45 $criteria->offset = ($i - 1) * $step; //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10 46 $rent_contract_list = T_rent_contract::model()->findAll($criteria); 47 48 if (count($rent_contract_list)) 49 { 50 foreach($rent_contract_list as $rent_contract) 51 { 52 //用途 53 $purpose = "$rent_contract->f_province_code".'-'."$rent_contract->f_store_code".'-'."$rent_contract->f_cost_center_code"; 54 $f_month_pay_money = $rent_contract['f_month_pay_money']; //固定金额 55 $f_rent = $rent_contract['f_rent'] ; //房租 56 57 //合并房租和物业费 58 $table_contents = array( 59 "\t".$rent_contract->f_rent_number, //房租账号 \t 把数字转字符串 60 $rent_contract->f_rent_name, //账号名称(房租收款人) 61 $rent_contract->f_rent_bank, //开户行(房租收款人) 62 $f_month_pay_money,//固定金额, 64 ); 65 } 66 67 $content = ''; 68 foreach($table_contents as $table_content) 69 { 70 $content .= $table_content . $delimiter; 71 } 72 $content = substr($content, 0, -1); 73 $content .= "\n"; 74 echo iconv('UTF-8', 'GBK//IGNORE', $content); 75 } 76 } 77 }