前端html代码:
<a href="#" id="Export" class="inlineBlock operatBtn"><i class="icon-plus bigger-130"></i>
导出
</a>
Jquery:
var export_condition = {};
$("#Export").click(function(){
export_condition.time = $("#search2_time").val();
export_condition.client = $("#search2_client").val();
export_condition.username = $("#search2_user_name").val();
export_condition.count = 0;
export_condition.filename = "";
result_export();
});
function result_export()
{
$.post("{{url('/log/log_export')}}",export_condition , function(data){
// console.log(data);
if(data.status === 1) {
// $("#export_show").hide();
layer.confirm('没有数据', {
btn: ['确定'] //按钮
}, function () {
layer.closeAll();
});
} else if (data.status === 2) {
package_file(data.filename);
} else if (data.status === 3) {
export_condition.count = data.count;
export_condition.filename = data.filename;
result_export();
}
});
}
function package_file(filepath)
{
//console.log(filepath);
$.post("{{url('/log/actionPackage')}}", {filepath : filepath}, function(data){
// $("#export_show").hide();
if (data.status === true) {
layer.confirm('导出完毕,是否下载?', {
btn: ['确定', '取消'] //按钮
}, function () {
layer.closeAll();
window.location.href = "/storage/exports/" + data.filename;
}, function () {
});
} else {
layer.confirm('导出失败', {
btn: ['确定'] //按钮
}, function () {
layer.closeAll();
});
}
})
}
php:
写方法之前引入Export.php类文件:
require_once 'app/Http/Models/Home/Export.php';
public function log_export()
{
$where = [];
if(request('time')){
$start = date("Y-m-d",strtotime(explode('-', request('time'))[0]));
$end = date("Y-m-d",strtotime(explode('-', request('time'))[1]))." 23:59:59";
$where[] = ["time",">=",$start];
$where[] = ["time","<=",$end];
}
if(request('client')){
$where[] = ["client",'=',request('client')];
}
if(request('user_name')){
$where[] = ["username",'=',request('user_name')];
}
$filename = request('filename');
$count = request('count');
$offset = $count * 10000;
$data = Log::where($where)->orderBy('time','desc')->offset($offset)->limit(10000)->get()->toArray();
foreach ($data as $key => $value) {
$data[$key]['level'] = $value['level']==3?'失败':'成功';
}
$colFilter = function($col){
return $this->index_col2name($col);
};
$valueFilter = function($col, $value) {
return $this->col2value($col, $value);
};
$res = Export::excel($data, $colFilter, $valueFilter, $filename, $count);
return $res;
}
public function actionPackage()
{
$status = false;
$filePath = request('filepath');
$rootPath = '/home/wwwroot/www/storage/exports/';
//dump($filePath);die;
if (is_dir($rootPath.$filePath))
{
$cmd = "cd ". $rootPath . $filePath . "/" . "\n";
$cmd .= "tar -zcf " . $rootPath . $filePath . ".tar.gz * ";
$result = system($cmd);
if (file_exists($rootPath . $filePath . ".tar.gz"))
{
$status = true;
}
$cmd = "cd ". $rootPath . "\n";
$cmd .= "rm " . $filePath . " -rf";
$result = system($cmd);
}
$res = ['status' => $status, 'filename' => $filePath . ".tar.gz "];
return $res;
}
Export.php
<?php
/**
* Created by zhou.
* User: zhou
* Date: 2015/9/17
* Time: 15:16
*/
namespace App\Http\Controllers\Home;
use PHPExcel;
use PHPExcel_IOFactory;
class Export
{
public static function excel($data, callable $colCallback = null, callable $valueCallback = null, $filename = "", $count)
{
//dump($data);
$rootPath = './storage/exports/';
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Byzoro百卓网络")
->setTitle("Byzoro百卓网络");
if (isset($data["models"])) {
$data = $data["models"];
}
$data_count = count($data);
//$data_count = 0;
if ($data_count == 0 && $filename == ""){
return array('status' => 1,'message' => '无数据导出');
} else if ($data_count === 0 && $filename !== "") {
return array('status' => 2,'message' => '导出完毕','filename' => $filename,'count' => $count);
}
// var_dump($data);die;
$colscount = 0;
$linecount = 2;
$cols = [];
$wcols = [];
foreach ($data as $key => $row) {
foreach ($row as $col => $value) {
if (!isset($cols[$col])) {
$c = $col;
$colCallback !== null && $c = $colCallback($col);
if ($c === "") continue;
$intCol = self::int2col($colscount);
$wcols[$c] = $intCol;
$cols[$col] = $intCol;
$colscount++;
}
$valueCallback !== null && $value = $valueCallback($col, $value);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cols[$col] . $linecount, $value);
}
$linecount++;
}
$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($wcols as $c => $v) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($v . 1, $c);
$objActSheet->getColumnDimension($v)->setWidth(20);
}
// header('Content-Type: application/Excel2007');
// header('Content-Disposition: attachment;filename="Export_' . date("Y_m_d_H_i_s", time()) . '.xls"');
// header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// $objWriter->save('php://output');
//dump($objWriter);die;
$count++;
$filename = $filename == "" ? "Export_" . date("Y_m_d_H_i_s", time()) : $filename;
if (!is_dir($rootPath.$filename))
{
mkdir ($rootPath.$filename, 0777);
//md ($rootPath.$filename);
}
$savename = $rootPath.$filename."/".$filename."(".$count.")".".xls";
$objWriter->save($savename);
// Byzoro::$app->end();
if ($data_count < 10000) {
return array('status' => 2,'message' => '导出完毕','filename' => $filename,'count' => $count);
} else {
return array('status' => 3,'filename' => $filename,'count' => $count);
}
}
public static function int2col($int)
{
$c = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
if ($int < 26) {
return $c[$int];
} elseif ($int > 255) {
throw new Exception("ExportExcel列过多");
} else {
$y = $int % 26;
$i = (intval($int / 26)) - 1;
return $c[$i] . $c[$y];
}
}
}