關於 PHP 導出 excel csv 經常使用的有 PHPexcel ,本文整理了一些其餘方案。php
sudo apt-get install -y zlib1g-dev git clone https://github.com/jmcnamara/libxlsxwriter.git cd libxlsxwriter make && sudo make install // https://github.com/viest/php-excel-writer // https://laravel-china.org/topics/6888/php-high-performance-excel-extension-five-hundred-year-formula-no-memory-leak#reply37308 for($index = 0 ; $index < 10000 ; $index++){ $data[$index] = ['viest', 23, 666666666666666666, '銀河市地球區程序村PHP組菜鳥灣66號', 15666666666]; } $timeStart = microtime(true); $config = [ 'path' => '/vagrant/', ]; $excel = new \Vtiful\Kernel\Excel($config); $textFile = $excel->fileName("test.xlsx") ->header(['name', 'age', 'id_card', 'address', 'phone']) ->data($data) ->outPut(); $timeEnd = microtime(true); $time = $timeEnd - $timeStart; echo "導出Excel花費: $time seconds\n";
composer require maatwebsite/excel // 導出 Excel 並能直接在瀏覽器下載https://laravel-china.org/topics/1918/extension-how-to-deal-with-the-excel-file-in-the-laravel-project # $export_file_name = 要生成的文件名 Excel::create($export_file_name, function ($excel) { $excel->sheet('Sheetname', function ($sheet) { $sheet->appendRow(['data 1', 'data 2']); $sheet->appendRow(['data 3', 'data 4']); $sheet->appendRow(['data 5', 'data 6']); }); })->download('xls'); // 導出 Excel 並存儲到指定目錄 Excel::create($export_file_name, function ($excel) { $excel->sheet('Sheetname', function ($sheet) { $sheet->appendRow(['data 1', 'data 2']); $sheet->appendRow(['data 3', 'data 4']); $sheet->appendRow(['data 5', 'data 6']); }); })->store('xls', $object_path); 問題: 1. var_dump($reader->toArray());//第一行內容爲key,若是爲中文,須要修改excel.php的 'to_ascii' => false, 2.大數字變成科學計數 如 11111111111 顯示成 1.11111E+29 導出 excel 錯誤 PHPExcel_Calculation_Exception: Q5!A65 → Formula Error: An unexpected error occured in /application/www/web_git-pull/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php:291 在excel中一個單元格若是是以「=」開頭,則說明這個單元格是根據其餘單元格的值算出來的,「=」後面必須跟着一個合法的表達式,而那個字符串是用戶的輸入,很明顯不該該是一個合法的表達式,因此應該在代碼中過濾掉或者 $str = 「\t」.$str;
//https://github.com/mk-j/PHP_XLSXWriter/tree/master/examples 有不少測試代碼 include_once("xlsxwriter.class.php"); ini_set('display_errors', 0); ini_set('log_errors', 1); error_reporting(E_ALL & ~E_NOTICE); $filename = "example.xlsx"; // for($index = 0 ; $index < 250000 ; $index++){ $data[$index] = ['viest', 23, 666666666666666666, '銀河市地球區程序村PHP組菜鳥灣66號', 15666666666]; } $timeStart = microtime(true); $writer = new XLSXWriter(); $writer->writeSheet($data); $writer->writeToFile('example.xlsx'); $timeEnd = microtime(true); $time = $timeEnd - $timeStart; echo "導出Excel花費: $time seconds\n"; echo '#'.floor((memory_get_peak_usage())/1024/1024)."MB"."\n";
//https://github.com/bean-du/excel $writer = new ExcelWriter(); for ($i = 0; $i < 100; $i++){ for ($j = 0; $j < 10; $j++){ $data[$i][$j] = 'test ['.$i.']+['.$j.']'; } } $writer->setWidth(range('A','J')) ->setValue($data) ->setFormat('xls') ->setAlignment(array('A1','C1'),'CENTER') ->setBackgroundColor(array('A1','C1'),'#ccccc') ->setAlignment(array('A2','C2'),'RIGHT') ->setActiveSheetName('Bean') ->outPut('test.xls');
function export_csv($filename) { 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'); }
PDF、PPT、Excel、Word、視頻等格式文件在線預覽 http://jquery.malsup.com/media/ https://view.officeapps.live....{yourFileOnlinePath}
提供超大文件上傳的Laravel擴展包https://github.com/peinhu/Aet...
PHP高效導出Excel https://segmentfault.com/a/11...
打造最全面的PHPExcel開發解決方案 https://segmentfault.com/a/11...jquery