Program Tip

다운로드 할 phpexcel

programtip 2020. 10. 13. 19:05
반응형

다운로드 할 phpexcel


안녕하세요 저는 phpexcel을 처음 사용하고 내 서버에 저장하지 않고 클라이언트 다운로드에 만든 Excel을 보내거나 다운로드 한 직후 삭제하는 방법이 있는지 궁금합니다.

사용자가 내가 방금 만든 엑셀로 "팝업"을 제공 할 페이지에 "내보내기 버튼"을 만들려고합니다.

이제 테이블을 만든 후 다음을 수행합니다.

$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);

$objXLS->getActiveSheet()->setTitle('Test Stats');

$objXLS->setActiveSheetIndex(0);

$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
$objWriter->save(__DIR__."/test1.xls");

하지만 그것은 내 서버에 저장합니다

감사합니다


파일에 저장하는 대신 php://output문서에 저장하세요 .

$objWriter->save('php://output');

그러면 브라우저에있는 그대로 전송됩니다.

당신은 몇 가지 추가 할 헤더 문서를 브라우저 유형을 알 수 있도록 파일인지, 파일 다운로드와 그것의 일반적인 같이 제 1 및 그것은 (파일 이름) 방법을 지정해야합니다 :

// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');

// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');

// Write file to the browser
$objWriter->save('php://output');

먼저 헤더를 수행 한 다음 저장합니다. Excel 헤더의 경우 다음 질문도 참조하십시오 . Excel 문서에 대한 MIME 유형 설정 .


$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');

// Do your stuff here

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');

// This line will force the file to download
$writer->save('php://output');

이 통화 사용

$objWriter->save('php://output');

XLS 시트를 현재있는 페이지로 출력하려면 현재 페이지에 다른 에코, 인쇄물, 출력이 없는지 확인하십시오.


XLSX 용

확장자가있는 XLSX의 $ xlsName 이름에 설정 합니다. 예 : $ xlsName = 'teste.xlsx';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

XLS 용

확장자가있는 XLS의 $ xlsName 이름에 설정 . 예 : $ xlsName = 'teste.xls';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

당신이 이미 당신의 문제를 해결했을 가능성이 있습니다.

all files downloaded starts with empty line, in my case where four empty lines, and it make a problem. No matter if you work with readfile(); or save('php://output');, This can be fixed with adding ob_start(); at the beginning of the script and od_end_clean(); just before the readfile(); or save('php://output');.


 header('Content-type: application/vnd.ms-excel');

 header('Content-Disposition: attachment; filename="file.xlsx"');

 header('Cache-Control: max-age=0');

 header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

 header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

 header ('Cache-Control: cache, must-revalidate');

 header ('Pragma: public');

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

 $objWriter->save('php://output');

참고URL : https://stackoverflow.com/questions/8566196/phpexcel-to-download

반응형