首先需要去官网http://www.php.cn/xiazai/leiku/1491,下载后只需要Classes目录下的文件即可。
1、PHPExcel导出方法实现过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
2、PHPExcel导入方法实现过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
不用PHPExcel类也能导出数据:
/*
* @creator Jimmy
* @data 2018/1/05
* @desc 数据导出到excel(csv文件)
* @param $filename 导出的csv文件名称 如date("Y年m月j日").'-test.csv'
* @param array $tileArray 所有列名称
* @param array $dataArray 所有列数据
*/
public function exportToExcel($filename, $tileArray=[], $dataArray=[]){
ini_set('memory_limit','512M');
ini_set('max_execution_time',0);
ob_end_clean();
ob_start();
header("Content-Type: text/csv");
header("Content-Disposition:filename=".$filename);
// $fp=fopen('php://output','w');
$fp=fopen('D:\phpStudy\PHPTutorial\WWW\juyouyu\Source\API\backend\web\\'.$filename,'w');
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))
fputcsv($fp,$tileArray);
$index = 0;
foreach ($dataArray as $item) {
if($index==1000){
$index=0;
ob_flush();
flush();
}
$index++;
fputcsv($fp,$item);
} ob_flush();
flush();
ob_end_clean();
} public function actionIndex()
{
$a = array('姓名','年龄');
$b = array(array('a',21),array('b',23));
$filename = date("Y年m月j日").'-test.csv';
$this->exportToExcel($filename,$a,$b);
}