@@ -29,7 +29,7 @@ class Excel
2929 * @throws \PhpOffice\PhpSpreadsheet\Exception
3030 * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
3131 */
32- public static function exportData ($ list = [], $ header = [], $ filename = '' , $ suffix = 'xlsx ' )
32+ public static function exportData ($ list = [], $ header = [], $ filename = '' , $ suffix = 'xlsx ' , $ path = '' )
3333 {
3434 if (!is_array ($ list ) || !is_array ($ header )) {
3535 return false ;
@@ -78,37 +78,53 @@ public static function exportData($list = [], $header = [], $filename = '', $suf
7878 {
7979 case 'xlsx ' :
8080 $ writer = new Xlsx ($ spreadsheet );
81- header ("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8; " );
82- header ("Content-Disposition: inline;filename= \"{$ filename }.xlsx \"" );
83- header ('Cache-Control: max-age=0 ' );
84- $ writer ->save ('php://output ' );
81+ if (!empty ($ path )) {
82+ $ writer ->save ($ path );
83+ } else {
84+ header ("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8; " );
85+ header ("Content-Disposition: inline;filename= \"{$ filename }.xlsx \"" );
86+ header ('Cache-Control: max-age=0 ' );
87+ $ writer ->save ('php://output ' );
88+ }
8589 exit ();
8690
8791 break ;
8892 case 'xls ' :
8993 $ writer = new Xls ($ spreadsheet );
90- header ("Content-Type:application/vnd.ms-excel;charset=utf-8; " );
91- header ("Content-Disposition:inline;filename= \"{$ filename }.xls \"" );
92- header ('Cache-Control: max-age=0 ' );
93- $ writer ->save ('php://output ' );
94+ if (!empty ($ path )) {
95+ $ writer ->save ($ path );
96+ } else {
97+ header ("Content-Type:application/vnd.ms-excel;charset=utf-8; " );
98+ header ("Content-Disposition:inline;filename= \"{$ filename }.xls \"" );
99+ header ('Cache-Control: max-age=0 ' );
100+ $ writer ->save ('php://output ' );
101+ }
94102 exit ();
95103
96104 break ;
97105 case 'csv ' :
98106 $ writer = new Csv ($ spreadsheet );
99- header ("Content-type:text/csv;charset=utf-8; " );
100- header ("Content-Disposition:attachment; filename= {$ filename }.csv " );
101- header ('Cache-Control: max-age=0 ' );
102- $ writer ->save ('php://output ' );
107+ if (!empty ($ path )) {
108+ $ writer ->save ($ path );
109+ } else {
110+ header ("Content-type:text/csv;charset=utf-8; " );
111+ header ("Content-Disposition:attachment; filename= {$ filename }.csv " );
112+ header ('Cache-Control: max-age=0 ' );
113+ $ writer ->save ('php://output ' );
114+ }
103115 exit ();
104116
105117 break ;
106118 case 'html ' :
107119 $ writer = new Html ($ spreadsheet );
108- header ("Content-Type:text/html;charset=utf-8; " );
109- header ("Content-Disposition:attachment;filename= \"{$ filename }. {$ suffix }\"" );
110- header ('Cache-Control: max-age=0 ' );
111- $ writer ->save ('php://output ' );
120+ if (!empty ($ path )) {
121+ $ writer ->save ($ path );
122+ } else {
123+ header ("Content-Type:text/html;charset=utf-8; " );
124+ header ("Content-Disposition:attachment;filename= \"{$ filename }. {$ suffix }\"" );
125+ header ('Cache-Control: max-age=0 ' );
126+ $ writer ->save ('php://output ' );
127+ }
112128 exit ();
113129
114130 break ;
0 commit comments