1

我在使用 spout 库生成 excel 时遇到问题。它需要很长时间才能完成。我有大量数据要导出。这将在 Excel 中生成至少 600 行和 98 列。

require(APPPATH .'libraries/spout/src/Spout/Autoloader/autoload.php');  
$this->load->model('Report_model','report');
$pay_ele = $this->report->pay_elements_data();// iT has 70+ codes like 
hra,Da etc.
$pay_code = '';
$array = [];
array_push($array,'E- 
ID','Name','Month','Year','Department','Grade','Accno','Bank 
Name','IFSC','Aadhar','PFNo','ESI','PAN','Working 
Days','Holidays','Weekoffs','Presents','Leaves','Absents');
foreach( $pay_ele as $pay) {
array_push($array,$pay->code);
}
array_push($array, 'Gross Pay');
array_push($array, 'Net Pay');
$writer->addRowWithStyle($array, $style);
if(empty($employee_id)) {
$employees  = $this->report->find_all_employee_ids();
}
foreach ( $employees as $emp) {
$employee_id    = $emp->employee_id;
$name           = $emp->full_name;
$department     = $emp->company_department;
$grades         = $emp->category_code;
$bank_details       = $this->report->find_bank_details($employee_id);
$p_det              = $this->report->personal_detail($employee_id);
$mon_dat  = $this->report->find_all_monhtlydata($employee_id,$month,$year);
if(!empty($mon_dat)) {
foreach ( $mon_dat as $mondata) {
$absent_days    = $mondata->absent_days; 
$present_days       = $mondata->present_days;
$working_days   = $mondata->working_days;
$holidays       = $mondata->holidays;
$week_off       = $mondata->week_off;
$leave_days     = $mondata->leave_days;
}
}
foreach( $bank_details as $bank) {
$acc_number = $bank->account_number;
$bank_name = $bank->bank_name;
$ifsc_code = $bank->ifsc_code;
}
$data1 = [$employee_id,$name,$month,$year,$department,$grades,$acc_number,
          $bank_name,$ifsc_code,$addhar,$pf,$esi,$pan,$working_days];
$gtotAmt = 0;
$totAmt = 0;
foreach( $pay_ele as $pay) {
$n_data = $this->report->find_pay_ledger_data_withparametrs
          ($employee_id,$month,$year,$department,$pay->code);
$amt = 0;
foreach( $n_data as $dat) {
$amt = round($dat->amount,2);
$totAmt += round($dat->amount,2);
if($pay->type == 'ADDITION') {
$gtotAmt += round($dat->amount,2);
} }
array_push($data1,$amt); 
}
array_push($data1,$gtotAmt);
array_push($data1,$totAmt);
$writer->addRow($data1);
}
$writer->close();
if (file_exists($filePath)
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; 
filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
}

我想以非常快的模式导出 excel,但实际上需要 2 个多小时。对于少数员工来说,它生成 excel 的速度非常快,但对于 500 多名员工来说却不是这样。

4

2 回答 2

1

600 行,100 列 = 60,000 个单元格。根据文档,生成电子表格的时间不应超过几秒钟。

所以我很确定它花费的时间与 Spout 周围的代码有关(也许仔细检查数据是如何获取的?)。您可以添加日志以查看在您的程序中花费的时间。

$writer->openToFile($filePath)此外,您可以直接使用,而不是使用和读取文件以将其发送到浏览器$writer->openToBrowser($fileName)。无需设置额外的标题。

于 2019-02-04T22:02:40.023 回答
0

您应该开始寻找瓶颈。600 行是不匹配的。你确定问题出在excel库中吗?您将尝试在应用程序的任何部分测量时间。

于 2019-02-04T12:14:38.923 回答