Skip to content

Instantly share code, notes, and snippets.

@neotohin
Created March 14, 2023 06:28
Show Gist options
  • Save neotohin/b250cde34e6123247085ce87ba07761a to your computer and use it in GitHub Desktop.
Save neotohin/b250cde34e6123247085ce87ba07761a to your computer and use it in GitHub Desktop.
PHPExcel to generate xls from array
function erecruitment_export_write_excel( $data, $uri){
if( count($data) == 0 ) return '';
// load phpexcel library
$module_path = drupal_get_path('module', 'erecruitment');
require_once( $module_path . '/vendor/autoload.php' );
$doc = new PHPExcel();
$active_sheet = 0 ;
foreach( $data as $job_id => $rows ) {
// Add new sheet
$doc->createSheet( $active_sheet );
$doc->setActiveSheetIndex( $active_sheet++ );
erecruitment_export_write_excel_sheet( $doc, $job_id, $rows);
}
// Write The Excel file
$writer = PHPExcel_IOFactory::createWriter($doc, 'Excel5');
$writer->save( drupal_realpath( $uri ) );
}
function erecruitment_export_write_excel_sheet( &$doc, $job_id, $data ) {
// Pre calculate all First Level and Second Level Header
$header = array();
foreach( $data as $row ){
// Loop Through Each row Items
foreach( $row as $key => $item ) {
if( ! isset( $header[ $key ] ) ) {
$header[ $key ] = array(
'span' => 1,
'secondary_keys' => array(),
);
}
// If particular row item has an array then consider that as second level
// then generate 2nd level keys and update 1st level key's cell merge count
if( is_array( $item) ) {
foreach( $item as $secondaryKey => $secondaryValue) {
if( isset( $header[$key]['secondary_keys'][ $secondaryKey]) ) continue;
$header[$key]['secondary_keys'][ $secondaryKey] = 1;
$header[$key]['span'] = count( $header[$key]['secondary_keys'] );
}
}
}
}
// Print Job Details
$job_title = 'No Job Selected';
if( $job_id != 0 ) {
$job_node = node_load( $job_id );
$job_title = $job_node->title;
}
$doc->getActiveSheet()
->setCellValueByColumnAndRow( 0, 1, 'Job Title')
->setCellValueByColumnAndRow( 1, 1, $job_title)
->mergeCellsByColumnAndRow( 1, 1, 14, 1);
// Print Header
$hasSecondaryRow = false;
$rowIndex = 3;
$col = 0;
foreach( $header as $key => $row ) {
// See If First Level Row as secondary Keys
if( $row['span'] > 1 or count( $row['secondary_keys']) > 0 ) {
$hasSecondaryRow = true;
// In that case merge cells for first level key
$doc->getActiveSheet()->mergeCellsByColumnAndRow( $col, $rowIndex, ($col + $row['span'] - 1), $rowIndex);
$sec_key_start = $col;
// Loop Through to fill Secondary Keys on 2nd row
foreach( $row['secondary_keys'] as $sec_key => $sec_value ) {
$doc->getActiveSheet()->setCellValueByColumnAndRow( $sec_key_start++, $rowIndex + 1, $sec_key);
}
}
$doc->getActiveSheet()->setCellValueByColumnAndRow( $col, $rowIndex, $key);
$col += $row['span'];
}
// Now check if there is any secondary header. If yes, then increase data row count by 1
$rowIndex = $hasSecondaryRow ? ( $rowIndex + 2) : ( $rowIndex + 1) ;
// Print Actual Data
foreach( $data as $row ){
$column = 0;
// Loop Through Header
foreach( $header as $key => $item ) {
// If header has more than one secondary item then traverse 2nd level
if( $item['span'] > 1 or count( $item['secondary_keys']) > 0 ) {
foreach( $item['secondary_keys'] as $sec_key => $value ) {
$doc->getActiveSheet()->setCellValueByColumnAndRow( $column++, $rowIndex,
isset( $row[ $key ][ $sec_key ] ) ? $row[ $key ][ $sec_key ] : '' );
}
}
// Else this is a single level header so just print out data
else {
$doc->getActiveSheet()->setCellValueByColumnAndRow( $column++, $rowIndex,
isset( $row[ $key ] ) ? $row[ $key ] : '' );
}
}
$rowIndex++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment