php 导出 excel

关于 PHP 导出 excel csv 常用的有 PHPexcel ,本文整理了一些其他方案。

高性能 Excel 扩展

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";

laravel-excel

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";

Excel处理包

//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');

down csv

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...

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章