问题描述
这会将文件存储在 storage
路径中。
public function downloadResults(array $filterarr) {
unset($queryArr);
$start = 0;
$limit = 10000;
$response = $this->getFilterResults($filterarr,true,$start,$limit);
$resultHits = $response['resultHits'];
# if results are empty,throw an exception
if($resultHits->isEmpty())
throw new \Exception("No results found for given filter.",1);
# otherwise continue with the download process...
$exportArr = [];
foreach($resultHits as $key=>$value) {
$list = (array) $value;
array_push($exportArr,$list);
}
$type = 'xls';
$export = Excel::create('result_set_download_'.time(),function($excel) use ($exportArr) {
$excel->sheet('mysheet',function($sheet) use ($exportArr) {
$sheet->fromArray($exportArr);
});
})->store($type,storage_path('app/public/exports'),true);
return $export;
}
如果导出和存储成功,则将存储的文件名发送回浏览器,否则发送错误消息。
try {
$exportArr = $this->product->downloadResults($filterarr);
$status = true;
$jsonArr['filename'] = $exportArr['file'];
} catch (\Exception $e) {
$status = false;
$jsonArr['message'] = $e->getMessage();
} finally {
$jsonArr['success'] = $status;
echo json_encode($jsonArr);
}
到目前为止,这一切正常,但如果导出很少,我会收到此错误:
mysheet!Z2898 -> Formula Error: Unexpected,
导出时没有进行任何计算。
我也在这里找到了一个类似的帖子 Laravel Excel Formula Error: Unexpected operator '='
这是那个帖子的答案:
// Enable calculation
$reader->calculate();
// disable calculation
$reader->calculate(false);
但我不知道把它放在哪里。