PHP(Thinkphp框架)将数据表导出csv文件
生活随笔
收集整理的這篇文章主要介紹了
PHP(Thinkphp框架)将数据表导出csv文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CSV文件類似于excel文件,用逗號表示分隔符,換行符代表該行結束
訪問方法
public function export_csv(){$csvModel = M('select_question');$csvData = $csvModel->field('question,answer_A,answer_B,answer_C,answer_D,true_answer')->select();$str = "題目,答案A,答案B,答案C,答案D,正確答案\n";$str = iconv('utf-8', 'gb2312', $str);foreach ($csvData as $item) {$question = iconv('utf-8', 'gb2312', $item['question']);$a = iconv('utf-8', 'gb2312', $item['answer_a']);$b = iconv('utf-8', 'gb2312', $item['answer_b']);$c = iconv('utf-8', 'gb2312', $item['answer_c']);$d = iconv('utf-8', 'gb2312', $item['answer_d']);$t = iconv('utf-8', 'gb2312', $item['true_answer']);$str .= $question . "," . $a . "," . $b . "," . $c . "," . $d . "," . $t . "\n";}$filename = '選擇題.csv';$this->export_filename($filename, $str);}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
請求頭導出方法
public function export_filename($filename,$data){header("Content-type:text/csv");header("Content-Disposition:attachment;filename=" . $filename);header('Cache-Control:must-revalidate,post-check=0,pre-check=0');header('Expires:0');header('Pragma:public');echo $data;}來源:http://blog.csdn.net/a2824256/article/details/53897844
總結
以上是生活随笔為你收集整理的PHP(Thinkphp框架)将数据表导出csv文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: csv数据的导出
- 下一篇: 高端装备的股票有哪些 这几家都得好好了解