Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
added short migration guide for symfony serializer + csv encoder
  • Loading branch information
zhil authored Jun 28, 2019
1 parent d2cdc11 commit 30705f2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,37 @@ Remaining todos include:
* Tests and test coverage
* TravisCI
* Improved documentation

## Symfony serializer

If you are migrating from Symfony Serializer component + CSV encoder - you can use code like

```
$spreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnsMap = [];
$lineIndex = 1;
foreach ($data as $line) {
foreach ($line as $columnName=>$columnValue) {
if (!$columnIndex = array_search($columnName, $columnsMap)) {
$columnsMap[] = $columnName;
$columnIndex = count($columnsMap);
}
$sheet->getCellByColumnAndRow($columnIndex, $lineIndex)->setValue($columnValue);
}
$lineIndex++;
}
$writer = $this->get('phpoffice.spreadsheet')->createWriter($spreadsheet, 'Xlsx');
ob_start();
$writer->save('php://output');
$excelOutput = ob_get_clean();
return new Response(
$excelOutput,
200,
[
'content-type' => 'text/x-csv; charset=windows-1251',
'Content-Disposition' => 'attachment; filename="price.xlsx"'
]
);
```

0 comments on commit 30705f2

Please sign in to comment.