@@ -235,7 +235,6 @@ public function _getDataAs( $chart_id, $type ) {
235235 }
236236
237237 $ filename = $ title ;
238-
239238 switch ( $ type ) {
240239 case 'csv ' :
241240 $ final = $ this ->_getCSV ( $ rows , $ filename , false );
@@ -324,9 +323,9 @@ private function _getCSV( $rows, $filename, $enclose ) {
324323 * @param string $filename The name of the file to use.
325324 */
326325 private function _getExcel ( $ rows , $ filename ) {
327- // PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet
328- $ chart = substr ( $ filename , 0 , 30 );
329- $ filename .= '.xlsx ' ;
326+ // OpenSpout allows for long sheet names, but let's keep the same limit for compatibility.
327+ $ chart = substr ( $ filename , 0 , 30 );
328+ $ filename .= '.xlsx ' ;
330329 if ( ! apply_filters ( 'vizualizer_export_include_series_type ' , true ) ) {
331330 unset( $ rows [1 ] );
332331 $ rows = array_values ( $ rows );
@@ -339,22 +338,32 @@ function( $r ) {
339338 }
340339 $ vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php ' ;
341340 if ( is_readable ( $ vendor_file ) ) {
342- include_once ( $ vendor_file );
343- }
344- $ xlsData = '' ;
345- if ( class_exists ( 'PhpOffice\PhpSpreadsheet\Spreadsheet ' ) ) {
346- $ doc = new PhpOffice \PhpSpreadsheet \Spreadsheet ();
347- $ doc ->getActiveSheet ()->fromArray ( $ rows , null , 'A1 ' );
348- $ doc ->getActiveSheet ()->setTitle ( sanitize_title ( $ chart ) );
349- $ doc = apply_filters ( 'visualizer_excel_doc ' , $ doc );
350- $ writer = PhpOffice \PhpSpreadsheet \IOFactory::createWriter ( $ doc , 'Xlsx ' );
351- ob_start ();
352- $ writer ->save ( 'php://output ' );
353- $ xlsData = ob_get_contents ();
354- ob_end_clean ();
341+ include_once $ vendor_file ;
342+ }
343+ $ xlsData = '' ;
344+ if ( class_exists ( 'OpenSpout\Writer\Common\Creator\WriterEntityFactory ' ) ) {
345+ try {
346+ // Use OpenSpout to create the XLSX file in memory.
347+ $ writer = \OpenSpout \Writer \Common \Creator \WriterEntityFactory::createXLSXWriter ();
348+ $ writer ->openToFile ( 'php://output ' ); // Open to output instead of a file.
349+ $ writer ->getCurrentSheet ()->setName ( sanitize_title ( $ chart ) );
350+
351+ // Write rows.
352+ foreach ( $ rows as $ row ) {
353+ $ rowFromValues = \OpenSpout \Writer \Common \Creator \WriterEntityFactory::createRowFromArray ( $ row );
354+ $ writer ->addRow ( $ rowFromValues );
355+ }
356+
357+ ob_start ();
358+ $ writer ->close (); // Saves and closes the file in the output buffer.
359+ $ xlsData = ob_get_clean ();
360+ } catch ( Exception $ e ) {
361+ do_action ( 'themeisle_log_event ' , Visualizer_Plugin::NAME , 'OpenSpout writer error: ' . $ e ->getMessage (), 'error ' , __FILE__ , __LINE__ );
362+ error_log ( 'OpenSpout writer error: ' . $ e ->getMessage () );
363+ }
355364 } else {
356- do_action ( 'themeisle_log_event ' , Visualizer_Plugin::NAME , 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist! ' , 'error ' , __FILE__ , __LINE__ );
357- error_log ( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist! ' );
365+ do_action ( 'themeisle_log_event ' , Visualizer_Plugin::NAME , 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist! ' , 'error ' , __FILE__ , __LINE__ );
366+ error_log ( 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist! ' );
358367 }
359368 return array (
360369 'csv ' => 'data:application/vnd.ms-excel;base64, ' . base64_encode ( $ xlsData ),
0 commit comments