Skip to content

Instantly share code, notes, and snippets.

@mikehaertl
Created August 13, 2015 18:30
Show Gist options
  • Save mikehaertl/280455b503dc8c4ccddd to your computer and use it in GitHub Desktop.
Save mikehaertl/280455b503dc8c4ccddd to your computer and use it in GitHub Desktop.
<?php
/**
* writeToExcelFiles
*
* $messages is an array of this format:
* [
* 'de' => [
* 'categoryA' =>
* 'text in line 1',
* 'text in line 2',
* ],
* 'categoryB' =>
* 'text in line 1',
* 'text in line 2',
* ],
* ],
* 'en' => [ ... ],
* ]
*/
protected function writeToExcelFiles($messages, $excelDir)
{
foreach ($messages as $language => $categories) {
$excel = new PHPExcel();
$index = 0;
foreach ($categories as $category => $sources) {
$sheet = new PHPExcel_Worksheet($excel, $category);
$excel->addSheet($sheet, $index++);
$sheet->getColumnDimension('A')->setWidth(60);
$sheet->getColumnDimension('B')->setWidth(60);
$sheet->setCellValue('A1', 'Source', PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->setCellValue('B1', 'Translation', PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getStyle('A1:B1')->applyFromArray([
'font' => [
'bold' => true,
]
]);
$row = 2;
foreach ($sources as $source) {
$sheet->setCellValue('A'.$row, $source, PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getRowDimension($row)->setRowHeight(-1);
$row++;
}
$sheet->getStyle('A2:B'.$row)->getAlignment()->setWrapText(true);
}
$excel->removeSheetByIndex($index);
$excel->setActiveSheetIndex(0);
$writer = new PHPExcel_Writer_Excel5($excel);
$writer->save($excelDir.DIRECTORY_SEPARATOR.$language.'.xls');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment