public function download($header, $data, $fileName)
{
$config = [
'path' => $this->getTmpDir() . '/',
];
$now = date('YmdHis');
$fileName = $fileName . $now . '.xlsx';
$xlsxObject = new \Vtiful\Kernel\Excel($config);
$fileObject = $xlsxObject->fileName($fileName);
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$style = $format->bold()->background(
\Vtiful\Kernel\Format::COLOR_YELLOW
)->align(Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();
$fileObject->header($header)
->data($data)
->freezePanes(1, 0)
->setRow('A1', 20, $style);
$filePath = $fileObject->output();
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
header('Content-Length: ' . filesize($filePath));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
if (copy($filePath, 'php://output') === false) {
throw new RuntimeException('导出失败');
}
@unlink($filePath);
return true;
}
private function getTmpDir()
{
$tmp = ini_get('upload_tmp_dir');
if ($tmp !== False && file_exists($tmp)) {
return realpath($tmp);
}
return realpath(sys_get_temp_dir());
}
public function readFile($path,$fileName)
{
$config = ['path' => $path];
$excel = new \Vtiful\Kernel\Excel($config);
$data = $excel->openFile($fileName)
->openSheet()
->getSheetData();
return $data;
}